九度OJ 1019:简单计算器
来源:互联网 发布:廊坊百度快照优化推广 编辑:程序博客网 时间:2024/04/30 05:49
#include <iostream>#include <stack>#include <stdio.h>#include <string>#include <map>#include <vector>#include <regex>using namespace std;/*** 九度:1019:简单计算器* [10/9/2014 liu]*/// 1、将中缀表达式转换成前缀表达式或者是后缀表达式map<string,int> mapOperator;int getOperPriority(string a){// find函数返回的是一个迭代器std::map<string, int>::iterator pos;pos = mapOperator.find(a);return pos->second;}vector<string> split(string str, string pattern){vector<string> v;int size = str.length();str += pattern; // 这句话要注意while (str !=""){int index = str.find(pattern);string strtemp = str.substr(0, index);str.erase(0, index + 1);v.push_back(strtemp);}return v;}vector<string> centertopreExpression(vector<string> str){// mapOperator.insert(make_pair("+", 1));mapOperator.insert(make_pair("-", 1));mapOperator.insert(make_pair("*", 2));mapOperator.insert(make_pair("/", 2));// 3+4*5-4/3 // 定义两个栈stack<string> operatorStack; // 操作符栈stack<string> numStack; // 操作数栈for (int i = str.size() - 1; i >= 0; i--){if (isdigit(str[i][0])) // isdigit 判断字符{numStack.push(str[i]);}else{if (operatorStack.empty())operatorStack.push(str[i]);else{if (getOperPriority(str[i]) >= getOperPriority(operatorStack.top())){operatorStack.push(str[i]);}else{numStack.push(operatorStack.top());operatorStack.pop();while (!operatorStack.empty() && getOperPriority(str[i]) < getOperPriority(operatorStack.top())){numStack.push(operatorStack.top());operatorStack.pop();}operatorStack.push(str[i]);}}}}while(!operatorStack.empty()){numStack.push(operatorStack.top());operatorStack.pop();}int n = numStack.size();vector<string> strtemp; int temp = 0;while (!numStack.empty()){strtemp.push_back(numStack.top());numStack.pop();temp++;}return strtemp;}void calculate(stack<double> &intStack,string str){double a = intStack.top();intStack.pop();double b = intStack.top();intStack.pop();if (str == "*"){intStack.push(a*b);}else if (str == "/" ){intStack.push(a / b);}else if (str == "-"){intStack.push(a -b);}else if (str == "+"){intStack.push(a+b);}}int GetResult(string str){vector<string> strvec = split(str, " ");vector<string> resultstr = centertopreExpression(strvec);stack<double> intStack;for (int i = resultstr.size() - 1; i >= 0; i--){if (isdigit(resultstr[i][0])){int a = atoi(resultstr[i].c_str());intStack.push(a);}else{calculate(intStack, resultstr[i]);}}double a = intStack.top();return a;}int main(){string str;while (getline(cin,str)){printf("%d", GetResult(str));}return 0;}
上面的代码估计不能AC,做的题目太少,但是功能应该实现了
主要是使用了中缀表达式转换成前缀表达式,算是实现了一种算法,参考如下博客
http://blog.csdn.net/antineutrino/article/details/6763722
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:简单计算器
- 题目1019:简单计算器 九度OJ
- 九度OJ—题目1019:简单计算器
- JAVA Static静态变量
- node.js部署开发环境,node不是内部命令也不是外部命令解决方案
- 在控制台应用程序中如何使用CString
- nginx php-fpm内存配置问题
- 白话经典算法系列之七 堆与堆排序
- 九度OJ 1019:简单计算器
- Codeforces Round #273 (Div. 2) --A Initial Bet
- oracle_001
- 从今天开始写博客
- 从不断更新query日志的字符串流中随机选择一个字符串?10000个字符串呢?
- Android教程-从零开始一步一步接入SDK
- 二分图匹配 POJ2195
- numa
- oracle regexp_replace函数