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.

Community
  • 1
  • 1
Anicho
  • 2,647
  • 11
  • 48
  • 76
  • 2
    Do you care about order of operations? – rerun Jun 10 '11 at 18:32
  • hadn't thought of that but would prefer normal rules of operator precedence. – Anicho Jun 10 '11 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? – Joe White Jun 10 '11 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 http://stackoverflow.com/questions/4582398/writing-a-simple-equation-parser/4582438#4582438 or try searching SO for "equation parser." – rajah9 Jun 10 '11 at 18:41
  • I quite like this approach, but haven't had the time to check it answers the question. http://social.msdn.microsoft.com/Forums/en-NZ/csharplanguage/thread/31ac96da-415e-424b-9e1f-6aec86c4c3ae – Mike Miller Jun 10 '11 at 18:44
  • Yes seems I am going to learn the shunting-yard algorithm this is exactly the sort of solution I want for my problem, understanding different data structures and algorithms is key to my progression... :D thank you all – Anicho Jun 10 '11 at 19:05
  • Hey Henk, your right no excuse I was searching the wrong things. – Anicho Jun 10 '11 at 19:54

3 Answers3

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.

fardjad
  • 20,031
  • 6
  • 53
  • 68
  • +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. – Pete Jun 10 '11 at 18:43
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)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
loxxy
  • 12,990
  • 2
  • 25
  • 56
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.

Xint0
  • 5,221
  • 2
  • 27
  • 29