RN Calculator with Stacks

I am in need of help with my stack::peek function, I plan on calling the peek function at the end to display the correct result however I am a little lost regarding the programming required. any help is appreciated.

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  #include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

struct NODE
{
    float num;
    NODE *next;
};

class stack
{
  private:
    NODE *head;

  public:
    stack();
    void push(float);
    float pop();
    int nElements();
    float display();
};

class RPN : public stack
{
  public:
    void add();
    void subtract();
    void multiply();
    void divide();
};

stack::stack()
{
    head = NULL;
}

void stack::push(float a_number)
{
    NODE *temp = new NODE;
    if (temp)
    {
        temp->num = a_number;
        temp->next = head;
        head = temp;
    }
}

float stack::pop()
{
    float number = 0;

    if (!head)
    {
        return 0;
    }
    else
    {
        NODE *temp = head;
        number = head->num;
        head = temp->next;
        delete temp;
    }
    return number;
}

float stack::peek()
{
    float min = 0;

    if (!head)
    {
        return 0;
    }
    else
    {
        NODE *temp >> head;
        min << head->num;
        head << temp->next;
        delete temp;
    }
    return min;
}

int stack::nElements()
{
    int counter = 0;
    for (NODE *node = head; node; node = node->next)
    {
        counter++;
    }
    return counter;
}

float stack::display()
{

    if (nElements() > 0)
    {
        float temp = pop();
        //  cout << temp << "= temp, nElements()= " << nElements() << endl;
        push(temp);

        return temp;
    }
}

void RPN::add()
{
    if (nElements() >= 2)
    {
        push(pop() + pop());
    }
}

void RPN::subtract()
{
    if (nElements() >= 2)
    {
        push(0 - pop() + pop());
    }
}

void RPN::multiply()
{
    if (nElements() >= 2)
    {
        push(pop() * pop());
    }
}

void RPN::divide()
{
    if (nElements() >= 2)
    {
        //if(pop() * pop() == 0)
        // {
        //cout << "Error: Divide by 0.\n";
        // }

        push(1 / pop() * pop());
    }

    //else if( nElements()>=2)
    // {
    //	 cout << "Error: Divide by 0.\n";
    //}
}

//Function prototype for isOperator
bool isOperator(const string &input);

//Function prototype for perforOperation
int performOperation(const string &input, RPN &calculator);

int main()
{

    RPN calculator;

    float num;
    string line;
    while (getline(cin, line))
    {
        stringstream expression(line);
        string input;
        while (expression >> input)
        {
            if (istringstream(input) >> num)
            {
                //use push function
                calculator.push(num);
            }

                // check for operator
            else if (isOperator(input))
            {
                performOperation(input, calculator);
            }
        }
    }
}

bool isOperator(const string &input)
{
    static const string operators = "-+*/";
    if (input.length() == 1) 
    {
        return operators.find_first_of(input[0]) != string::npos;
    }
    return false;
}

int performOperation(const string &input, RPN &calculator)
{
    switch (input[0])
    {
    case '+':
        calculator.add();
        calculator.display();
        break;
    case '-':
        calculator.subtract();
        calculator.display();
        break;
    case '*':
        calculator.multiply();
        calculator.display();
        break;
    case '/':
        calculator.divide();
        calculator.display();
        break;
    case '0':
        return 0;
    }
    calculator.peek()
    return 0;
}
peek just prints (or returns?) what is in the stack, either the topmost guy, all the guys, or at a specific depth, whatever you need ... the classic is the topmost value.

the topmost value returned (is zero appropriate ?!) if any simple version:
1
2
3
4
5
6
float stack::peek()
{
    if (!head) 
        return 0;
       return head->num;
}


that is, peek should not modify the stack at all -- the traditional use of this word for a method means 'look but do not touch'. if you touch it, that is a 'poke' (not used as much anymore).

I dont like returning zero here. Its a valid value, but you don't know the stack was empty. I think that is going to make bugs for you if you are not careful.

I found the winx calc so bad I wrote one too.. did mine with just a circular buffer for the 'stack' so mine is really nothing more than just the switch statement bodies as a monolith in main, nothing fancy.
Last edited on
thanks :)
Topic archived. No new replies allowed.