《数据结构代码.docx》由会员分享,可在线阅读,更多相关《数据结构代码.docx(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#include#include#include typedef struct(char op;int level;JOpNode;typedef struct(OpNode op100;int top; stack;void init(stack *st)(st-top=0;)OpNode pop(stack *a)if (a-top=0)return a-op(a-top);)void push(stack *a,OpNode op)(a-op(a-top)+=op;)OpNode top(stack *a)(if (a-top=0)return a-op(a-top)-l;)typede
2、f struct(double num100;int top; numstack;void init2(numstack *st)st-top=0;double pop2(numstack *a)if (a-top=0)(return(-l);)a-top;return a-numa-top;)void push2(numstack a,double num)(a-num(a-top)+=num;)void main()(void change (char str,char exp);double CalResult(char exp);double Directcalresult(char
3、str);char str100,exp100;printf(算术表达式为:n);gets(str);change(str,exp);gets(exp);printf(后缀表达式为:sn”,exp);printf(运算结果为:%fnH ,CalResul t(exp);printf(直接运算的结果为:%fn ,Directcalresult(str);void change (char str,char ch) (int i=0;int k=0;char c;stack st;OpNode op;OpNode ops;init(&st);c=stri+;while (c!=0)if (c=O&
4、c=0&c0) op=top(&st);else break;)pop(&st);if(c=Tc=)(op.op=c;op.level=l;if (st.top=0)(push(&st,op);/)else(ops=top(&st);while (ops.level=op.level)(ops=pop(&st);chk+=ops.op;if (st.top0) ops=top(&st);elsebreak;push(&st,op);)if(c=f|c=7,|c=,%)(op.op=c;op.level=2;if (st.top=0) (push(&st,op);) else ( ops=top
5、(&st);while (ops.level=op.level) (ops=pop(&st); chk+=ops.op;if (st.top0) ops=top(&st);else break;) push(&st,op);)c=strfi+;)while(st.top!=0)(ops=pop(&st); chk+=ops.op;) chk=O;double CalResult(char exp) (char c;numstack numst;double dl,d2,dr;int k=0;int i=0;char *s;char transflOO;init2 (&numst);c=expk
6、+;while (c!=0)(if(c=-+|c=-,|c=-*|c=7|c=-%1)switch(c)(case:d2=pop2(&numst);d 1 =pop2(&numst);dr=dl+d2;push2(&numst,dr); break;case:d2=pop2(&numst);dl=pop2(&numst);dr=dl-d2;push2(&numst,dr);break;case :d2=pop2(&numst);d 1 =pop2(&numst);dr=dl*d2;push2(&numst,dr); break;case 71:d2=pop2(&numst);d 1 =pop2
7、(&numst);dr=dl/d2;push2(&numst,dr);break;case:d2=pop2(&numst);dl=pop2(&numst);dr=(double)(int)d 1 %(int)d2);push2(&numst,dr);break;if(c=0&c=0&c=9|c =二(transi+=c;c=expk+;)transi+=,O,;i=0;s=trans;dl=atof(s);push2(&numst,dl);)c=expk+;)return pop2(&numst);return result;double Directcalresult(char str)(s
8、tack ms;numstack mns;/double calculate(double odl,double od2,OpNode op);int index=0;int len=strlen(str);char c;char trans100|;/int i=0;char * s;double d;OpNode tempn;OpNode templn;double oda,odb,odr;double result;init (&ms);init2(&mns);while(index=O&c=,0&c=tempn.level) ( templn=pop(&ms);odb=pop2(&mn
9、s);oda=pop2(&mns);odr=calculate(oda,odb,templn); push2(&mns,odr); if(ms.top0) (templn=top(&ms);) else break;) push(&ms,tempn);if(c=-*|c=7|c=-%)tempn.level=2;lempn.op = c;if(ms.top=0)(push(&ms,tempn);)else(templn=top(&ms);while (templn.level=tempn.level)(templn=pop(&ms);odb=pop2(&mns);oda=pop2(&mns);
10、odr=calculate(oda,odb,templn);push2(&mns,odr);if(ms.top0)(templn=top(&ms);)elsebreak;templn=top(&ms);push(&ms,tempn);)ifB()(tempn.level=-l;tempn.op=c;push(&ms,tempn);)if(c=f)(while(tempn.op!=()(templn=pop(&ms);odb=pop2(&mns);oda=pop2(&mns);odr=calculate(oda,odb,templn);push2(&mns,odr);if (ms.top0)te
11、mpn=top(&ms);elsebreak;)pop(&ms);)tempn=top(&ms);while(l)(templn=pop(&ms);odb=pop2(&mns);oda=pop2(&mns);odr=calculate(oda,odb,templn);push2(&mns,odr);if (ms.top0)tempn=top(&ms);elsebreak;)result =pop2(&mns);return result;)double calculate(double odl,double od2,OpNode op)switch(op.op)(case: return odl+od2;case: return odl-od2;case: return odl*od2;case 7: return odl/od2;case ,Q/o : return (double)(int)od 1 %(int)od2);return 0;