I am working on how to convert a infix to postfix expression and then calculate that converted postfix expression. I have the following thought process on how to do this.

  1. Receive a infix expression (Through the main file)

  2. Instantiate the variables in this expression through a function called InstantiateVariable(char, int)

a + (2 + 2)

where a = 2; THUS the values must be added accordingly before it can be parsed and converted into the postfix

  1. After the infix expression is received and the variables are instantiated, I want to "convert" this infix into a postfix expression in my shuntingYard function. Which I have managed to do and its working.

  2. Lastly, I want to calculate/evaluate this postfix expression that was created in my shuntingYard function.

So basically I want to use the same string after it changes. Thus leading to my question: how can I "capture" or simply get to use the postfix string I've created in my shuntingYard function so that I can calculate/evaluate that specific string.

Something like this:

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;

Here is my class I am using:

class Expression
    //string expression;
    /*The constructor receives a string
    expression in infix notation*/

    /*The destructor*/

    /*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);

You have a few options.

You can return a format string of sorts where the string is encoded with meta information that allows you to assign values in a transformation. Think of how printf format strings work but with labels. For example:

expr_string = ":a + ( 2 + 2 )"

would be returned and when you want to use it you do the transformation. Something like (in pseudocode):

transform (expr_string, "a=3") => "3 + ( 2 + 2 )"

That is a trivial (and incomplete) example but gives the flavor of what I am suggesting.

A better solution, however, is to not return a string at all. Return an object that represents an expression with variables in it. Then expose an API that allows you to assign values to those variables and then evaluate the expression.

struct Expr {
   double evaluate ();
   void assign (const std::string& var, double val);
   // .. rest of implementation

That way you can reuse the object as many times as you want (or copy it, or generate a new one, ...) and the interface hides all the gory details about how you are managing the symbol table and transformations behind the scene.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.