Grammar for calculator

I have intensely tried to understand the structure behind Bjarne Stroustrup's calculator (Chapter 6 & 7) from Programming Principles and Practice Using C++, but I can't seem to get through all of it.

So what I did was to try to create my own versions of the calculator. I have roughly created 4 versions of a calculator, slowly progressing. Now, I have somewhat created a parser for the operator and operands. Letting the calculator take inputs such as 2 + 3 * 2 / 4 in one line. Yet, I can't seem to get the order of operations (grammar) to work. As of now, the calculator won't take parentheses or be obedient for the order of operations. Which is something I hope to fix.

So for the actual code. It takes in a left value, an operator and a right value:

1
2
3
4
5
6
7
8
cin >> lval;
while (cin >> op) {
  if (op == ';') {
  cout << "= " << lval << endl;
  keep_window_open();
  getchar();
}
cin >> rval;


Then it proceeds by a switch case to math the values up with the operator between them.

1
2
3
4
5
6
7
8
9
switch (op) {
case '+':
  lval += rval;
  break;
case '-':
  lval -= rval;
  break;  

// Etc.. 


And after this is where my question lands. How can I replace the switch with something that will interept my want of order of operations? I have looked into recursion, RPN, Bjarne's code and so forth. But I can't seem to get it working without something easier to wrap my head around first.

Here's the full source if someones interested: https://repl.it/repls/BleakSimilarKestrel
Last edited on
Is it possible to get some help with integrating the Shaunting Yard algorithm?
Am I wrong to bump this?
Topic archived. No new replies allowed.