高手帮忙作一道题呗
输入一数学表达式的字符串,输出该表达式值(不考虑先乘除后加减).例:输入:2*3+4/5-6 输出:-4高手可一定要露脸啊!
#include<iostream.h> typedef struct { int num[10]; int top; }numStack; typedef struct { char ch[10]; int top; }charStack; void InitnStack(numStack &ns) { ns.top=-1; } void InitcStack(charStack &cs) { cs.top=0; cs.ch[0]='#'; } bool iscEmpty(charStack cs) { if(cs.top==0)return true; else return false; } void numPush(numStack &ns,int n) { ns.top++; ns.num[ns.top]=n; } void charPush(charStack &cs,char n) { cs.top++; cs.ch[cs.top]=n; } int numPop(numStack &ns) { ns.top--; return ns.num[ns.top+1]; } char charPop(charStack &cs) { cs.top--; return cs.ch[cs.top+1]; } int getnTop(numStack ns) { return ns.num[ns.top]; } char getcTop(charStack cs) { return cs.ch[cs.top]; } int IsChar(char c) { switch(c) { case '+':return 1;break; case '-':return 2;break; case '*':return 3;break; case '/':return 4;break; case '#':return 5;break; case '(':return 6;break; case ')':return 7;break; default:return 0;break; } } int IsNum(char c) { switch(c) { case '0':return 0; case '1':return 1; case '2':return 2; case '3':return 3; case '4':return 4; case '5':return 5; case '6':return 6; case '7':return 7; case '8':return 8; case '9':return 9; default:return 10; } } bool translate(char s[],numStack &ns,charStack &cs) { int i=0,m,n; InitnStack(ns); InitcStack(cs); while(s[i]!='\0') { switch(IsChar(s[i])) { case 7: { while(getcTop(cs)!='(') { switch(IsChar(charPop(cs))) { case 1:m=numPop(ns);n=numPop(ns);numPush(ns,n+m);break; case 2:m=numPop(ns);n=numPop(ns);numPush(ns,n-m);break; case 3:m=numPop(ns);n=numPop(ns);numPush(ns,n*m);break; case 4:m=numPop(ns);n=numPop(ns);numPush(ns,n/m);break; } } charPop(cs); i++; break; } case 6:charPush(cs,s[i]);i++;break; case 1: { while(IsChar(getcTop(cs))!=1&&IsChar(getcTop(cs))!=5&&IsChar(getcTop(cs))!=6) { switch(IsChar(getcTop(cs))) { case 2:charPop(cs);m=numPop(ns);n=numPop(ns);numPush(ns,n-m);break; case 3:charPop(cs);m=numPop(ns);n=numPop(ns);numPush(ns,n*m);break; case 4:charPop(cs);m=numPop(ns);n=numPop(ns);numPush(ns,n/m);break; } } charPush(cs,s[i]); i++; break; } case 2: { while(IsChar(getcTop(cs))!=1&&IsChar(getcTop(cs))!=5&&IsChar(getcTop(cs))!=6) { switch(IsChar(getcTop(cs))) { case 2:charPop(cs);m=numPop(ns);n=numPop(ns);numPush(ns,n-m);break; case 3:charPop(cs);m=numPop(ns);n=numPop(ns);numPush(ns,n*m);break; case 4:charPop(cs);m=numPop(ns);n=numPop(ns);numPush(ns,n/m);break; } } charPush(cs,s[i]); i++; break; } case 3: { while(IsChar(getcTop(cs))!=1&&IsChar(getcTop(cs))!=5&&IsChar(getcTop(cs))!=2&&IsChar(getcTop(cs))!=3&&IsChar(getcTop(cs))!=6) { charPop(cs);m=numPop(ns);n=numPop(ns);numPush(ns,n/m);break; } charPush(cs,s[i]); i++; break; } case 4:charPush(cs,s[i]);i++;break; case 0: { int num=0; while(IsChar(s[i])==0) { if(IsNum(s[i])==10) { if(s[i]!='\0') { cout<<"ÊäÈëÓÐÎó£¡"<<endl; return false; } else break; } else { m=IsNum(s[i]); num=10*num+m; i++; } } // cout<<m<<endl; numPush(ns,num); break; } } } return true; } int calculate(numStack &ns,charStack &cs) { int m,n; while(!iscEmpty(cs)) { switch(IsChar(charPop(cs))) { case 1:m=numPop(ns);n=numPop(ns);numPush(ns,n+m);break; case 2:m=numPop(ns);n=numPop(ns);numPush(ns,n-m);break; case 3:m=numPop(ns);n=numPop(ns);numPush(ns,n*m);break; case 4:m=numPop(ns);n=numPop(ns);numPush(ns,n/m);break; case 6:cout<<"ÀšºÅ²»Æ¥Å䣡"<<endl;return 0; } } return numPop(ns); } void main() { while(1) { char a[20]; cin>>a; if(a[0]=='E') return; else { numStack ns; charStack cs; InitnStack(ns); InitcStack(cs); if(translate(a,ns,cs)==true) { cout<<"------------------"<<endl; cout<<a<<"="<<calculate(ns,cs)<<endl; } } cout<<"ÊäÈëEÍ˳ö"<<endl; cout<<endl; } }