Click here to Skip to main content
15,883,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I'm faced with the following problem in my objective-c project for the ipad, maybe somebody has a solution.
I need to parse complex expressions such as: (5+6i)(4-8i) (and many much more complex expressions).
At the moment I need to find a way to convert a string of such an expression into code. For example:

string expression = "(5+6i)(4-8i)";

into:

double complex expressionSolution = (5+6i)(4-8i); (this gives the right answer)

In other words, is there a simple way to convert a string into code?
Posted
Updated 2-Jul-13 14:52pm
v2

1 solution

First thing would be to evaluate the expression into something you can parse:

Converting Postfix Expressions to Infix[^] (In C#, but concept is the same)

Then you need to find out what the tokens are (variables) and assign values to them (in your example, i is a variable token).

After that, its a simple matter of evaluating the infix expression. Infix evaluation is pretty easy, just pop off 2 numbers and an operator, do the operation, push the value back on the stack and repeat until you only have a value on the stack, the value is the answer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900