NYOJ 409 郁闷的C小加(三)
来源:互联网 发布:南海知乎 编辑:程序博客网 时间:2024/05/17 02:11
郁闷的C小加(三)
时间限制:1000 ms | 内存限制:65535 KB
难度:4
- 描述
- 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很高兴。但C小加是个爱思考的人,他又想通过这种方法计算一个表达式的值。即先把表达式转换为前缀和后缀表达式,再求值。这时又要考虑操作数是小数和多位数的情况。
- 输入
- 第一行输入一个整数T,共有T组测试数据(T<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数并且小于1000000。
数据保证除数不会为0。 - 输出
- 对于每组测试数据输出结果包括三行,先输出转换后的前缀和后缀表达式,再输出计算结果,结果保留两位小数。
- 样例输入
21+2=(19+21)*3-4/5=
- 样例输出
+ 1 2 =1 2 + =3.00- * + 19 21 3 / 4 5 =19 21 + 3 * 4 5 / - =119.20
- 上传者
苗栋栋
#include<iostream>#include<stack>#include<iomanip>#include <stdlib.h>#include<algorithm>#include<iomanip>using namespace std;int cmp(char c){if(c=='+'||c=='-')return 1;if(c=='*'||c=='/')return 2;return 0;}void compute(stack<double>&num1,stack<char>&op){double b=num1.top();num1.pop();double a=num1.top();num1.pop();switch(op.top()){case '+':num1.push(a+b);break;case '-':num1.push(a-b);break;case '*':num1.push(a*b);break;case '/':num1.push(a/b);break;}op.pop();}int main(){int n;cin>>n;stack<char>op;stack<double>num;stack<double>num1;while(n--){string str,str1,str2,str3;cin>>str;str2=str;str=str.substr(0,str.size()-1);reverse(str.begin(),str.end());for(int i=0;i<str.size();i++){if(isdigit(str[i])){while(i<str.size()&&(isdigit(str[i])||str[i]=='.')){str1+=str[i];i++;}str1+=' ';i--;}elseif(str[i]==')')op.push(str[i]);elseif(str[i]=='('){while(op.top()!=')'){str1+=op.top();str1+=' ';op.pop();}op.pop();}else{if(op.empty()||cmp(op.top())<=cmp(str[i]))op.push(str[i]);else{while(!op.empty()&&cmp(op.top())>cmp(str[i])) { str1+=op.top(); str1+=' '; op.pop();}op.push(str[i]);}}}while(!op.empty()){str1+=op.top();str1+=' ';op.pop();}str1=str1.substr(0,str1.size()-1);reverse(str1.begin(),str1.end());str1=str1+' '+'=';for(int i=0;i<str2.size();i++){if(isdigit(str2[i])){double m=atof(&str2[i]);while(i<str2.size()&&(isdigit(str2[i])||str2[i]=='.')){str3+=str2[i];i++;}str3+=' ';i--;num1.push(m);}elseif(str2[i]=='(')op.push(str2[i]);elseif(str2[i]==')'){while(op.top()!='('){str3+=op.top();str3+=' ';compute(num1,op);}op.pop();}else{if(op.empty()||cmp(op.top())<cmp(str2[i]))op.push(str2[i]);else{while(!op.empty()&&cmp(op.top())>=cmp(str2[i])) { str3+=op.top(); str3+=' '; compute(num1,op);}op.push(str2[i]);}}}while(!op.empty()){str3+=op.top();str3+=' ';op.pop();}cout<<str1<<endl; cout<<str3<<endl;cout<<fixed<<setprecision(2)<<num1.top()<<endl;}return 0;}
这c小加也是真艹dan 天天郁闷
阅读全文
0 0
- 郁闷的C小加(三)(nyoj 409)
- NYOJ 409 郁闷的C小加(三)
- nyoj 题目409 郁闷的C小加(三)
- NYOJ 409 郁闷的C小加(三)
- NYOJ 409 郁闷的C小加(三)
- NYOJ--郁闷的C小加(三)
- 郁闷的C小加(三)
- 郁闷的C小加(三)
- 郁闷的C小加(三)
- nyoj-郁闷的C小加
- 郁闷的C小加(三) 409
- NYOJ 267 郁闷的C小加(二)
- nyoj 257 郁闷的C小加(一)
- NYOJ - 郁闷的C小加(一)
- NYOJ - 郁闷的C小加(二)
- NYOJ 257 郁闷的C小加(一)
- NYOJ--郁闷的C小加(一)
- NYOJ 267郁闷的C小加(二)
- 156个Python网络爬虫资源
- 转角
- 左值引用和右值引用
- 约瑟夫环
- 《好未来编程题》 输入n个整数,输出出现次数大于等于数组长度一半的数
- NYOJ 409 郁闷的C小加(三)
- java微信开发的服务器(令牌)token
- obs 核心结构体(一)
- SpringMVC+freemarker 视图解析最小实现
- 观察这模式的学习
- 使用webpack 的commonjs
- 每日一题(52)—— 进程
- Android 源码学习笔记一——前期准备
- 洛谷 P2735 电网 Electric Fences