#哪位大佬帮我看看,我的逆波兰表达式输出为什么不对呢?
- 应该输出的是:10.5 4.5 + 3 - 12 2 5 + 3 - 2 / * - 5 5 1 4 * - * +
- 代码输出的是:10.5 4.5 + 3 - 12 2 5 + 3 - 2 / * 5 5 1 4 * - * + -
有一个减号的顺序不对!
主要看 toRPN()函数就好。
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::stack;
using std::string;
using std::vector;
void toRPN(string usrInput, int inputLen, vector<string>* rpnArray);
bool isDigit(char chInStr);
bool isDigit(string str);
double calculator(vector<string>* rpnArray);
double opt(double num2, double num1, string optr);
int optrOrder(char optr);
int main()
{
cout << "请输入算式:" << endl;
cout << "- 输入:";
string usrInput = "10.5+4.5-3-12*((2+5-3)/2)+5*(5-1*4)";
cout << usrInput << endl;
//std::getline(cin, usrInput);
int inputLen = (int)usrInput.length();
vector<string> rpnArray;
toRPN(usrInput, inputLen, &rpnArray);
cout << "- 结果:" << calculator(&rpnArray) << endl;
return 0;
}
//将表达式转换为逆波兰表达式
void toRPN(string usrInput, int inputLen, vector<string>* rpnArray)
{
stack<char> Operator;
//中缀转后缀
int i = 0;
while (i < inputLen)
{
//处理空格
if (usrInput[i] == ' ')
{
i++;
continue;
}
//处理操作数
if (isDigit(usrInput[i]))
{
string numStr;
while (i < inputLen && (isDigit(usrInput[i]) || usrInput[i] == '.'))
{
numStr += usrInput[i++];
}
rpnArray->push_back(numStr);
continue;
}
//处理左括号
if (usrInput[i] == '(')
{
Operator.push(usrInput[i]);
i++;
}
//处理右括号
else if (usrInput[i] == ')')
{
while (!Operator.empty() && Operator.top() != '(')
{
rpnArray->push_back(string(1, Operator.top()));
Operator.pop();
}
if (!Operator.empty())
{
Operator.pop();
}
i++;
}
//处理四则运算符
else if (optrOrder(usrInput[i]) > 0)
{
if (!Operator.empty() && Operator.top() != '(' &&
optrOrder(Operator.top()) >= optrOrder(usrInput[i]))
{
rpnArray->push_back(string(1, Operator.top()));
Operator.pop();
}
Operator.push(usrInput[i]);
i++;
}
//处理无效字符
else
{
i++;
continue;
}
}
//处理剩下的内容
//输出测试
while (!Operator.empty())
{
rpnArray->push_back(string(1, Operator.top()));
Operator.pop();
}
for (int i = 0; i < (int)rpnArray->size(); i++)
{
cout << (*rpnArray)[i] << " ";
}
cout << endl;
}
//判读字符是否为数字
bool isDigit(char chInStr)
{
if ((chInStr >= '0' && chInStr <= '9') || chInStr == '.')
{
return true;
}
return false;
}
//判读字符串是否为数字
bool isDigit(string str)
{
bool judge;
for (int i = 0; i < (int)str.length(); i++)
{
if ((str[i] >= '0' && str[i] <= '9') || str[i] == '.')
{
judge = true;
}
else
{
judge = false;
}
}
return judge;
}
//获取运算结果
double calculator(vector<string>* rpnArray)
{
stack<double> Number;
int arrLen = (int)rpnArray->size();
double left;
double right;
double result;
for (int i = 0; i < arrLen; i++)
{
if (isDigit((*rpnArray)[i]))
{
Number.push(stod((*rpnArray)[i]));
}
else
{
right = Number.top();
Number.pop();
left = Number.top();
Number.pop();
result = opt(left, right, (*rpnArray)[i]);
Number.push(result);
}
}
return result;
}
//计算运算符两侧数字
double opt(double left, double right, string optr)
{
if (optr == "+")
{
return left + right;
}
else if (optr == "-")
{
return left - right;
}
else if (optr == "*")
{
return left * right;
}
else
{
return left / right;
}
}
//获取优先级
int optrOrder(char optr)
{
if (optr == '+' || optr == '-')
{
return 1;
}
if (optr == '*' || optr == '/')
{
return 2;
}
return 0;
}