gpt4 book ai didi

c++ - 给出错误答案的表达式树

转载 作者:行者123 更新时间:2023-11-28 08:21:19 25 4
gpt4 key购买 nike

编辑

这是家庭作业,所以请不要直接编写代码。只是提示,谢谢!

我正在从事一个项目,该项目将使用表达式树来派生各种事物,然后对它们执行操作。现在我不太担心派生部分,我只想把操作部分放下。

我使用的表达式树代码适用于整数,但一旦我输入“x”或任何其他变量,我的答案就错了。我的程序使用后缀表达式字符串...下面是正确和错误的示例。

5 6 + 返回 11. 正确

5x 6x + 返回 11。不正确的需要是 11x

这是我的代码:

// This is the expression tree code I'm using
#ifndef EXPRNODE_H
#define EXPRNODE_H

#include <cstdlib> // for NULL
using namespace std;

//====================================== class ExprNode
class ExprNode {
public:
ExprNode(char oper, ExprNode* left, ExprNode* right);
ExprNode(int val);
int eval() const; // Evaluate expr tree. Return result.

private:
char _op; // one of +, -, *, /, #
int _value; // integer value used for constants.
ExprNode* _left; // left subtree
ExprNode* _right; // right subtree
};
#endif

//============================================= ExprNode constructor
// Constructs node for a binary operator.
ExprNode::ExprNode(char oper, ExprNode* left, ExprNode* right) {
_op = oper;
_left = left;
_right = right;
}

//============================================== ExprNode constructor
// Constructs a node for an integer constant
ExprNode::ExprNode(int v) {
_op = '#';
_value = v;
_left = NULL;
_right = NULL;
}

//===================================================== ExprNode::eval
int ExprNode::eval() const {
// Recursively evaluate expression tree and return result.
int result;
switch (_op) {
case '+':
result = _left->eval() + _right->eval();
break;
case '-':
result = _left->eval() - _right->eval();
break;
case '*':
result = _left->eval() * _right->eval();
break;
case '/':
result = _left->eval() / _right->eval();
break;
case '#':
result = _value; // an integer constant
break;
}
return result;
}

bool isOperator (char operand)
{
return operand == '+' || operand == '-' || operand == '*' || operand == '/' || operand == '^';
}

bool isNumber (char potentialNumber)
{
return potentialNumber >= '0' && potentialNumber <= '9';
}

bool isX (char letter)
{
return letter == 'x' || letter == 'X';
}

我不打算包括从中缀到后缀的代码,因为它是不必要的(我认为)......接下来是表达式树和计算的代码

// the expression string is the postfix expression I returned previously
void expressionTree(string expression)
{
string tempNum = "";
string tempNum2 = "";
int count = 1;
int tempNumInt;
int tempNum2Int;

// creates a blank total value and blank numbers
ExprNode* totalVal = new ExprNode('+', new ExprNode(0), new ExprNode(0));
ExprNode* tNum;
ExprNode* tNum2;

// loop through the postfix expression
for (unsigned int iterator = 0; iterator < expression.length(); iterator++)
{
if (isOperator(expression[iterator]))
{
// Don't need to worry about at the moment
if (expression[iterator] == '^')
{
// go to derivative later
}
else
{
if (count % 2 != 0)
{
// we'll do different derivatives here.... for now just add, subtract, multiply, divide
totalVal = new ExprNode(expression[iterator], tNum, tNum2);
}
else if (count % 2 == 0 && expression[iterator] == '+' || expression[iterator] == '*')
{
totalVal = new ExprNode(expression[iterator], tNum, totalVal);
}
else if (count % 2 == 0 && expression[iterator] == '-' || expression[iterator] == '/')
{
totalVal = new ExprNode(expression[iterator], totalVal, tNum);
}
}
count++;
}
if (isNumber(expression[iterator]) && count % 2 != 0)
{
tempNum += expression[iterator];
}
else if (isNumber(expression[iterator]) && count % 2 == 0)
{
tempNum2 += expression[iterator];
}
if (expression[iterator] == ' ' && count % 2 != 0)
{
tempNumInt = atoi (tempNum.c_str());
tNum = new ExprNode(tempNumInt);
tempNum = "";
count++;
}
else if (expression[iterator] == ' ' && count % 2 == 0)
{
tempNum2Int = atoi (tempNum2.c_str());
tNum2 = new ExprNode(tempNum2Int);
tempNum2 = "";
count++;
}
else if (expression[iterator] == ' ')
{
count++;
}
}
cout << totalVal->eval() << endl;
}

我会尽力解释任何不清楚的地方。提前致谢。

最佳答案

我并没有指出确切的错误,而是给你一个建议:int ExprNode::eval() const 不应该返回 'int'。这不足以处理变量结果,例如“11x”(这不能用简单的 int 表示)。您必须创建自己的结构来存储结果的整数部分和变量部分(最后一个是可选的)。

关于c++ - 给出错误答案的表达式树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5736014/

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