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.
Receive a infix expression (Through the main file)
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
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.
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
{
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);
};