2位输入的表达式树
Expression tree for 2 digit inputs
我试图使一个表达式树,能够插入两位数。目前,由于char的功能,它只能插入数字0 - 9。我当前的代码适用于后缀表达式,所以当我为"+52"创建表达式树时,它的计算结果为10。如果我想为10 * 2创建一个表达式,它将不起作用,因为我的代码将其视为3位数字和一个操作符。
下面是我的代码:/*
* C++ Program to Implement Expression Tree
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
/** class TreeNode **/
class TreeNode
{
public:
char data;
TreeNode *left, *right;
/** constructor **/
TreeNode(char data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
/** class StackNode **/
class StackNode
{
public:
TreeNode *treeNode;
StackNode *next;
/** constructor **/
StackNode(TreeNode *treeNode)
{
this->treeNode = treeNode;
next = NULL;
}
};
class ExpressionTree
{
private:
StackNode *top;
public:
/** constructor **/
ExpressionTree()
{
top = NULL;
}
/** function to clear tree **/
void clear()
{
top = NULL;
}
/** function to push a node **/
void push(TreeNode *ptr)
{
if (top == NULL)
top = new StackNode(ptr);
else
{
StackNode *nptr = new StackNode(ptr);
nptr->next = top;
top = nptr;
}
}
/** function to pop a node **/
TreeNode *pop()
{
if (top == NULL)
{
cout<<"Underflow"<<endl;
}
else
{
TreeNode *ptr = top->treeNode;
top = top->next;
return ptr;
}
}
/** function to get top node **/
TreeNode *peek()
{
return top->treeNode;
}
/** function to insert character **/
void insert(char val)
{
if (isDigit(val))
{
TreeNode *nptr = new TreeNode(val);
push(nptr);
}
else if (isOperator(val))
{
TreeNode *nptr = new TreeNode(val);
nptr->left = pop();
nptr->right = pop();
push(nptr);
}
else
{
cout<<"Invalid Expression"<<endl;
return;
}
}
/** function to check if digit **/
bool isDigit(char ch)
{
return ch >= '0' && ch <= '9';
}
/** function to check if operator **/
bool isOperator(char ch)
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
/** function to convert character to digit **/
int toDigit(char ch)
{
return ch - '0';
}
/** function to build tree from input */
void buildTree(string eqn)
{
for (int i = eqn.length() - 1; i >= 0; i--)
insert(eqn[i]);
}
/** function to evaluate tree */
double evaluate()
{
return evaluate(peek());
}
/** function to evaluate tree */
double evaluate(TreeNode *ptr)
{
if (ptr->left == NULL && ptr->right == NULL)
return toDigit(ptr->data);
else
{
double result = 0.0;
double left = evaluate(ptr->left);
double right = evaluate(ptr->right);
char op = ptr->data;
switch (op)
{
case '+':
result = left + right;
break;
case '-':
result = left - right;
break;
case '*':
result = left * right;
break;
case '/':
result = left / right;
break;
default:
result = left + right;
break;
}
return result;
}
}
};
/** Main Contains menu **/
int main()
{
string s;
cout<<"Expression Tree Test"<<endl;
ExpressionTree et;
et.buildTree("*52");
cout<<"nnEvaluated Result : "<<et.evaluate();
}
如何使代码能够处理和计算两位数?
首先,'+ 5 2'称为前缀,而不是后缀表示法。我不确定你是否想转换你的代码来处理中缀表达式(5+2),这将需要你重写整个东西,或前缀表达式与多个数字(+ 10 5),在任何一种情况下,你可能想要考虑输入字符串中的空白。
你应该改变Treenode。data应该是int而不是char,因为实际上你希望"+ 10 5"被解析为总共三个treenode,其中一个包含"10"。
通常的方法是首先将输入字符串拆分为令牌,然后再将令牌集合传递给buildTree。输入字符串"+ 10 5"必须首先变成包含字符串"+","10","5"的向量/数组。然后,当您将这个集合传递给buildTree时,每个元素将成为一个新的TreeNode。
除此之外,查看代码中的stackknode类似乎是多余的;你应该能够做任何你想要的只有TreeNode。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何获取用户输入的算术表达式,如 5-8+7*4-8+9 或 1+5?
- C++如何使用表达式获取用户输入?
- 如何使用正则表达式将输入数字格式化为字符串
- C++:Lambda 表达式的输入
- 如何用键盘输入数学表达式
- 正则表达式在开头单独匹配单词,或者在输入结束时通过不重复字符串附加到某些内容但附加到某些内容
- 通过getline()输入,正则表达式无法正常工作
- getline 和在类函数中传递输入流,主表达式错误
- 检查输入字符串是否为数字且为C++,如果是,则将其转换为 int(正则表达式?
- 使用正则表达式进行输入验证
- 使用正则表达式检查输入的有效性
- 正则表达式的输入掩码
- 正则表达式库,用于维护状态,逐个字符接受输入,并在找到匹配时返回true
- 2位输入的表达式树
- 使用正则表达式验证输入
- 从输入中导出最小正则表达式
- 缓存重复输入数据的表达式求值