Home > Back-end >  Why reverse polish expression values, seek out the value of the error, (should be the main function
Why reverse polish expression values, seek out the value of the error, (should be the main function

Time:03-22

#include
# include
#include
#include
using namespace std;
Struct Node
{
int data;
Node * next;
};
The class Stack
{
Private:
Node * top;
Public:
The Stack ()
{
Top=NULL;
}
~ the Stack ();
Void Push (int Elem);//into the stack
Int the Pop ();//out of the stack, the return value type
Int Top ();//return stack element
};
Stack: : ~ Stack ()//destructors, empty Stack
{
Node * p;
While (top)
{
P=top - & gt; next;
Delete the top;
Top=p;
}
cout <"Stack is empty" & lt; }
Int Stack: : Pop ()
{
If (top==NULL)
{
cout <"The stack is empty" & lt; return -1;
}
The else
{
Node * p;
int a;
A=top - & gt; The data;
P=top;
Top=top - & gt; next;
The delete p;
return a;
}
}
Void Stack: : Push (int Elem)//into the Stack,
{
Node * s;
S=new Node;
S - & gt; Data=https://bbs.csdn.net/topics/Elem;
S - & gt; Next=top;
Top=s;
}
Int Stack: : Top ()
{
Return the top - & gt; The data;
}



Int main ()//main function
{
Stack st.
Char string [10].
Int I=0;
cin> The string [0];
While (string [I]!='#')
{
If (isdigit (string [I]))
{
St. Push (string [I]);
}
The else
{
Int a, b, the result;
The switch (string [I])
{
Case: '+'
A=st. Pop ();
B=st. Pop ();
Result=a + b;
St. Push (result);
break;
In case the '-' :
A=st. Pop ();
B=st. Pop ();//pop up first to the second operand
Result=b - a;
St. Push (result);
break;
Case: '*'
A=st. Pop ();
B=st. Pop ();
Result=a * b;
St. Push (result);
break;
Case '/' :
A=st. Pop ();
B=st. Pop ();
If (a==0)
{
cout <"Error" & lt; }
The else
{
Result=b/a;
St. Push (result);
}


}
}
i++;
cin> String [I];
}
cout return 0;
}

CodePudding user response:

The input expression without braces

CodePudding user response:

I input the 3 * 2 # {end with # #)
The results should be 6, but the console output is 2550
  • Related