Home > Back-end >  Stack realization arithmetic for help
Stack realization arithmetic for help

Time:11-01

Bosses, I am now doing a arithmetic of evaluation procedure to implement in the stack, but now no matter what I lose expression in, come out of the answers are random Numbers, can you help me have a look at where there is a problem, how to improve it
 # include & lt; Stdio. H> 
#include
# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
# define OK 1
# define the ERROR 0
# define OVERFLOW - 2

Typedef char SElemType;
Typedef int the Status;
Typedef char OperandType;//operation type
Typedef struct {
SElemType *base;
SElemType * top;
Int stacksize;
}SqStack;

Status InitStack (SqStack * S)//constructs an empty stack
{
S - & gt; The base=(SElemType *) malloc (STACK_INIT_SIZE * sizeof (SElemType));
if(! S - & gt; Base) exit (OVERFLOW);
S - & gt; Top=S - & gt; The base;
S - & gt; Stacksize=STACKINCREMENT;
Return OK;
}

The Status of Push (SqStack * S, SElemType e)//into the stack
{
If (S - & gt; Top - S - & gt; Base & gt;=S - & gt; Stacksize)
{
S - & gt; The base=(SElemType *) realloc (S - & gt; Base, (S - & gt; Stacksize + STACKINCREMENT) * sizeof (SElemType));
if(! S - & gt; Base) exit (OVERFLOW);
S - & gt; Top=S - & gt; Base + S - & gt; Stacksize;
S - & gt; Stacksize +=STACKINCREMENT;
}
* S - & gt; Top++=e;
Return OK;
}

Status of Pop (SqStack * S, SElemType * e)//the stack
{
If (S - & gt; Top==S - & gt; Base) return the ERROR;
=* * e - S - & gt; Top;
Return OK;
}

SElemType GetTop (SqStack * S, SElemType * e)//get the top element
{
If (S - & gt; Top==S - & gt; Base)
{
Return the ERROR;
}
* e=* (S - & gt; Top - 1);
Return OK;
}

OperandType Easyoperate (OperandType a, OperandType b, OperandType theta)//calculation simple operations
{
OperandType answer;
The switch (theta)
{
Case: '+'
Answer=a + b;
Return answer; break;
In case the '-' :
Answer=a - b;
Return answer; break;
Case: '*'
Answer=a * b;
Return answer; break;
Case '/' :
Answer=a/b;
Return answer; break;
}
}

Char Compare (char a, char b)//judgment operator priority
{
int i,j;
Char the prior [7] [7]=//show the operator precedence relation of two dimensional array
{
/* '+' a '-' a '*' '/', '(')' '#' */
//* '+' * '& gt; ', '& gt; ', '& lt; ', '& lt; ', '& lt; ', '& gt; ', '& gt; '
//* '-' * '& gt; ', '& gt; ', '& lt; ', '& lt; ', '& lt; ', '& gt; ', '& gt; '
//* '*' * '& gt; ', '& gt; ', '& gt; ', '& gt; ', '& lt; ', '& gt; ', '& gt; '
/* */'/' '& gt; ', '& gt; ', '& gt; ', '& gt; ', '& lt; ', '& gt; ', '& gt; '
//* '(' *' & lt; ', '& lt; ', '& lt; ', '& lt; ', '& lt; 'and'=', ' ', '
//* ') '*' & gt; ', '& gt; ', '& gt; ', '& gt; ', 'a', '& gt; ', '& gt; '
/* */' '#' & lt; ', '& lt; ', '& lt; ', '& lt; ', '& lt; ', ' 'and'=',
};

The switch (a)
{
A case of '+' : I=0; break;
Case '-' : I=1; break;
Case '*' : I=2; break;
Case '/' : I=3; break;
Case '(' : I=4; break;
Case ') : I=5; break;
Case '#' : I=6; break;
}
The switch (b)
{
Case '+' : j=0; break;
Case '-' : j=1; break;
Case '*' : j=2; break;
Case '/' : j=3; break;
Case '(' : j=4; break;
Case ') : j=5; break;
Case '#' : j=6; break;
}

Return the prior [I] [j];
}

Bool (OperandType c)//In judgment is digital or operator
{
The switch (c)
{
Case: '+' return true;
Case '-' : return true;
Case: '*' return true;
Case '/' : return true;
Case '(' : return true;
Case ') : return true;
Case '#' : return true;
Default: return false.
}
}

OperandType Operate ()
{
SqStack OPTR OPND;
OperandType a, b, last, theta, num;
Char OP [6]={' + ', '-', '*', '/', '(',') '};
InitStack (& amp; OPTR);
Push (& amp; OPTR, '#');
InitStack (& amp; OPND);

Printf (" input expression: ");
Char equation;
Equation=getchar ();
GetTop (& amp; OPTR, & amp; The last);
While (equation!='#' | | last!='#')//input formula, end with #
{
if(! In (equation))//if not operator, the char types of Numbers to int and then into the OPND stack
{
The int data [10].
int i=0;
while(! In (equation))
{
Data [I]=equation - '0';
i++;
Equation=getchar ();
}
for(int j=0; j{
Num=10 + num * data [j];
}
Push (& amp; OPND, num);
}
The else
{
The switch (Compare (last, equation))
{
Case '& lt; ':
Push (& amp; OPTR equation);
Equation=getchar ();
break;
Case: '='
Pop (& amp; OPTR, & amp; The last);
Equation=getchar ();
break;
Case '& gt; ':
Pop (& amp; OPTR, & amp; Theta);
Pop (& amp; OPND, & amp; B);
Pop (& amp; OPND, & amp; A);
Push (& amp; OPND Easyoperate (a, b, theta));
break;
}
}
GetTop (& amp; OPTR, & amp; The last);
}
GetTop (& amp; OPND, & amp; The last);
Return the last;
}

Int main ()
{
Printf (" % d ", Operate ());
return 0;
}
  • Related