Need help with Evaluating each function. I don't want to have to include spaces in the input either like is currently needed. Could you help
Need help with Evaluating each function. I don't want to have to include spaces in the input either like is currently needed.
Could you help me Evaluate each result of my function? I was thinking maybe convert infix/prefix to postfix then evaluate the postfix result.
Please add comments so I can see what was done. Thank you.
#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
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(char 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
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();
}
// evaluation = evaluate(myExpression); // cout << evaluation; return postfix;
}
string Expression::inToPre(string myExpression){
double evaluation; stack
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
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
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){ // infix to postfix then evaluate MUST BE NUMERIC DIGIT TO EVALUATE AND SPACES stack
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 // 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(); } 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 < } 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 << "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
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started