Search code examples
cevaluationexpression-evaluation

Multiple operation arithmetic


I just completed a beginners C programming course and I thought handling arithmetic would be a great first project! I had no problem figuring out how to do basic single operation arithmetic.

I ahieved what I initially desired but now I'm curious if it's possible with(out) some library, to handle multiple operators in precedence, e.g. 5+2*3. Of course 2*3 should be evaluated and then 5 added to the product. If parenthesis wrap an equation it should be handled first

So to clarify I want to read stdin and parse a literal string, then handle any arithmetic by PEMDAS precedence. I have tried many different queries to find something like what I'm looking for - I'm sure someone has done it, I just can't find it!


Solution

  • After some time, I managed to write my own infix to postfix equation. The evaluation part.. Not so much!

    I did end up with a functional solution from @Jerry Jeremiah's answer. Using the TinyExpr library, I was able to include their header, compile with their .c program and upon calling te_interp, handle arithmetic better than I could hope to!