<pre name="code" class="cpp">/*************************************************************************
> File Name: ReversePolish.cpp
> Author: Shaojie Kang
> Mail: kangshaojie@ict.ac.cn
> Created Time: 2015年09月16日 星期三 11时17分04秒
> Problem:
将中缀表达式转化为逆波兰式,并计算
************************************************************************/
#include<iostream>
#include<string>
#include<stack>
#include<cctype>
#include<map>
#include<cstdlib>
#include<vector>
using namespace std;
/* 优先级定义 */
map<string, int> precedence;
/* transfer function */
vector<string> Transfer(string &str)
{
vector<string> result;
stack<string> operators;
if(str.empty()) return result;
str.insert(0, 1, '(');
str.push_back(')');
int size = str.size();
for(int i = 0; i < size; ++i)
{
// 忽略空格
if(str[i] == ' ') continue;
// 提取数字
if(isdigit(str[i]))
{
string temp;
while(i < size && isdigit(str[i]))
{
temp += str[i];
++i;
}
result.push_back(temp);
--i;
continue;
}
// 将(压入栈
if(str[i] == '(')
operators.push(string(1, str[i]));
// 遇到右括号是,左括号之前的操作符都要出栈
else if(str[i] == ')')
{
while(operators.top() != "(")
{
result.push_back(operators.top());
operators.pop();
}
operators.pop(); // 左括号出栈
}
else if(operators.empty() || precedence[string(1, str[i])] > precedence[operators.top()])
operators.push(string(1, str[i]));
else // 当前操作符的优先级大于栈顶操作符的优先级
{
// 优先级大于当前操作符的操作符都要出栈
while(!operators.empty())
{
string op = operators.top();
if(precedence[op] >= precedence[string(1, str[i])])
{
result.push_back(op);
operators.pop();
}
else break;
}
operators.push(string(1, str[i])); // 将当前操作符号入栈
}
}
return result;
}
// 计算逆波兰式
int Calculate(string &operations)
{
vector<string> result = Transfer(operations);
stack<int> nums;
for(int i = 0; i < result.size(); ++i)
{
if(isdigit(result[i][0]))
nums.push(atoi(result[i].c_str()));
else
{
int num2 = nums.top();
nums.pop();
int num1 = nums.top();
nums.pop();
if(result[i] == "+")
nums.push(num1 + num2);
else if(result[i] == "-")
nums.push(num1 - num2);
else if(result[i] == "*")
nums.push(num1 * num2);
else nums.push(num1 / num2);
}
}
return nums.top();
}
void print(const vector<string> nums)
{
for(int i = 0; i < nums.size(); ++i)
cout<<nums[i]<<" ";
cout<<endl;
}
int main()
{
precedence["("] = -1;
precedence["+"] = precedence["-"] = 0;
precedence["*"] = precedence["/"] = 1;
string str("5/4+2*(3+2)*10");
cout<<Calculate(str)<<endl;
return 0;
}
将中缀表达式转化为逆波兰式(c++实现)
最新推荐文章于 2024-08-26 00:20:27 发布