2位输入的表达式树

Expression tree for 2 digit inputs

本文关键字:表达式 输入 2位      更新时间:2023-10-16

我试图使一个表达式树,能够插入两位数。目前,由于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。