Home > other >  Urgent ask god to help me take a look at the code!!!!!! Themselves did not, it is to use c language
Urgent ask god to help me take a look at the code!!!!!! Themselves did not, it is to use c language

Time:09-24

# include
# include
# include
# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
# define TRUE 1
# define FALSE 0
# define OVERFLOW - 2
Typedef char ElemType;
Typedef int the Status;
Typedef struct {
ElemType * base;
ElemType * top;
Int stacksize;
} Sqstack;
The Status StackEmpty (Sqstack S) {
If (S.t op==S.b ase) return TRUE;
The else return FALSE;
}
The Status Initstack (Sqstack & amp; S) {
S.b ase=(ElemType *) malloc (STACK_INIT_SIZE * sizeof (ElemType));
if(! S.b ase) exit (OVERFLOW);
S.t op=S.b ase;
S.s tacksize=STACK_INIT_SIZE;
}
Void Push (Sqstack & amp; S, ElemType e) {//pressure stack operations
If ((S.t op - S.b ase) & gt;=S.s tacksize) {
S.b ase=(ElemType *) realloc (S.b ase, (S.s tacksize + STACKINCREMENT) * sizeof (ElemType));//when the stack is greater than the length of the storage space redistribution
if(! S.b ase) exit (0);//redistribution failure
S.t op=S.b ase + S.s tacksize;//reset the stack and the stack size information
S.s tacksize +=STACKINCREMENT;
}
* S.t op++=e;
}
Void Pop (Sqstack & amp; S, ElemType & amp; E) {//stack operation
If (StackEmpty (S)) exit (0);//empty stack processing
E=* - S.t op;
}
ElemType GetTop (Sqstack S) {//if the stack is not empty, the stack elements in e, return OK, otherwise, the exit
ElemType e;
If (StackEmpty (S)) exit (0);//when the stack is empty out
E=* (S.t op - 1);
Return e;

}
The Status operatorstack ElemType (c) {//to determine whether character operand
Int I=0;
Char CTR []="+ - */";
While (CTR) [I] {
If (CTR [I]==c) return TRUE;
i++;
}
return FALSE;
}
The Status compare (char char s1, s2) {//compare two operator priority
Int I=0, j=0;
The switch (s1) {
A case of '+' : I=1;
Case '-' : I=1;
Case '*' : I=2;
Case '/' : I=2;
Case '^' : I=3;
}
The switch (s2) {
Case '+' : j=1;
Case '-' : j=1;
Case '*' : j=2;
Case '/' : j=2;
Case '^' : I=3;
}
If (i> J)=return TRUE;
The else return FALSE;
}
The Status calculation (char a, b char, char e) {//computing
If (a> 47) a=atoi (& amp; A);
If (b> 47) b=atoi (& amp; B);
The switch (e) {
Case: '+' return (a + b); break;
Case '-' : return (a - b); break;
Case: '*' return (a * b); break;
Case '/' : return (a/b); break;
Case: '^' return (a ^ b); break;
}
}
ElemType convertpoland () {//here before and after the expression has a # symbol
Sqstack S, Q;
char c;
ElemType e, e1, e2 and e3.
Initstack (S);
Initstack (Q);
Push (Q, '#');
Printf (" please enter the expression and ends with a # \ n ");
C=getchar ();
While (c!='#' | | GetTop (Q)!='#') {
if(! Operatorstack (c)) {//character STR [I] is not the operator, that is, it is the operand
Push (Q, c);
C=getchar ();
}
The else {
E=GetTop (S);//remove the stack elements
If (compare (e, c)) {//when the top symbol is greater than the current priority symbol, the stack
Pop (Q, e1);//remove the first two elements in the stack and the operation number operations
Pop (Q, e2);
E3=calculation (e1, e2, e);
Push (Q, e3);//the result in the operand stack Q
}
The else {
Push (S, c);//will be credited to the stack operator
C=getchar ();
}
}
}
While (GetTop (Q)!='#') {//take out the final result
Return GetTop (Q);
}
}
The main () {
int result;
Result=(int) convertpoland ();
Printf (" result=% d ", result);
}
  • Related