POJ-1539(表达式求值)
来源:互联网 发布:阿里旺旺网络连接失败 编辑:程序博客网 时间:2024/05/23 13:42
题目:http://poj.org/problem?id=1539
#include <cctype>#include <string>#include <iostream>using namespace std;int value[26];char used[26], post[26];void preEval(string& exp){ int i = 0, len = exp.size(); for(int k = 0; k < len; ++k){ if(!isspace(exp[k])) exp[i++] = exp[k]; } exp.resize(len = i); for(i = 0; i < 26; ++i){ used[i] = 0; value[i] = i + 1; post[i] = 0; } for(i = 0; i < len; ++i){ if(isalpha(exp[i])) used[exp[i] - 'a'] = 1; } for(i = 0; i < len && (i = exp.find("++", i)) != string::npos; ){ if(i + 2 < len && isalpha(exp[i+2])) ++value[exp[i+2] - 'a']; else post[exp[i-1] - 'a'] = 1; exp = exp.substr(0, i) + exp.substr(i + 2); } for(i = 0; i < len && (i = exp.find("--", i)) != string::npos; ){ if(i + 2 < len && isalpha(exp[i+2])) --value[exp[i+2] - 'a']; else post[exp[i-1] - 'a'] = -1; exp = exp.substr(0, i) + exp.substr(i + 2); }// cout << "exp = " << exp << "\n";}int eval(const string& exp, int res = 0){ if(exp.empty()) return res; int i = 0; bool add = true; if(!isalpha(exp[i])){ add = exp[i] == '+'; ++i; } if(add) res += value[exp[i] - 'a']; else res -= value[exp[i] - 'a']; return eval(exp.substr(i+1), res);}void postEval(){ for(int i = 0; i < 26; ++i){ if(used[i]) cout << " " << char('a' + i) << " = " << value[i] + post[i] << "\n"; }}int main(){ ios::sync_with_stdio(false); string exp; while(getline(cin, exp)){ cout << "Expression: " << exp << "\n"; preEval(exp); cout << " " << "value = " << eval(exp) << "\n"; postEval(); } return 0;}
0 0
- POJ-1539(表达式求值)
- poj 2269 Friends(表达式求值)
- poj 1686 表达式求值判断
- poj 1686 Lazy Math Instructor(表达式求值)
- 表达式求值(上)
- 表达式求值(中)
- 表达式求值(下)
- 四则运算(表达式求值)
- 表达式求值(char)
- 表达式求值(nyoj305)
- 表达式求值(一)
- 表达式求值(二)
- 表达式求值(nyoj305)
- 表达式求值(一)
- 表达式求值(栈)
- 表达式求值(OJ)
- 表达式求值(二)
- 表达式求值(数据结构)
- Ubuntu新安装 功能软件集合
- uva 193(回溯)
- 多校第八场:几何+图论出度+模拟+找规律
- 线性表的链式表示和实现
- Linux 网络开发常见面试题回顾
- POJ-1539(表达式求值)
- Uva1626 线性DP
- 自用收藏的链接
- 给定一整型数组,若数组中某个下标值大的元素值小于某个下标值比它小的元素值,称这是一个反序
- LeetCode 1 Reverse Words in a String
- 2大类型的设备驱动程序(2 main types of device driver)
- POJ-1400(删除冗余括号)
- 迷茫
- UVA307