3

Possible Duplicate:
Evaluate C# string with math operators

So lets say I have a basic string with the value "1*2+4"

How would I go about parsing the information and then doing the calculation?

I am self teaching c# but my assumption is this is a common issue in programming but without using a library how would one do this?

My results so far have got me splitting the string up and putting them into an array of chars, but this is where I've stopped since I am trying to figure out how to compare chars to operators and chars to ints.

I am not sure if I am going the right way about this but would be great if someone could point me in the right direction.

Thank you for your help in advanced.

7
  • 2
    Do you care about order of operations? Commented Jun 10, 2011 at 18:32
  • hadn't thought of that but would prefer normal rules of operator precedence. Commented Jun 10, 2011 at 18:37
  • "without using a library" Why the restriction? You're assuming it's a common operation, but you don't want to use a solution that somebody else has already come up with? Commented Jun 10, 2011 at 18:41
  • 1
    Assuming you want 4+2*3 to be 10 (that is, multiplication or division precede addition or subtraction), then please take a look at stackoverflow.com/questions/4582398/… or try searching SO for "equation parser." Commented Jun 10, 2011 at 18:41
  • I quite like this approach, but haven't had the time to check it answers the question. social.msdn.microsoft.com/Forums/en-NZ/csharplanguage/thread/… Commented Jun 10, 2011 at 18:44

3 Answers 3

11

What you're looking for is the Shunting-yard algorithm. You'll need at least two stacks; one for storing operators and one for operands. After you fill the stacks you can make a RPN and calculate the answer.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I recently had to implement a school assignment very similar to this (I used words with operators like & and |). Shunting yard algorithm is what I used as well. You'll become familiar with Reverse Polish Notations, stacks and queues. It's a decent exercise.
3

Well c# (or any other language) might provide you with various tools to help you, but the overall approach to the problem will always remain the same whatever the programming language be.

So yes, you do split up into operators & integers. You do recognize the characters one by one, but try to do it in the most efficient way of the language. Fosco's anser points to the right link. Use Ncalc Library than doing manual labor.

However, to complete what you started :

int.Parse(str) int.TryParse(str, out num) 

...are the functions you may consider to convert character strings into integers (which you got, by using split() function?) in C#. You can read about them here...(Parse, TryParse)

1 Comment

That's helpful the parse function make life a lot easier....
2

If you want to learn how the various existing libraries do it, you should learn about parsing, lexical and syntactic analysis, expression trees, compiler theory, etc. Also, go through the source-code of any of the multiple open-source libraries that do it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.