gpt4 book ai didi

c++ - 没有优先级且左结合的字符串计算器

转载 作者:行者123 更新时间:2023-11-28 03:18:24 25 4
gpt4 key购买 nike

这个字符串计算器的代码没有运算符优先级,我不太确定如何修复它。我相信它与 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;
}

最佳答案

我发现您的代码存在两个问题。

首先,更容易修复。 Cin >> string 只会获取第一个空白字符之前的所有内容。可能值得考虑 http://www.cplusplus.com/reference/istream/istream/getline/这将把一切都带到换行符。如果您键入“1 + 1”,这将阻止您的程序仅返回“1”

你的第二个问题,也是我认为你更感兴趣的一个,是你不小心使你的运算符右结合。这是程序递归方式的结果。如果您考虑一下函数为语句 1 + 2 * 3 调用自身的方式。当它遇到第一个 + 时,它会调用 ExpressionValue('1') + ExpressionValue(2 * 3)。因此,在这种情况下,乘法将在加法之前计算。

解决该问题的最简单方法可能就是向后解析字符串,但我怀疑这需要对处理括号的代码进行相当多的更改。

有点不相关的说明。在样式方面,您的一些代码有点难以阅读,因为您没有缩进函数的内部。

关于c++ - 没有优先级且左结合的字符串计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16182297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com