在不同的功能中使用相同的字符串

时间:2014-08-05 16:04:19

标签: c++ postfix-notation infix-notation

我正在研究如何将中缀转换为后缀表达式,然后计算转换后的后缀表达式。关于如何做到这一点,我有以下思考过程。

  1. 接收中缀表达式(通过主文件)

  2. 通过名为InstantiateVariable(char,int)的函数在此表达式中实例化变量

    a +(2 + 2)

    其中a = 2; 因此,必须先相应地添加这些值,然后才能将其解析并转换为后缀

  3. 收到中缀表达式并实例化变量后,我想将此中缀“转换”为 shuntingYard 函数中的后缀表达式。我已经成功完成了它的工作。

  4. 最后,我想计算/评估在我的shuntingYard函数中创建的这个后缀表达式。

  5. 所以基本上我想在更改后使用相同的字符串。因此导致我的问题:如何“捕获”或只是使用我在shuntingYard函数中创建的后缀字符串,以便我可以计算/评估该特定字符串。

    这样的事情:

    void instantiateVariable(char, int)
    {
        //receive the string through the main.cpp in the expression constructor
        //assign the values to the variables;
        a * (2 + 2); //set a = 3
        //Update, if you may, the "new" infix string
    }
    
    string shuntingYard(string)
    {
        //Receiving this updated infix string
        //Convert it
        return the postfix string; //The converted string
    }
    
    int evaluate(string)
    {
        //calculate the postfix string in the above shuntingYard function
        returns the postfix string's value;
    }
    

    以下是我正在使用的课程:

    class Expression
    {
    private:
        //string expression;
    public:
        /*The constructor receives a string
        expression in infix notation*/
        Expression(string);
    
        /*The destructor*/
        ~Expression();
    
        /*Sets a value for a variable in the expression.  The
        name of the variable (first parameter) and its value
        (second parameter) are passed to this function*/
    
        /*Apply values to the variables in the expression*/
        void instantiateVariable(char, int);
    
        /*Evaluates and returns the answer to the expression*/
        int evaluate(string);
    
        // Function to convert Infix expression to postfix
        string shuntingYard(string);
    
        /*Function to verify whether an operator has higher precedence over other*/
        int higherPrecedence(char, char);
    
        /*Function to verify whether a character is operator symbol or not*/
        bool IsOperator(char);
    
        /*Function to verify whether a character is alphanumeric character (letter or numeric digit) or not*/
        bool IsOperand(char);
    };
    

0 个答案:

没有答案