您的位置:首页 > 编程语言

四则运算——逆波兰

2015-09-05 16:11 363 查看
写了个四则运算的代码,贴出来给大家看看。代码考虑了负数和不止个位数参与运算的情况,还考虑有中括号大括号的情况。

VS 2010下编译。

<span style="font-size:18px;">#include <iostream>
#include<string>
#include <vector>
using namespace std;

int getPriority(int ch)
{
switch(ch)
{
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
}
return -1;
}

void getSuffixExpre(string str,vector<string>& resultVec)
{
vector<char> stack;
string temp;
bool nagtiveNum = false;
for (int i = 0; i < (int) str.size(); ++i)
{
if (str[i] == ' ')
{
continue;
}
if (str[i] == '-')//处理负数
{
if (i - 1 < 0 || !('0' <= str[i - 1] && '9' >= str[i - 1]))
{
temp.push_back('-');
continue;
}
}
if (str[i] == '[' || str[i] == '{')//处理大括号和中括号
{
str[i] = '(';
}
if (str[i] == ']' || str[i] == '}')
{
str[i] = ')';
}

if (('0' <= str[i] && '9' >= str[i]) )//数字直接输出
{
temp.push_back(str[i]);
}
else//以符号为分界
{
if (!temp.empty())
{
resultVec.push_back(temp);
temp.clear();
}

if (stack.empty()|| str[i] == '(')
{
stack.push_back(str[i]);
}
else
{
if (str[i] == ')')//遇到左括号出栈直到右括号
{
while (stack.back() != '(')
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
}

stack.pop_back();
}
else
{
while (getPriority(str[i]) <= getPriority(stack.back()))//优先级小于栈顶优先级的时候将栈顶元素出栈
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
if (stack.empty())
{
break;
}
}
stack.push_back(str[i]);
}
}
}
}
if (!temp.empty())//最后一个数字
{
resultVec.push_back(temp);
temp.clear();
}

while(!stack.empty())//剩下的符号出栈
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
}

}
vector<string> getInputExpreVec(string str)
{
vector<string> inputVec;
string temp;
for (int i = 0; i < (int)str.length(); ++i)
{
if (str[i] == ' ')
{
continue;
}

if ('0' <= str[i] && '9' >= str[i])
{
temp.push_back(str[i]);
}
else
{
if (!temp.empty())
{
inputVec.push_back(temp);
temp.clear();
}
temp = str[i];
inputVec.push_back(temp);
temp.clear();
}

}

if (!temp.empty())
{
inputVec.push_back(temp);
}

return inputVec;
}
double computRule(string str, double first, double second)
{
switch(str[0])
{
case '+':
return first + second;
case '-':
return first - second;
case '*':
return first * second;
case '/':
return first / second;
}
return -1;
}

double computExpre(vector<string> suffix_expre)//计算后缀表达式
{
vector<double> stack;
double fisrtOperator = 0;
double secondOperator = 0;
for (int i = 0; i < (int)suffix_expre.size(); ++i)
{
if ((suffix_expre[i] != "+") && (suffix_expre[i] != "-") && (suffix_expre[i] != "*") && (suffix_expre[i] != "/"))//数字直接入栈
{
stack.push_back(atof(suffix_expre[i].c_str()));
}
else//符号出栈尾部两个元素运算
{
secondOperator = stack.back();//先出的是第二操作数
stack.pop_back();
fisrtOperator = stack.back();
stack.pop_back();
stack.push_back(computRule(suffix_expre[i], fisrtOperator, secondOperator)) ;
}
}
return stack.back();
}
int main()
{
string inputStr;
getline(cin, inputStr);
vector<string> suffix_vec;
getSuffixExpre(inputStr, suffix_vec);

cout << computExpre(suffix_vec);
return 0;
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息