资源描述:
《c++表达式求值实现》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1:工程结构2:源代码1.cpp:#include#include#include#includeusingnamespacestd;typedeffloattype1;typedefchartype2;structzhan1{type1opnd[50];intbasic;inttop;};structzhan2{type2optr[52];intbasic;inttop;};charyunsuan[8]={'+','-','*','/','(',')','#'};intpriority[7][7]={{3,3,1,
2、1,1,3,3},{3,3,1,1,1,3,3},{3,3,3,3,1,1,3},{3,3,3,3,1,3,3},{1,1,1,1,1,2,0},{3,3,3,3,0,3,3},{1,1,1,1,1,0,2}};intyunsuanfu(charc){for(inti=0;i<7;i++){if(c==yunsuan[i]){return1;}}return0;}intyouxianji(chara,charb){intia=0;intib=0;for(inti=0;i<7;i++){if(a==yunsuan[i]){ia=i;break;}}for(i=0;i<7;i++){if(b
3、==yunsuan[i]){ib=i;break;}}returnpriority[ia][ib];}intoperate(inta,charthe,intb){chars[5]={'+','-','*','/'};if(the==s[0])returna+b;if(the==s[1])returna-b;if(the==s[2])returna*b;if(the==s[3])returna/b;}voidmain(){zhan1s1;zhan2s2;s1.top=0;s1.basic=0;s2.top=0;s2.basic=0;s1.opnd[s1.top]=0;charc;s2.op
4、tr[s2.top]='#';c=getchar();while(c!='#'
5、
6、s2.optr[s2.top]!='#'){if(yunsuanfu(c)){switch(youxianji(s2.optr[s2.top],c)){case0:cout<<"输入操作符错误,重新输入:"<7、atb=s1.opnd[s1.top-1];s1.top--;floata=s1.opnd[s1.top-1];s1.top--;floatanswer=operate(a,theta,b);s1.opnd[s1.top]=answer;s1.top++;break;}}else{if(isdigit(c)){floatshuzi[12];inti=0;doubles=0.0;floatzhi=0;shuzi[i]=(float)c-48.0;i++;c=getchar();while(isdigit(c)&&i<=11){shuzi[i]=(float)c-48.0;i++;c=get
8、char();}s=i;for(intj=0;j