Testing out my Reverse Polish stack...

Hi all. Just trying to get through this addition portion before moving on with the rest. Can't seem to figure out this pop,pop,push sequence. I know I'm heading in the right direction but can't nail down the exact coding. Any tips?

#include <iostream>


using namespace std;

class polishnode
{

public:

int numinput;
class polishnode *next;

polishnode(int operand, polishnode * ptr)
{
numinput = operand;
next = ptr;
}

};

class polishcalc
{

private:
polishnode *top;

public:
polishcalc()
{
top = 0;
}
bool isEmpty()
{
return top == 0;
}

int expressionentry(char * userinput)
{
int operand;

polishnode * temp;

char * pch;

pch = strtok (userinput, " ");
while(pch != NULL)
{
if (pch = "+")
{
int operandone;
int operandtwo;
int ans;


if (!isEmpty()) //pop first numerical value
operandone = top->numinput;
delete top;
top->next;

if (!isEmpty()) //pop second numerical value
operandtwo = top->numinput;
delete top;
top->next;

ans = operandone+operandtwo;

top->next = new polishnode(ans,top); //push answer back onto stack
}
else
top = new polishnode(operand,top); //push numerical value until an operator is read

}
return 0;

}

void print(char * userinput)
{
for(polishnode *temp = top; temp != 0; temp = temp->next)
{
cout<< " " << temp->numinput;
}
cout << endl;
}


};

int main ()
{
polishcalc input;
char userinput[99];
char * pch;
cout << "Please enter a reverse polish expression. \n";
cin.getline (userinput,99);

input.expressionentry(userinput);

input.print(userinput);

system("PAUSE");
}
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
#include <iostream>

using namespace std;

class polishnode
{

public: 

int numinput; 
class polishnode *next;

polishnode(int operand, polishnode * ptr)
{
numinput = operand;
next = ptr;
}

};

class polishcalc 
{

private: 
polishnode *top;

public: 
polishcalc()
{
top = 0;
}
bool isEmpty()
{
return top == 0;
}

int expressionentry(char * userinput)
{ 
int operand;

polishnode * temp;

char * pch;

pch = strtok (userinput, " ");
while(pch != NULL)
{
if (pch = "+")
{
int operandone;
int operandtwo;
int ans;


if (!isEmpty()) //pop first numerical value
operandone = top->numinput;
delete top;
top->next;

if (!isEmpty()) //pop second numerical value
operandtwo = top->numinput;
delete top;
top->next;

ans = operandone+operandtwo;

top->next = new polishnode(ans,top); //push answer back onto stack
}
else
top = new polishnode(operand,top); //push numerical value until an operator is read

}
return 0;

}

void print(char * userinput)
{
for(polishnode *temp = top; temp != 0; temp = temp->next)
{
cout<< " " << temp->numinput;
}
cout << endl;
}


};

int main ()
{ 
polishcalc input;
char userinput[99];
char * pch;
cout << "Please enter a reverse polish expression. \n";
cin.getline (userinput,99);

input.expressionentry(userinput);

input.print(userinput);

system("PAUSE");
}


It's the same code, just for easier reading
Last edited on
Topic archived. No new replies allowed.