Question: I am posting my full c++ file. I am stuck on my evaluating expressions function ... double evaluate();Basically we are suppose to convert infix /prefix/postfix
I am posting my full c++ file. I am stuck on my evaluating expressions function ... double evaluate();Basically we are suppose to convert infix /prefix/postfix and evaluate. I just don't know how I can implement an evaluate function for the result of infix/post/pre. I was thinking converting them all to postfix but my evaluate currently requires spaces.. which I don't want. And I am unsure how to do that with my functions.
If you help maybe add comments so I see what was done.
#include #include #include #include #define flag '#' #include
using namespace std;
class Expression{ public:
string inToPost(string);//done string postToIn(string);//done string inToPre(string); string preToIn(string); string convertThis; // Expression that we want converted double evaluate(string); // Evaluate numeric expression string evaluated;
Expression(string input, int direction); //constructor
bool isOperator(char character); bool isOperand(char character); int isHigherWeight(char character); bool isHigherPrecedence(char op1, char op2); void printResult(int choice);
private: string infix; string postfix; string prefix;
};
//Constructor function Expression::Expression(string input, int direction){ convertThis = input; switch (direction){ case 1: infix = input; case 2: postfix = input; case 3: prefix = input;
} }
//Operator Function checks to see if character is a legal symbol bool Expression::isOperator(char character){ if((character == '*')||(character == '+')||(character == '-')||(character == '/')) return true; else return false; }
//Operand Function checks to see if character is a legal character bool Expression::isOperand(char character){ if(character >= 'a' && character <= 'z') return true; if(character >= 'A' && character <= 'Z') return true; if(character >= '0' && character <= '9') return true; else return false; }
//Function determines the weight of Operator. int Expression::isHigherWeight(char character){ int weight = -1; // switch(character){ case '+': case '-': weight = 1; case '*': case '/': weight = 2; } return weight; }
//Function that compares weights of two different Operators. bool Expression::isHigherPrecedence(char oper1, char oper2){ int op1Weight = isHigherWeight(oper1); int op2Weight = isHigherWeight(oper2);
// If operators have equal precedence, return true // return false if (op1Weight == op2Weight){ return true; }
return op1Weight > op2Weight ? true: false;{
}
}
//*************EVALUATE FUNCTIONS****************
int doOperation(int operation, int operand1,int operand2) { if(operation == '+') return operand1 + operand2; else if(operation == '-') return operand1 - operand2; else if(operation == '*') return operand1 * operand2; else if(operation == '/') return operand1 / operand2; else cout<<"Something went wrong "; return -1; }
bool isANumber(char character) { if(character >= '0' && character <= '9') return true; return false; }
string Expression::inToPost(string myExpression){ //THIS FUNCTION WORKS
double evaluation; stack Stack; string postfix = ""; // Initialize postfix as empty string.
for(int i = 0;i< myExpression.length();i++){ //go through array of string convertThis
if (myExpression[i] == ' ' || myExpression[i] == ',') continue;
else if(isOperator(myExpression[i])){ while(!Stack.empty() && Stack.top() != '(' && isHigherPrecedence(Stack.top(),myExpression[i])){ postfix += Stack.top(); //appending stack char to postfix string Stack.pop(); } Stack.push(myExpression[i]); }
else if(isOperand(myExpression[i])){ postfix += myExpression[i]; //appending expression at pos[i] to postfix string }
else if(myExpression[i] == '('){ Stack.push(myExpression[i]); } else if (myExpression[i] == ')'){
while(!Stack.empty() && Stack.top() != '('){ postfix += Stack.top(); //appending stack char to postfix string Stack.pop(); } Stack.pop(); }
}
while(!Stack.empty()){ postfix += Stack.top(); //appending stack char to postfix string Stack.pop();
}
evaluated = postfix; evaluate(evaluated); return postfix;
}
string Expression::inToPre(string myExpression){
double evaluation; stack Stack; string prefix = ""; // Initialize postfix as empty string. int length;
length = myExpression.length(); for(int i = length -1; i >=0 ;i--){ //go through array of string convertThis
if (myExpression[i] == ' ' || myExpression[i] == ',') continue;
else if(isOperator(myExpression[i])){ while(!Stack.empty() && Stack.top() != ')' && isHigherPrecedence(Stack.top(),myExpression[i])){ prefix += Stack.top(); //appending stack char to postfix string Stack.pop(); } Stack.push(myExpression[i]); }
else if(isOperand(myExpression[i])){ prefix += myExpression[i]; //appending expression at pos[i] to postfix string }
else if(myExpression[i] == ')'){ Stack.push(myExpression[i]); } else if (myExpression[i] == '('){
while(!Stack.empty() && Stack.top() != ')'){ prefix += Stack.top(); //appending stack char to postfix string Stack.pop(); } Stack.pop(); }
}
while(!Stack.empty()){ prefix += Stack.top(); //appending stack char to postfix string Stack.pop();
} reverse(prefix.begin(), prefix.end()); //reverse complete string to form prefix // evaluation = evaluate(myExpression); // cout << evaluation;
return prefix; }
string Expression::postToIn(string myExpression){ //THIS FUNCTION WORKS.
stack Stack; string infix = ""; // Initialize postfix as empty string. string leftParenthesis = "("; string rightParenthesis = ")"; string leftValue; string rightValue; string myOperator; string currentinfix; bool leftDone = true; // leftDone if false means left value needs to be initialised. bool rightDone = false; // rightDone true means rightDone already has a value hence leftvalue should go to stack , right value to //leftvalue and new value to right value in case of operand. leftValue = myExpression[0]; for(int i = 1;i< myExpression.length();i++){
if (isOperand(myExpression[i])){ if(leftDone){ if(rightDone){ Stack.push(leftValue); leftValue = rightValue; rightValue = myExpression[i];
}else{ rightValue = myExpression[i]; rightDone = true; } }else{ leftDone = myExpression[i]; leftDone = true; }
}else{ if(rightDone){
leftValue = leftParenthesis + leftValue + myExpression[i] + rightValue + rightParenthesis; rightDone = false; } else{
rightValue = leftValue; leftValue = Stack.top(); Stack.pop(); leftValue = leftParenthesis + leftValue + myExpression[i] + rightValue + rightParenthesis; }
}
} return leftValue; }
string Expression::preToIn(string myExpression){
// first convert prefix to postfix then postfix to infix
stack Stack; string infix = ""; // Initialize postfix as empty string. string leftParenthesis = "("; string rightParenthesis = ")"; string leftValue; string rightValue; string myOperator; string currentinfix; bool leftDone = true; // leftDone if false means left value needs to be initialised. bool rightDone = false; // rightDone true means rightDone already has a value hence leftvalue should go to stack , right value to int length; //leftvalue and new value to right value in case of operand. leftValue = myExpression[0]; length = myExpression.length(); for(int i = length -1; i >=0 ;i--){
if (isOperand(myExpression[i])){ if(leftDone){ if(rightDone){ Stack.push(leftValue); leftValue = rightValue; rightValue = myExpression[i];
}else{ rightValue = myExpression[i]; rightDone = true; } }else{ leftDone = myExpression[i]; leftDone = true; }
}else{ if(rightDone){
leftValue = rightParenthesis + leftValue + myExpression[i] + rightValue + leftParenthesis; rightDone = false; } else{
rightValue = leftValue; leftValue = Stack.top(); Stack.pop(); leftValue = rightParenthesis + leftValue + myExpression[i] + rightValue + leftParenthesis; }
}
} reverse(leftValue.begin(), leftValue.end()); return leftValue; }
double Expression::evaluate(string myExpression){ // postfix valuate MUST BE NUMERIC DIGIT TO EVALUATE **I DONT WANT TO HAVE TO USE SPACES** stack Stack; //declaring stack of int, as we are only dealing with numbers
for(int i = 0;i< myExpression.length();i++) { //if theres a space or comma.. keep going. if(myExpression[i] == ' ' || myExpression[i] == ',') continue; else if(isOperator(myExpression[i])) { //Expression is postfix so if we run into Operator pop two operands and perform operation // Pop two operands. int operand2 = Stack.top(); Stack.pop(); int operand1 = Stack.top(); Stack.pop(); // Perform operation int result = doOperation(myExpression[i],operand1,operand2); //Push back result of operation on stack. Stack.push(result); }
else if(isANumber(myExpression[i])){ // Extract the numeric operand from the string // Keep incrementing i as long as you are getting a numeric digit. int operand = 0; while(i // For a number with more than one digits, as we are scanning from left to right. // Everytime , we get a digit towards right, we can multiply current total in operand by 10 // and add the new digit. operand = (operand*10) + (myExpression[i] - '0'); i++; } // Finally, you will come out of while loop with i set to a non-numeric character or end of string // decrement i because it will be incremented in increment section of loop once again. // We do not want to skip the non-numeric character by incrementing i twice. i--;
// Push operand on stack. Stack.push(operand); } } // If expression is in correct format, Stack will finally have one element. This will be the output. return Stack.top();
cout << evaluated; }
void Expression::printResult(int choice){
cout << "What is your choice?" << endl; cin >> choice; cout << "******************************************************************************" << endl << endl; if (choice == 1) cout << "The conversion from Infix to Postfix is: " << inToPost(convertThis) << endl < else if (choice == 2) cout << "The conversion from Infix to Prefix is: " << inToPre(convertThis) << endl << endl << "******************************************************************************" << endl; else if (choice == 3) cout << "The conversion from Postfix to Infix is: " << postToIn(convertThis) << endl << endl << "******************************************************************************" << endl; else if (choice == 4) cout << "The conversion from Prefix to Infix is: " << preToIn(convertThis) << endl << endl << "******************************************************************************" << endl; else if (choice == 5) cout << "The Evaluation of your Postfix expression equals: " << evaluate(convertThis) << endl <
}
int main(){
string convertThis;
int choice;
string str;
cout << " Conversion Choice Menu " << endl;
cout << "______________________________________________" << endl << endl; cout << "[1]Infix to Postfix? -> No spaces or parenthesis needed!" << endl; cout << "[2]Infix to Prefix? -> No spaces or parenthesis needed!"<< endl; cout << "[3]Postfix to Infix? -> No spaces! " << endl; cout << "[4]Prefix to Infix? -> No spaces! " << endl; cout << "[5]Evaluate a Postfix Expression? -> Has to be numbers with spaces in between!" < cout << "______________________________________________" << endl << endl; //int choice; //cin.ignore();
cout << "Enter the expression you want to convert: "; getline(cin,str);
Expression myexp(str, 1); myexp.printResult(choice);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
