0

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!

phuclv
  • 37,963
  • 15
  • 156
  • 475
wick3dr0se
  • 19
  • 4
  • 4
    https://en.m.wikipedia.org/wiki/Shunting_yard_algorithm – David Ranieri Jun 30 '22 at 00:33
  • 3
    Use a stack to push and pop operators and operands – stark Jun 30 '22 at 00:33
  • Here is a tutorial: https://www.tutorialspoint.com/dsa_using_c/dsa_using_c_parsing_expressions.htm And then this less tutorial-ish version: https://cp-algorithms.com/string/expression_parsing.html And an actual library that does it: https://github.com/codeplea/tinyexpr – Jerry Jeremiah Jun 30 '22 at 01:12
  • duplicates: [How to evaluate a mathematical expression using strings and arrays in C](https://stackoverflow.com/q/26574098/995714), [Is there a way to run C code that's stored in a string](https://stackoverflow.com/q/66094374/995714), [What is the best way to evaluate mathematical expressions?](https://stackoverflow.com/q/5115872/995714) – phuclv Jun 30 '22 at 01:26
  • Does this answer your question? [Is there a way to run C code that's stored in a string](https://stackoverflow.com/questions/66094374/is-there-a-way-to-run-c-code-thats-stored-in-a-string) – phuclv Jun 30 '22 at 01:26
  • You're looking at ASTs here. [Abstract Syntax Trees](https://en.wikipedia.org/wiki/Abstract_syntax_tree). – Shark Jun 30 '22 at 14:47
  • 1
    Or just binary expression trees. – Lundin Jun 30 '22 at 14:49
  • arithmetic doesn't really require a full fledged grammar, so lexical scanner and semantics can be bypassed/skipped. A stack would work well too, however for it to work - you'd need to transform your arithmetic expression from [infix notation](https://en.wikipedia.org/wiki/Infix_notation) to [prefix notation](https://en.wikipedia.org/wiki/Polish_notation) as that is much more stack-friendly. However, i might be wrong on this, but i believe transforming it from infix to prefix would require it to be parsed into an AST regardless. – Shark Jun 30 '22 at 14:52
  • @Lundin yes, or that. arithmetic doesn't really have operations that work on more than two operands. :) i was merely suggesting a direction to be considered. – Shark Jun 30 '22 at 14:54
  • It's easy to write a simple [recursive descent parser](https://en.wikipedia.org/wiki/Recursive_descent_parser) that evaluates as it goes. See for example [this answer](https://stackoverflow.com/questions/14472680#66104290). – Steve Summit Jun 30 '22 at 15:16
  • I am working on implementing a push and pop stack currently, as suggested by @stark. I looked up infix to postfix in C after reading about the shunting yard algorithim linked initally. Trying to implement it now – wick3dr0se Jun 30 '22 at 17:18

1 Answers1

0

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!

wick3dr0se
  • 19
  • 4