Home > Back-end >  Data structure, arithmetic expression, the program run result is a little problem, logically don
Data structure, arithmetic expression, the program run result is a little problem, logically don

Time:12-31

 # include & lt; stdio.h> 
#include
# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
# define the ERROR - 1
Typedef struct {//define the sequential storage structure of stack
char *base;
char *top;
int stacksize;
} SqStack;
Typedef struct {//define the sequential storage structure of stack
Int * base;
Int * top;
int stacksize;
} SqStackNum;

Void InitStack (SqStack * s)//build an empty stack
{
S - & gt; The base=(char *) malloc (STACK_INIT_SIZE * sizeof (char));
if(! S - & gt; Base) exit (1);
S - & gt; Top=s - & gt; The base;
S - & gt; Stacksize=STACK_INIT_SIZE;
}
Void InitStackNum SqStackNum * (s)//build an empty stack
{
S - & gt; The base=(int *) malloc (STACK_INIT_SIZE * sizeof (int));
if(! S - & gt; Base) exit (1);
S - & gt; Top=s - & gt; The base;
S - & gt; Stacksize=STACK_INIT_SIZE;
}
Int GetTop (SqStack s, char * e)
{
If (s.t op==s.b ase) return the ERROR;
* e=* (s.t op - 1);
return 1;
}
Int GetTopNum (SqStackNum s, int * e)
{
If (s.t op==s.b ase) return the ERROR;
* e=* (s.t op - 1);
return 1;
}

Void push SqStack * s, char (e)
{
If ((s - & gt; Top - s - & gt; Base) & gt;=s - & gt; Stacksize)//stack is full, redistribution of storage
{
S - & gt; The base=(char *) realloc (s - & gt; Base, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof (char));
if(! S - & gt; Base) exit (1);
S - & gt; Top=s - & gt; Base + s - & gt; Stacksize;
S - & gt; Stacksize +=STACKINCREMENT;
}
* s - & gt; Top++=e;
}
Void pushNum SqStackNum * s, int (e)
{
If ((s - & gt; Top - s - & gt; Base) & gt;=s - & gt; Stacksize)//stack is full, redistribution of storage
{
S - & gt; The base=(int *) realloc (s - & gt; Base, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof (int));
if(! S - & gt; Base) exit (1);
S - & gt; Top=s - & gt; Base + s - & gt; Stacksize;
S - & gt; Stacksize +=STACKINCREMENT;
}
* s - & gt; Top++=e;
}

Pop (SqStack * s is an int, char * e)
{
If (s - & gt; Top==s - & gt; Base) return 0;
=* * e - s - & gt; Top;
return 1;
}
Int popNum (SqStackNum * s, int * e)
{
If (s - & gt; Top==s - & gt; Base) return 0;
=* * e - s - & gt; Top;
return 1;
}

Int StackEmpty (SqStack * s)
{
If (s - & gt; Top==s - & gt; Base) return 1;//the stack is empty, the return value is 1
The else return 0;
}

Int precede (char char a, c, e, e)
{
If ((c=='(' & amp; & E==') ') | | (c=='#' & amp; & E=='#')) return 0;
Else if ((c=='+' & amp; & E=='(') | | (c==' - '& amp; & E=='(') | | (c==' * '& amp; & E=='(') | | (c=='/' & amp; & E=='(') | | (c==' + '& amp; & E=='*') | | (c=='-' & amp; & E=='*') | | (c=='+' & amp; & E=='/') | | (c=='-' & amp; & E=='/')) return 1;
The else return 1;
}

Int operate (int a, char e, int b)
{
The switch (e)
{
Case: '+' return (a + b);
Case '-' : return (a - b);
Case: '*' return (a * b);
Case '/' : return (a/b);
}
}
Int EvaluateExpression ()
{
SqStack OPTR; SqStackNum OPND;
InitStack (& amp; OPTR);
InitStackNum (& amp; OPND);
Push (& amp; OPTR, '#');
Int a, b;
Char e, c;
C=getchar ();
While (c!='#' & amp; & ! StackEmpty (& amp; OPTR))
{
If (' 0 '& lt;=c& & C<='9')
{
PushNum (& amp; OPND, (int), c);//for the operand, directly into the stack
C=getchar ();
}
The else
{
GetTop (OPTR, & amp; e);
The switch (precede (e, c))
{
Case 1: push (& amp; OPTR, c);//stack elements low priority
C=getchar ();
break;
Case 0: pop (& amp; OPTR, & amp; e);//priority, in view of the () and # #
C=getchar ();
break;
Case 1: pop (& amp; OPTR, & amp; e); PopNum (& amp; OPND, & amp; a);
PopNum (& amp; OPND, & amp; B);
PushNum (& amp; OPND to operate ((int), a, e, b) (int));
C=getchar ();
break;
}

}
}
GetTopNum (OPND, & amp; a);
Return (int) a;
}

Int main ()
{
Printf (" please enter the arithmetic expression (ending with #, parentheses input in English) : \ n ");
int a;
A=EvaluateExpression ();
Printf (" results for: \ n ");
Printf (" % c ", a);
}


The last run results always seem to value of the last number in each expression, didn't see which write wrong
Such as online!!!!! Is urgent! Thank you all!!!!!
  • Related