Home > Back-end >  Why do I turn infix suffix result is wrong? O bosses give directions
Why do I turn infix suffix result is wrong? O bosses give directions

Time:09-28

#include
#include
#include
# define MAXSIZE 1024
# define TRUE 1
# define FALSE 0
typedef char ElemType;
Typedef struct {//order stack definition
ElemType data [MAXSIZE];
Int top;//the stack pointer
} SequenStack;
Typedef struct {//queue structure definition
ElemType data [MAXSIZE];
Int the front;
Int rear;
} SequenQueue;
//initialize the stack
SequenStack * InitStack ()
{
SequenStack * S=(SequenStack *) malloc (sizeof (SequenStack));
S-> Top=1;
Return S;
}
//whether the stack is empty
Int EmptyStack (SequenStack * S)
{
If (1==S - & gt; Top)
return TRUE;
The else
return FALSE;
}
//whether the stack with
Int FullStack (SequenStack * S)
{
if(S-> Top==MAXSIZE - 1)
return true;
The else
return FALSE;/* * * * * * * * to improve the following code/

}
//into the stack
Int Push SequenStack * S, ElemType (e)
{
//whether the stack with
If (FullStack (S))
{
Printf (" stack full!" );
return FALSE;
}

/* * * * perfect * * * * * * the following code/
S-> Top++;//stack pointer plus 1
S-> Data [S - & gt; top]=e;//
save values to the stack
return TRUE;
}
//the stack
Int Pop (SequenStack * S, ElemType * e)
{
* e=S - & gt; Data [S - & gt; top];/* * * * * * * * perfect * * * * * * * the following code/
S-> Top -;
return true;
}
//take the top element
ElemType GetTop (SequenStack * S)
{
The return of S - & gt; Data [S - & gt; top];
}
//initialize the queue
SequenQueue * InitQueue ()
{
SequenQueue * Q=NULL;

Q=(SequenQueue *) malloc (sizeof (SequenQueue));
Q - & gt; The front=Q - & gt; Rear=0;

return Q;
}
//whether the queue is empty
Int EmptyQueue (SequenQueue * Q)
{
If (Q - & gt; The front==Q - & gt; Rear)
return TRUE;
The else
return FALSE;
}
//to determine whether a queue with
Int FullQueue (SequenQueue * Q)
{
If ((Q - & gt; Rear MAXSIZE==+ 1) % Q - & gt; Front)/* * * * * perfect code/
return TRUE;
The else
return FALSE;
}
//into the team
Int the EnQueue (SequenQueue * Q, ElemType e)
{
If (FullQueue (Q))
return FALSE;

Q - & gt; Data [Q - & gt; rear]=e;
Q - & gt; Rear=(Q - & gt; Rear + 1) % MAXSIZE;

return TRUE;
}
//the team
Int to DeQueue (SequenQueue * Q, ElemType * e)
{
* e=Q - & gt; Data [Q - & gt; front];/* * * * * * * perfect code/
Q - & gt; front++;
return TRUE;
}
//take team header element
ElemType GetHead (SequenQueue * Q)
{
Return the Q - & gt; Data [Q - & gt; front];
}
//to the priority of operator
//priority order: (& lt; The +, - & lt; *,/
{int getPriority (char op)
Int priority;
If (op=='*' | | op=='/')
Priority=2;
If (op=='+' | | op=='-')
Priority=1;
If (op=='(')
Priority=0;
Return the priority;
}
/*
* output postfix expression
*/
Void PrintPostfix (SequenQueue * postfixQueue)
{
ElemType e;
while(! EmptyQueue (postfixQueue))
{
DeQueue (postfixQueue, & amp; e);
Printf (" % c ", e);
}

Putchar (" \ n ");
}
Int Trans (char * expr, SequenQueue * postfixQueue)
{
Char * p=expr.
ElemType e;

SequenStack * signStack=NULL;//symbol stack, it is the auxiliary stack

//initialize the symbol stack
SignStack=InitStack ();/* * * * perfect code * * * * * */

//expression from left to right scan
While (* p!='\ 0')
{
If (* p & gt;='0' & amp; & * p & lt;='9')//digital expression directly into the queue
{
The EnQueue (postfixQueue, * p);
}
Else if (' ('==* p)//left parenthesis, directly into the stack notation
{
Push (signStack, * p);/* * * * * * * * perfect code/
}
Else if (') '==* p)//if it is a right parenthesis, will all sign out in front of the left parenthesis stack into the expression of queue
{
Do {
Pop (signStack, & amp; e);/make/symbol for
If (e!='(')//will not (into the expression of queue
The EnQueue (postfixQueue, e);/* * * * * * * * * perfect code/
} while ((e!='(') & amp; & (! EmptyStack (signStack)));
}
The else//other symbols, according to the priority selection into the stack
{
If (EmptyStack (signStack))//the stack is empty, if the symbol is directly to the current operator into the stack
{
Push (signStack, * p);
}
The else
{

/* * * * * * * perfect code * * * * * * * */
While (getPriority (e) & gt; GetPriority (* p))//and compare the current operator priority on the top of the stack
{
Pop (signStack, & amp; e);/make/symbol for
The EnQueue (postfixQueue, e);//operator into the expression of queue
If (EmptyStack (signStack))//judgment symbol whether the stack is empty
{
break;
}
}

//the current operator into the stack
Push (signStack, * p);/* * * * * perfect code * * * * * */
}
}//end the else
P++;
}//end while

//all symbols stack into the expression of queue
while(! EmptyStack (signStack))
{
Pop (signStack, & amp; e);
The EnQueue (postfixQueue, e);
}
return TRUE;
}
Int main (int arg c, char * argv [])
{
SequenStack * S=InitStack ();//initialize the stack
SequenQueue * Q=InitQueue ();//initialize the stack
ElemType e;//element e

Push (S, 'A');
Push (S, 'B');
Push (S, "C");
Push (S, 'D');
Push (S, 'E');
Printf (" % c ", GetTop (S));
Pop (S, & amp; e);
Printf (" % c ", GetTop (S));
Pop (S, & amp; e);
Printf (" % c ", GetTop (S));
Pop (S, & amp; e);
Printf (" % c ", GetTop (S));
Pop (S, & amp; e);
Printf (" % c ", GetTop (S));
printf("\n");
The EnQueue (Q, 'A');
The EnQueue (Q), 'B');
The EnQueue (Q, 'C');
The EnQueue (Q, 'D');
The EnQueue (Q, 'E');
Printf (" % c ", GetHead (Q));
DeQueue (Q, & amp; e);
Printf (" % c ", GetHead (Q));
DeQueue (Q, & amp; e);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related