This example for you to share the use of C + + chain stack implementation of the expression evaluation of the specific code for your reference, the specific content as follows
#include <iostream.h> typedef int Status;
typedef char Cstack;
#define OK 1 #define ERROR 0 typedef struct STACKNODE {cstack data;
struct Stacknode *next;
}stacknode,*linkstack;
Status Initstack (Linkstack &s) {s=null;
return OK;
Status Push (Linkstack &s,cstack e) {Stacknode *p;
P=new Stacknode;
p->data=e;
p->next=s;
S=p;
return OK;
Status POPs (Linkstack &s,cstack &e) {Stacknode *p;
if (s==null) return ERROR;
e=s->data;
P=s;
s=s->next;
Delete p;
return OK;
} cstack GetTop (Linkstack S) {if (s!=null) return s->data;}
Status in (Cstack ch) {cin>>ch;
if (ch== ' + ') return OK;
else if (ch== '-') return OK;
else if (ch== ' * ') return OK;
else if (ch== '/') return OK;
else if (ch== ' # ') return OK;
else return ERROR; } cstack precede (cstack t1,cstack T2) {switch (t1) {case ' + ': switch (T2) {case ' + ': return ' > ';
;
Case '-': return ' > '; CaSe ' * ': return ' < ';
Case '/': return ' < ';
Case ' (': Return ' < ';
Case '] ': return ' > ';
Case ' # ': Return ' > ';
} break;
Case '-': switch (T2) {case ' + ': return ' > ';
Case '-': return ' > ';
Case ' * ': return ' < ';
Case '/': return ' < ';
Case ' (': Return ' < ';
Case '] ': return ' > ';
Case ' # ': Return ' > ';
} break;
Case ' * ': switch (T2) {case ' + ': return ' > ';
Case '-': return ' > ';
Case ' * ': return ' > ';
Case '/': return ' > ';
Case ' (': Return ' < ';
Case '] ': return ' > ';
Case ' # ': Return ' > ';
} break;
Case '/': switch (T2) {case ' + ': return ' > ';
Case '-': return ' > ';
Case ' * ': return ' > ';
Case '/': return ' > ';
Case ' (': Return ' < '; Case '] ': return ' > '; bReak;
Case ' # ': Return ' > ';
} break;
Case ' (': switch (T2) {case ' + ': return ' < ';
Case '-': return ' < ';
Case ' * ': return ' < ';
Case '/': return ' < ';
Case ' (': Return ' < ';
Case '] ': return ' = ';
Case ' # ': Return ' > ';
} break;
Case '] ': switch (T2) {case ' + ': return ' > ';
Case '-': return ' > ';
Case ' * ': return ' > ';
Case '/': return ' > ';
Case ' (': return ' = ';
Case '] ': return ' > ';
Case ' # ': Return ' > ';
} break;
Case ' # ': return ' = ';
Break
} cstack Operator (cstack t1,cstack t2,cstack T3) {t1=t1-48;
t3=t3=48;
int C;
Switch (T2) {case ' + ': c=t1+t2+48;
return C;
Break
Case '-': c=t1-t2+48;
return C;
Break
Case ' * ': c=t1*t2+48;
return C;
Break
Case '/': c=t1/t2+48;
return C;
Break }
}
void Main () {Linkstack optr,opan;
Cstack Cha1,cha2,x,cha,thea;
Initstack (OPTR);
Initstack (Opan);
Push (Optr, ' # ');
cout<< "The middle value of the input expression and the final result is limited to single-digit digits between 0~9 and end with #" <<endl; while (cha!= ' # ' | |
GetTop (optr)!= ' # ') {cin>>cha; if (!
In (CHA)) Push (Opan,cha);
else switch (precede (GetTop (optr), cha) {case ' < ': Push (Optr,cha);
cin>>cha;
Break
Case ' > ': Pop (Optr,thea);
Pop (OPAN,CHA1);
Pop (OPAN,CHA2);
Push (Opan, (CHA1,THEA,CHA2));
Break
Case ' = ': Pop (optr,x);
Break
} cout<<gettop (Opan) <<endl;
Return }
The above is the entire content of this article, I hope that you learn C + + program to help.