1

I tried to make a code that calculates the values of the formulas the user inputs. I.e , if user inputs "10+5" , the program would print "The sum is 15" etc. At first, i thought this is a easy thing to do, but if realized that just using scanf orsth wouldn't do the trick. Then i messed around with arrays and loops to see if the loop encounters "-" or "+" signs in input and then saving the character before "-" or "+" and after it and then calculating it, but i couldnt make this work either. Could you please lead me in the right direction on how to get this done. Thank you very much!

stensootla
  • 13,945
  • 9
  • 45
  • 68
  • 1
    Expression evaluation is usually performed by converting infix notation(for eg. a+b) to postfix notation (for eg. ab+) and then evaluating it. It's a bit more than trivial for beginners, but easy once you get the idea. – vaisakh Feb 27 '12 at 17:14

4 Answers4

2

What you are trying to do is to parse arithmetic expressions, then evaluate them. There is a ton of stuff on this on the internet so, since this is your homework, I'll leave you to Google. Your first thought, that this would be easy to do, is probably a naive thought, though it's not a terribly difficult problem if you don't get too ambitious too quickly.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • Thank you for your reply. I googled it, but didn't find any answer after 10 minutes of searching. It might be because i really don't know the programmer's termins and the technical language used, so my searches are very simple worded and not very specific. And btw, this isn't my homework , i just tagged it so , because people in this forum have previously told me i should tag those kind of questions as homework :d . Thank you. I try searching the net for parse arithmetic expressions and see what i can find . – stensootla Feb 27 '12 at 17:18
  • have a look here http://stackoverflow.com/questions/4589951/parsing-an-arithmetic-expression-and-building-a-tree-from-it-in-java – scibuff Feb 27 '12 at 17:26
2

This can be quite complicated, especially when you get to operator precedence and you need to correctly calculate, for example, 2 + 5 * 6, which needs to be treated as 2 + (5 * 6). The correct way to approach this is to construct an expression tree (just like a compiler would). e.g.

  +
 / \
2   *
   / \
  5   6

You do this by creating binary tree. Each nodes holds an operation and (up to) two subnodes. Then you evaluate your expression by traversing the expression tree.

scibuff
  • 13,377
  • 2
  • 27
  • 30
  • Thank you for your answer. I try to google it for more information , as im just a beginner and don't exactly understand how should this be done. – stensootla Feb 27 '12 at 17:21
  • as @vaisakh mentioned first look up infix, prefix and postfix notation and how to convert from one to another; it is indeed easier to do the evaluation in postfix / prefix notation because you get the operator precedence for free – scibuff Feb 27 '12 at 17:23
1

This might be a little over your head, but what you can do is use a grammer engine for c and a lexical analyzer.

I believe it is called "BISON" and "YYLEX"

From what I remember in school, it is how we made our pascal compiler.

http://en.wikipedia.org/wiki/GNU_bison

After creating a tree. you then can analyze sub trees and then the root node will be the sum of the sub trees.

Kevin
  • 3,509
  • 4
  • 31
  • 45
0

These might be some steps that you might want to consider

  • get the input using getline() or fgets()
  • start reading the string from the beginning
  • do two passes, use one queue for operators and another for operands (numbers)
  • during the first pass, you reach an * or /, read the next number, perform the operation on the next number and the number you read before, and insert the result in a queue
  • also during the first pass, if you read a + or -, silently push the operator and operands into their respective queues
  • during the second phase handle + and -... using queues will help you properly handle successive minuses e.g. 4-3-3

These are not EXACT steps, but it's a heuristic worth looking into - try to work through these, change them according to what makes sense to you etc.

Ambidextrous
  • 810
  • 6
  • 14