九度OJ——1019简单计算器
来源:互联网 发布:excel 数据清洗 编辑:程序博客网 时间:2024/05/17 02:36
题目描述:
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入:
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出:
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入:
1 + 2
4 + 2 * 5 - 7 / 11
0
样例输出:
3.00
13.36
思路建我的另一篇博客:栈的应用——表达式求值
AC代码:
#include <iostream>#include <cstdio>#include <stack>#include <vector>#include <cstdlib>using namespace std;string str;vector<string> latter,mid;stack<string> s;stack<double> caculate;//栈外优先级int icp(char ch){ int result = 0; if(ch == '#'){ result = 0; } if(ch == '*'||ch == '/'){ result = 4; } if(ch == '+'||ch == '-'){ result = 2; } return result; } //栈内优先级int isp(char ch){ int result = 0; if(ch == '#'){ result = 0; } if(ch == '*'||ch == '/'){ result = 5; } if(ch == '+'||ch == '-'){ result = 3; } return result; } //生成中缀表达式void Median(){ for(int i = 0 ; i < str.length() ; i++){ if(str[i] >= '0' && str[i] <= '9'){ int j; for(j = i ; j < str.length() ; j++){ if(str[j]>= '0' && str[j] <= '9'){ continue; }else{ break; } } mid.push_back(string(str,i,j-i)); i = j-1; }else if(str[i] == ' '){ continue; }else{ mid.push_back(str.substr(i,1)); } } mid.push_back(string(1,'#'));}//生成后缀表达式void Latter(){ s.push(string(1,'#')); for(int i = 0 ; i < mid.size() ; i++){ //是数字直接追加大后缀表达式 if(mid[i][0] >= '0' && mid[i][0] <= '9'){ latter.push_back(mid[i]); }else{//是运算符 //栈顶元素的优先级小于栈外元素的优先级,栈外元素入栈 string ch = s.top(); if(isp(ch[0]) < icp(mid[i][0])){ s.push(string(mid[i])); }else if(isp(ch[0]) > icp(mid[i][0])){ //栈顶元素的优先级大于栈外元素的优先级,栈顶元素出栈到后缀表达式 latter.push_back(ch); s.pop(); i--; }else{//栈顶元素的优先级到等于栈外元素的优先级 ,说明遍历到"#",结束 s.pop(); } } } } int main(){ while(1){ mid.clear(); latter.clear(); str.clear(); getline(cin,str); if(str[0] == '0'){ break; } Median(); Latter(); for(int i = 0 ; i < latter.size() ; i++){ if(latter[i][0] >= '0' && latter[i][0] <= '9'){ double num = atof(latter[i].c_str()); caculate.push(num); }else{ double b = caculate.top(); caculate.pop(); double a = caculate.top(); caculate.pop(); double c; switch(latter[i][0]){ case '+': c = a + b;break; case '-': c = a - b;break; case '*': c = a * b;break; case '/': c = a / b;break; } caculate.push(c); } } double result = caculate.top(); caculate.pop(); printf("%.2f\n",result); } return 0; }
阅读全文
0 0
- 九度OJ——1019简单计算器
- 九度OJ—题目1019:简单计算器
- 九度OJ 1019 简单计算器
- 九度oj 题目1019:简单计算器
- 九度oj-1019:简单计算器
- 九度OJ-题目1019:简单计算器
- 九度oj-1019-简单计算器
- 九度OJ 1019:简单计算器
- 九度OJ-1019-简单计算器
- 九度OJ 题目1019:简单计算器
- 九度OJ-1019简单计算器
- <九度 OJ>题目1019:简单计算器
- 九度OJ 题目1019:简单计算器
- 九度OJ 1019 简单计算器
- 【九度oj】1019简单计算器
- 九度 OJ 题目1019:简单计算器
- 九度OJ-题目1019 简单计算器
- 九度OJ题目1019:简单计算器
- 南理工2016考研复试上机题男女程序员排队
- (简单)宏定义 与 用户自定义类型typedef
- 妹子图XXOO
- Graph Theory NO.1 HDU_1213_How Many Tables_并查集
- LazyInitializationException 懒加载异常的四种解决方案
- 九度OJ——1019简单计算器
- POJ 1515 浅谈无向图边双连通分量Tarjan求法
- 不同BIOS下的U盘启动设置方法
- Matlab 的动态曲线绘图
- 基于VC 6.0 开发的MFC程序如何在文件资源管理器中浏览并选中操作文件
- Qt之右键弹出菜单
- Learnable pooling with Context Gating for video classification
- HDU 5143 NPY and arithmetic progression 思维+枚举
- 在结构体中内存对齐的规则及其重要性