0$房天下问答|数据结构编写计算器程序$https://m.fang.com/ask/ask_1406584.html$https://static.soufunimg.com/common_m/m_public/201511/images/asksharedefault.png
packc/pages/ask/detail/detail?askid=1406584
共1个回答
-
-
-
朋迷家族
丨Lv 0
#pragma warning(disable:4786) #include<string> #include<map> #include<stack> #include<iostream> #include<ctype.h> using namespace std; void main() { map<string, char>table; table["++"]='>'; table["+-"]='>'; table["+*"]='<'; table["+/"]='<'; table["+("]='<'; table["+)"]='>'; table["+#"]='>'; table["-+"]='>'; table["--"]='>'; table["-*"]='<'; table["-/"]='<'; table["-("]='<'; table["-)"]='>'; table["-#"]='>'; table["*+"]='>'; table["*-"]='>'; table["**"]='>'; table["*/"]='>'; table["*("]='<'; table["*)"]='>'; table["*#"]='>'; table["/+"]='>'; table["/-"]='>'; table["/*"]='>'; table["//"]='>'; table["/("]='<'; table["/)"]='>'; table["/#"]='>'; table[")+"]='>'; table[")-"]='>'; table[")*"]='>'; table[")/"]='>'; table["))"]='>'; table[")#"]='>'; table["(+"]='<'; table["(-"]='<'; table["(*"]='<'; table["(/"]='<'; table["(("]='<'; table["()"]='='; table["#+"]='<'; table["#-"]='<'; table["#*"]='<'; table["#/"]='<'; table["#("]='<'; table["##"]='='; stack<float> num; stack<char> alp; alp.push('#'); string s="35*10*50"; s+='#'; int i=0; char c=s[i++]; int tag=0; while(c!='#'||alp.top()!='#') { if(isdigit(c)) { char d=s[i]; tag++; if(isdigit(d)) { num.push((float)c-48); } else { num.push((float)c-48); float n=1,m=0; for(int j=tag;j>0;j--) { m=num.top()*n+m; n=n*10; num.pop(); } num.push(m); tag=0; } //num.push((float)c-48);//字符转数字 c=s[i++]; } else { string s1; s1+=alp.top(); s1+=c; char c1=table[s1]; switch(c1) { case'<': alp.push(c); c=s[i++]; break; case'=': alp.pop(); c=s[i++]; break; case'>': float y=num.top(); num.pop(); float x=num.top(); num.pop(); char c2=alp.top(); alp.pop(); switch(c2) { case'+': x=x+y; break; case'-': x=x-y; break; case'*': x=x*y; break; case'/': x=x/y; break; } num.push(x); cout<<num.top()<<endl; break; } } } cout<<num.top(); }
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。

关注成功