标签:str nbsp tar print ++ bool blank https void
题目链接:https://www.nowcoder.com/questionTerminal/5759c29a28cb4361bc3605979d5a6130
AC代码:
#include <stack>
#include <stdio.h> #include <ctype.h> using namespace std; char str[220]; int mat[][5]={ 1,0,0,0,0, 1,0,0,0,0, 1,0,0,0,0, 1,1,1,0,0, 1,1,1,0,0, }; stack<int> op; stack<double> num; void getOp(bool &reto,int &retn,int &i){ if(i==0 && op.empty()){ reto=true; retn=0; return; } if(str[i]==0){ reto=true; retn=0; return; } if(str[i]>=‘0‘ && str[i]<=‘9‘){ reto=false; } else{ reto=true; switch(str[i]){ case ‘+‘:retn=1;break; case ‘-‘:retn=2;break; case ‘*‘:retn=3;break; case ‘/‘:retn=4;break; } i+=2; return; } retn=0; for(;str[i]!=0 && str[i]!=‘ ‘;i++){ retn*=10; retn+=str[i]-‘0‘; } if(str[i]==‘ ‘)i++; return; } int main(){ while(gets(str)){ if(str[0]==‘0‘ && str[1]==0)break; bool reto; int retn; int idx=0; while(!op.empty())op.pop(); while(!num.empty())num.pop(); while(true){ getOp(reto,retn,idx); if(reto==false)num.push((double)retn); else{ if(op.empty() || mat[retn][op.top()]==1) op.push(retn); else{ double tmp; while(mat[retn][op.top()]==0){ double a,b; b=num.top();num.pop(); a=num.top();num.pop(); switch(op.top()){ case 1:tmp=a+b;break; case 2:tmp=a-b;break; case 3:tmp=a*b;break; case 4:tmp=a/b;break; } op.pop(); num.push(tmp); } op.push(retn); } } if(op.size()==2 && op.top()==0) break; } printf("%.2f\n",num.top()); } return 0; }
标签:str nbsp tar print ++ bool blank https void
原文地址:https://www.cnblogs.com/yun-an/p/11067458.html