# contains "malloc.h"
# Define true 1
# Define false 0
# Define OK 1
# Definition error 0
# Definition is not feasible-1
# Define Overflow -2
Typedef int status;
# Define STACK_INIT_SIZE 100
# Define the stack increment 10
Typedef structure {
SElemType * base
SElemType * top
int stacksize
} SqStack
//Construct an empty stack
State InitStack(SqStack *S){
s-& gt; base =(selem type *)malloc(STACK _ INIT _ SIZE * sizeof(selem type));
If (! s-& gt; Base)
Exit (overflow);
s-& gt; top = S-& gt; Base;
s-& gt; stacksize = STACK _ INIT _ SIZE
Return ok;
}
//Judge whether it is an empty stack.
State stack properties (sqstacks)
if(S.top == S.base)
Return TRUE
other
Returns FALSE
}
//use e to return the top element of S.
Status GetTop(SqStack S,SElemType *e){
if(S.top == S.base)
Error returned;
* e = *(s . top- 1);
Return ok;
}
//Insert e as the new top element.
State push (SqStack *S, selemtype e) (
if((S-& gt; top-S-& gt; base)>= S-& gt; Stack size) {
s-& gt; base =(
selem type *)realloc(S-& gt; Base,
(S-& gt; stack size+stack increment)* sizeof(selem type)
);
If (! s-& gt; Base)
Exit (overflow);
s-& gt; top = S-& gt; base+S-& gt; stacksize
s-& gt; stack size+= stack increment;
}
*(S->; top)= e;
s-& gt; top++;
Return ok;
}
//Delete the top element of s and use e to return its value.
Status popup (SqStack *S, SElemType *e){
if(S-& gt; top = = S-& gt; Base)
Error returned;
s-& gt; top-;
* e = *(S->; Top);
Return ok;
}
//Call the function Visit () for each element of S in turn from the bottom of the stack to the top of the stack. Once it fails, the operation is invalid.
Status ListTraverse(SqStack S,Status (*visit)(SElemType)){
selem type * p;
p = s . base;
for(p = s . base; p & ltS.topp++)
(* visit) (* p);
Return ok;
}
//output element e
Status Output (Select Type e){
printf("%d ",e);
Return ok;
}
Code for expression evaluation:
/* Calculate the value of an integer expression
* expression must end with #
* Multiple numbers can appear in the expression,
* Spaces can appear in the expression.
* Operators include+,-,*,/,(,).
* The operation result can be a multi-bit integer and returned as an integer.
*/
Typedef int SElemType/* Type of element put on the stack */
# include & ltctype.h & gt
#include "stack_s.c "
/* Determines whether the input character is an operator.
*c indicates the input character.
*op array contains operators that can be recognized by the system.
*/
(char c,char op[]){
char * p;
p = op
while(*p! = '\0'){
if(c == *p)
Return TRUE
p++;
}
Returns FALSE
}
/* Compare the priorities of two operators.
* The operators to be compared are stored in A and B..
*'>' stands for a>b.
*'0' indicates that comparison is not possible.
*/
char precend(char a,char b){
int i,j;
char pre[][7]={
The priority between/* operators is made into a table */
{ ' & 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','=','0'},
{ ' & gt',' & gt',' & gt',' & gt',' 0 ',' & gt',' & gt'},
{ ' & lt',' & lt',' & lt',' & lt',' & lt','0','='}};
Switch (a)
Case'+':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;
}
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 pre[I][j];
}
/* Perform the actual operation.
*a and B respectively store two operands that operate in integer form.
*theta stores characters representing operators.
* The result is returned as an integer.
*/
int Operate(int a,char theta,int b){
int i,j,result
I = a;
j = b;
Switch (θ) {
case '+':result = I+j; Break;
case '-':result = I-j; Break;
case ' * ':result = I * j; Break;
case '/':result = I/j; Break;
}
Return the result;
}
/* Get the next integer or operator from the input buffer and bring it back to the tonic function by n..
* The return value of 1 indicates that the operator is obtained.
* A return value of 0 indicates that the shaping operand has been obtained.
*/
int getNext(int *n){
char c;
* n = 0;
while((c = getchar())= = ' '); /* Skip one or more spaces */
If (! Isdig (c)) {/* If the character is not a number, it can only be an operator */
* n = c;
Returns1;
}
Do {/* can execute this statement, indicating that this character is a number and it is a continuous number */
* n = * n * 10+(c-' 0 '); /* Convert consecutive numeric characters into corresponding integers */
c = getchar();
} while(is digit(c)); /* If the next character is a number, please enter the next cycle */
ungetc(c,stdin); /* The newly read character is not a number, but an operator. In order not to affect the next reading, put the characters back into the input buffer */
Returns 0;
}
int EvaluateExpression(){
int n;
Int mark;
int c;
char x,theta
int a,b;
char OP[]= "+-*/()# ";
SqStack OPTR
SqStack OPND
init stack(& amp; OPTR);
Push (& ampOPTR,' #');
init stack(& amp; OPND);
flag = get next(& amp; c);
GetTop(OPTR & amp; x);
And (c! ='#' || x! = '#')
{
If (flag == 0)
{
Push (& popnd, c);
flag = get next(& amp; c);
} Otherwise,
{
GetTop(OPTR & amp; x);
Switch (before (x, c))
{
Case' & lt':// The top element of the stack has low priority.
Push (& ampOPTR, c);
flag = get next(& amp; c);
Break;
Case' ='://Unbrack and accept the next character.
Pop (& optr &; x);
flag = get next(& amp; c);
Break;
Case ">"://stack, and stack the operation results.
Pop (& optr &; θ);
Pop (& b);
Pop (& a);
Push (& popnd, Operate(a, theta, b)););
Break;
}
}
GetTop(OPTR & amp; x);
}
GetTop(OPND & amp; c);
Return to c;
}
void main(){
int c;
Printf ("Please enter an expression:");
c = evaluate expression();
printf("Result=%d\n ",c);
getch();
}