Home > Back-end >  Turn order stack implementation infix postfix expression
Turn order stack implementation infix postfix expression

Time:10-22

#include
using namespace std;
# include "SeqStack19D Wang Yilian. H"
Template//constructor, initialization of the stack
SeqStack : : SeqStack ()
{
Int top=1;
} template
SeqStack : : ~ SeqStack () {} template//into the stack,
Void SeqStack : : Push (DataType x)
{
If (top==StackSize - 1) throw "overflow";
Data [+ + top]=x;
}

Template
DataType SeqStack : : Pop ()
{
DataType x;
If (top==1) throw "underflow";
X=data [top --];
return x;
}

Template//take the top element
DataType SeqStack : : GetTop ()
{
If (top!=1)
The return data [top];
}

Template
Void SeqStack : : Change ()
{
SeqStack C;
char ch;
Char outch;
Cout<" Please enter the infix expression, and end with # "& lt; Cin> Ch;
while(ch!='#')
{the while (ch>='0' & amp; & Ch<='9' | | ch>='a' & amp; & Ch<='z' | | ch>='A' & amp; & Ch<='Z')//direct output Numbers and characters {cout Ch; }
//meet + - if a character (ch=='+' | | ch=='-')//the lowest priority +, - {if (top==1) {C.P ush (ch);//when empty directly into the stack} else {//stack is not empty do {outch=C.P op (); If (outch=='(') {C.P ush (outch); } else {cout//in) else if (ch==') ') {//met), and output the stack elements in turn until meet (and the element deletion outch=C.P op (); While (outch!='(') {cout} else {cout} cin> Ch; }
//in the end, if the stack is not empty, in turn, the output stack element
While (C.t op!=1) {outch=C.P op (); Cout}
} the output error, bosses to give directions

CodePudding user response:

With STD stack code changes, LZ refer to a different place yourself

 using namespace STD. 

Int main () {
Stack C;//here to STD stack, but does not affect the original overall train of thought
char ch;
Cout<" Please enter the infix expression, and end with # "& lt; Cin> Ch;
while(ch!='#') {
While ((ch>='0' & amp; & Ch<='9') | | (ch>='a' & amp; & Ch<='z') | | (ch>='A' & amp; & Ch<)='Z')//in digital and character directly output
{cout Ch; }
//have the lowest priority +, -
If (ch=='+' | | ch=='-') {//when empty directly into the stack
while(! C.e mpty () & amp; & (C.t op ()!='(')) {//modify here
CoutC.p op ();
}
C.p ush (ch);
}
//met), output the stack elements in turn until met (and delete elements (
Else if (ch==') ') {
While (C.t op ()!='(') {
CoutC.p op ();
}
C.p op ();//here also changed the
}
Else if (ch=='(') {
C.p ush (ch);
}
//*,/, directly into the highest priority stack
Else if (ch=='*' | | ch=='/') {//here alone to separate brackets because, at the same level, but want to consider to order
//such as a/b * c suffix ab and ABC/c * */is not equivalent
///* this comment let go look at the effect
while(! C.e mpty () & amp; & (C.t op ()!='(') & amp; & (C.t op ()!='+') & amp; & (C.t op ()!='-')) {//here also changed (added at the same level of precedence)
CoutC.p op ();
}
//*/
C.p ush (ch);
}
//traversal operations meet #
Else if (ch=='#') {break; }
The else {coutCin> Ch;
}
//in the end, if the stack is not empty, in turn, the output stack element
while(! C.e mpty ()) {coutreturn 0;
}


CodePudding user response:

Please can take you to an empty function to
And the constructor in the stack

CodePudding user response:

reference 1st floor qybao response:
with STD stack code changes, LZ refer to a different place yourself

 using namespace STD. 

Int main () {
Stack C;//here to STD stack, but does not affect the original overall train of thought
char ch;
Cout<" Please enter the infix expression, and end with # "& lt; Cin> Ch;
while(ch!='#') {
While ((ch>='0' & amp; & Ch<='9') | | (ch>='a' & amp; & Ch<='z') | | (ch>='A' & amp; & Ch<)='Z')//in digital and character directly output
{cout Ch; }
//have the lowest priority +, -
If (ch=='+' | | ch=='-') {//when empty directly into the stack
while(! C.e mpty () & amp; & (C.t op ()!='(')) {//modify here
CoutC.p op ();
}
C.p ush (ch);
}
//met), output the stack elements in turn until met (and delete elements (
Else if (ch==') ') {
While (C.t op ()!='(') {
CoutC.p op ();
}
C.p op ();//here also changed the
}
Else if (ch=='(') {
C.p ush (ch);
}
//*,/, directly into the highest priority stack
Else if (ch=='*' | | ch=='/') {//here alone to separate brackets because, at the same level, but want to consider to order
//such as a/b * c suffix ab and ABC/c * */is not equivalent
///* this comment let go look at the effect
while(! C.e mpty () & amp; & (C.t op ()!='(') & amp; & (C.t op ()!='+') & amp; & (C.t op ()!='-')) {//here also changed (added at the same level of precedence)
CoutC.p op ();
}
//*/
C.p ush (ch);
}
//traversal operations meet #
Else if (ch=='#') {break; }
The else {coutCin> Ch;
}
//in the end, if the stack is not empty, in turn, the output stack element
while(! C.e mpty ()) {coutreturn 0;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related