0

I have an NSString:

n + (n - m)

Can I use DDMathParser for create an expression object.

For example I have base expression n + (n - m), but I need to have ability compare base expression n + (n - m) with an user expression e.g. (n - m) + n that typically the same as well.

Or is there alredy made solution how to compare two NSString considering signs and braces?

So I mean can I for example init two expressions with a string and then compare it like Expression1 = Expression2 using some method.

This is example how expressions have to look in parser

enter image description here

so this is the same struct and we can compare it using left and right node. of the tree. I am interesting to find out this solution that will parse string to expression tree. I think this solution was alredy made.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • 1
    The strings are not equivalent (same chars in the same order), so a compare and an `-isEqualToString:` would fail. NSString was not meant for mathematical equivalency. – CodaFi Nov 06 '12 at 22:56
  • yes I know it. I need a parser that can create tree from nsstring where leaf will be numbers and parent node will be signs, then I can compare this is expression I mean I can compare they structure. so in this case I will not care about order in which digital will be located. do you know what I mean? – Matrosov Oleksandr Nov 06 '12 at 23:00
  • you need to explain a bit more - what exactly do you desire - you want to compare the pattern? - what output do you need? – Jatin Nov 06 '12 at 23:02
  • ok, i will explain. I have NSString object with math elements. These are numbers, signs and letters. So when I have string like this 5 + (5 - 3) that means I have an expression. Of course I can use method for NSString isEqualToString: for full compare two strings, but this is not my purpose. My purpose is compare two expressions. For example if I have expression 5 + (5 - 3) but user enter (5 - 3) + 5 in this case strings are not the same but expressions are the same. you see? thanks for response. I need some parser that can do it - create expressiom from NSSrting and comapre it. – Matrosov Oleksandr Nov 06 '12 at 23:08
  • Well, if you run it through DD, and it gives you an output of two NSNumbers, then surely you could just compare those, right? – CodaFi Nov 06 '12 at 23:09
  • i dont use DD honestly, but I think it can help me. – Matrosov Oleksandr Nov 06 '12 at 23:11
  • and I don't need compare the results. I just need compare struct – Matrosov Oleksandr Nov 06 '12 at 23:12
  • I have upadted questions with images, that shows which expression I need – Matrosov Oleksandr Nov 06 '12 at 23:20

1 Answers1

1

Yes, you can use DDMathParser to do this:

NSString *string = @"5 + (5 - 3)";
NSError *error = nil;
DDExpression *expression = [DDExpression expressionFromString:string error:&error];

if (expression) {
  NSLog(@"%@", expression)
} else {
  NSLog(@"%@", error);
}

DDMathParser does have some (rudimentary) support for expression rewriting, but other than that it'd be up to you to compare the expression trees yourself.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498