Need some help with my post-fix calculator

Hello,

I'm trying to write a stack, and post-fix calculator. With the input that I'll be receiving, there will be a '#' before every number, a '!' before every operator, and a '.' to end the expression. I don't need to worry about error checking at all, and need to account for a space between every number and symbol. Input won't come in any other way.

With what I tried, I just tried to implement a basic stack class, and tried my best at the calculator. So far, when I enter the input into the console, it just goes to the next line. I'm pretty sure I have more things wrong, though.

Thanks for looking.

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

#include <iostream>
#include <string>
#include <cstdlib>
#include <math.h>
#include <sstream>

using namespace std;

class Stack{

    public:

    Stack(){
        this->size = 0;
    }
    Stack & push(double c){

        if (size == MAX_SIZE) die("Push Overflow");
        data[size] = c;
        size++;
        return *this;

    }
    double pop(){

        return data[--size];

    }

    double top() const{

        return data[size-1];

    }

    unsigned getSize() const{return size;}

    bool die(const string & msg){

        cerr << "Fatal Error: " << msg << endl;
        exit(EXIT_FAILURE);

    }

    void show(){

        for (int i = 0; i < size; i++){
            cout << data[i] << endl;
    }}

    private:

    static const unsigned MAX_SIZE = 50;
    double data[MAX_SIZE];
    unsigned size;

};

int main(){

    Stack s1;
    bool inputComplete = false;

    double num1 =0;
    double num2 = 0;
    double answer = 0;
    char c, ch;

    while(!inputComplete){

        cin >> ch;
        if (ch == '!'){

            cin >> c;

            num1 = s1.pop();
            num2 = s1.pop();

            switch(c){

                case '/':
                answer = num2/num1;
                break;

                case '^':
                answer = pow(num2, num1);
                break;

                case '*':
                answer = num2 * num1;
                break;

                case '-':
                answer = num2 - num1;
                break;

            }
            s1.push(answer);
        }

        else if (c == '#'){

            double number;
            cin >> number;

            s1.push(number);

        }

        else if (c == '.'){

            inputComplete == true;

        }
    }

    s1.show();

    return 0;
}
Last edited on
Just a little update, swapped out the cin.get's for just cin >> so that I ignore the whitespace, and don't have to worry about it. Getting a seg fault now.
Topic archived. No new replies allowed.