这个字符串计算器的代码没有运算符优先级,我不太确定如何修复它。我相信它与 switch 语句有关,但我需要这个字符串计算器从左到右移动并计算是否在减法/加法之前发生乘法运算。这是到目前为止的代码:
#include<iostream>
#include<sstream>
#include<string>
#include<cctype>
#include<cmath>
using namespace std;
enum {PLUS='+',MINUS='-',MULT='*'};
int numberValue(string &expr)
{
istringstream is(expr);
int value = 0;
is >> value;
return value;
}
int expressionValue(string &expr)
{
int i = 0;
int p = 0;
if(expr.at(0) == '(' && expr.at(expr.length()-1) == ')')
{
for(i=0;i<expr.length();i++)
{
if(expr.at(i)=='(')
p++;
else if(expr.at(i)==')')
p--;
if(p==0)
break;
}
if(i==expr.length()-1)
return expressionValue(expr.substr(1,expr.length()-2));
}
for(i=0;i<expr.length();i++)
{
if(expr.at(i)=='(')
p++;
else if(expr.at(i)==')')
p--;
else if(p==0 && ispunct(expr.at(i)))
{
switch(expr.at(i))
{
case PLUS:
return expressionValue(expr.substr(0,i)) +
expressionValue(expr.substr(i+1,expr.length()-i- 1));
case MINUS:
return expressionValue(expr.substr(0,i)) -
expressionValue(expr.substr(i+1,expr.length()-i-1));
case MULT:
return expressionValue(expr.substr(0,i)) *
expressionValue(expr.substr(i+1,expr.length()-i-1));
}
}
}
return numberValue(expr);
}
bool Validate(string inputStr)
{
for(int x = 0;x < inputStr.length(); x ++)
{
if((inputStr[x] == '+') || (inputStr[x] == '-'))
{
if((inputStr[x+1] == '+') || (inputStr[x+1] =='-'))
{
return false;
}
}
}
string arr = "0123456789+-*";
int count = 0;
for(int a = 0; a < inputStr.length(); a++)
{
for(int b = 0; b < arr.length(); b++)
{
if(inputStr[a] == arr[b])
{
count++;
}
}
}
if(count == inputStr.length())
{
return true;
}
else
{
return false;
}
}
int main()
{
string expressionString;
string retry = "y";
cout << "Enter an expression as a string...you can use addition, subtraction \nand multiplication."<< endl;
cout << "\nKeep in mind that this calculator does not accept decimals and the \nleading operand can not be negative." << endl;
cout << "\nOperands can have no more than 8 digits, each operator has the same \nprecedence, and each operator is left associative." << endl;
do
{
cout << "\nEnter an expression: ";
cin >> expressionString;
if(Validate(expressionString) == true)
{
cout << expressionValue(expressionString) << endl;
}
else
{
cout << "An error has occured in the input" << endl;
}
cout << "Press \"y\" to enter another expression or \"n\" if you want to quit." << endl;
cin >> retry;
}while
(retry == "y");
return 0;
}