Calculator

So I`m writing a calculator program and I need some help with an easy fix.

The only problem that I see is that twonum needs to be the first thing that
is popping off the stack when you are preparing to do a calculation, but I don`t quite know how to do this.

These are my two test runs with the program:
12 682 5 + *
The answer is: 8244
Please enter your expression:
15 8 -
The answer is: -7
Please enter your expression:

Notice that the first one works fine, because + and * are commutative, but the second one is getting a negative because it's subtracting 15 from 8 instead of the other way around.

It is also an issue with / so when I do
12 679 5 + 4 * 8 / +
The answer is: 12
you are doing 8 / 2736, which is 0, and then when you add 12 to that you get 12.
It should have been doing 2734 / 8 and then adding 12 to that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
main.c

#include <stack>
#include <iostream>
#include "calc_useful.h"
using namespace std;


int main()
{
  while(true)
  {
    // declare your stack here
    stack<int> stk;
    char c;
    int onenum, twonum;
    cout<<"Please enter your expression:\n";

    c = cin.get();
    while(c != '\n')
    {
      if(isdigit(c))
      {
        cin.putback(c);
        cin>>onenum;
        stk.push(onenum);
        // stack operation here.
      }
      else if(isop(c))
      {
        // if the stack is empty you will get a error.
      if(stk.empty())
      {
        cout<<"Wrong Expression!"<<endl;
        return 0;
      }
      onenum = stk.top();
      stk.pop();
      if(stk.empty())
      {
        cout<<"Wrong Expression!"<<endl;
        return 0;
      }
      twonum = stk.top();
      stk.pop();
      onenum = evaluate(onenum,twonum,c);
      stk.push(onenum);
      // here is where you have to pop a couple of numbers,
      // apply the operator to the numbers
      // and then push the result back into the stack            
      }
        c = cin.get(); // reading at the bottom of the sentinel loop
    }
        // this is where you get your final answer off the stack
        // it should be the only number left on the stack at this point
    if(stk.empty())
    {
      cout<<"Wrong Expression!"<<endl;
      return 0;
    }
    onenum = stk.top();
    stk.pop();
    if(!stk.empty())
    {
      cout<<"Error. Insufficient operators for operands.\n";
      return -1;
    }
    cout<<"The answer is: "<< onenum<<endl;
  }
}

cu.cc-----------------------
bool isop(char op){
        return op =='+' || op == '-' || op == '*' || op == '/';
}

int evaluate(int num1, int num2, char op){
    if(op == '+') return num1 + num2;
    if(op == '-') return num1-num2;
    if(op == '*') return num1*num2;
    if(op == '/') return num1/num2;
    else return 0;
}

double evaluate(double num1, double num2, char op){
    if(op == '+') return num1 + num2;
    if(op == '-') return num1-num2;
    if(op == '*') return num1*num2;
    if(op == '/') return num1/num2;
    else return 0;
}

cu.h---------------------------

double evaluate(double num1, double num2, char op);
int evaluate(int num1, int num2, char op);
bool isop(char op);

this is because you are popping them from a stack, which is reverse order.

pop num2 before num1, thats the order they were pushed.
Can I ask you another question? so I fixed the first problem, now if I wanted the loop to continue after the "wrong expression!" and "Error. Insufficient operators for operands." how do I got about doing that. I tried returning true but that did not work.
remove the returns and add else clauses....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    if(stk.empty())
    {
      cout<<"Wrong Expression!"<<endl;
    }
    else
    {
        onenum = stk.top();
        stk.pop();
        if(!stk.empty())
       {
          cout<<"Error. Insufficient operators for operands.\n";
       }
       else
       {
            cout<<"The answer is: "<< onenum<<endl;
        }
    }


you will also need a mechanism for the user to get out of the "while(true)" to end the program. the following should quit if the user just hits return instead of entering an expression.
1
2
3
4
5
6
7
...
c = cin.get();
if (!cin.good() or c==char(13))
   return 0;

while(c != '\n')
...
Last edited on
Topic archived. No new replies allowed.