There is some skeleton where we have to follow as part of our project so try and
ID: 3800102 • Letter: T
Question
There is some skeleton where we have to follow as part of our project so try and keep code without changing much.
The first function inToPost works perfectly. My main issue I am having is with my Infix to Prefix and Postfix to Infix.. "inToPre" , "postToIn" functions.
I have tried following Pseudo code but I cannot complete these two.
If you can help using comments that would be nice. I believe I can finish the rest if you help me with these two functions.
It must be done in C++.
Thank you.
#include
#include
#include
using namespace std;
class Expression{
public:
string inToPost(string);
string inToPre(string);
string postToIn(string);
string preToIn(string);
string convertThis; // Expression that we want converted
double evaluate(); // Evaluate 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();
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;{
}
}
string Expression::inToPost(string myExpression){
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();
}
return postfix;
}
string Expression::inToPre(string myExpression){
//
// stack operandStack;
// stack operatorStack;
// string prefix = ""; //make sure nothing is in string infix
// string rightOperand;
// string leftOperand;
// string opERAND;
// string opERATOR;
//
//
//
//
//
// for(int i = 0;i< myExpression.length();i++){ //While expression still remains, go through input
// if (myExpression[i] == ' ' || myExpression[i] == ',') continue;
//
//
// if(isOperand(myExpression[i])){
// operandStack.push(myExpression[i]);
// }
// else if (myExpression[i] = '(' || operatorStack.empty() || isHigherPrecedence(operatorStack.top(),myExpression[i])){
// operatorStack.push(myExpression[i]);
// }
//
// else if (myExpression[i] == ')'){
// while(!operatorStack.top() == '('){ // while not a left parenthesis
// opERATOR = operatorStack.top(); // operator
// operatorStack.pop();
// rightOperand = operandStack.top(); //Right operand
// operandStack.pop();
// leftOperand = operandStack.top(); //Left operand
// operandStack.pop();
// opERAND = opERATOR + leftOperand + rightOperand;
// }
// }
// operatorStack.pop(); // popping left parenthesis
//
//
//
// if (!isHigherPrecedence(operatorStack.top(),myExpression[i])){
//
// while(!operatorStack.empty()){
// opERATOR = operatorStack.top();
// operatorStack.pop();
// rightOperand = operandStack.top();
// operandStack.pop();
// leftOperand = operandStack.top();
// operandStack.pop();
// opERAND += opERATOR + leftOperand + rightOperand;
// }
//
// operatorStack.push(myExpression[i]);
// }
//
// while(!operatorStack.empty()){
// rightOperand = operandStack.top();
// operandStack.pop();
// leftOperand = operandStack.top();
// operandStack.pop();
//
// opERAND += opERATOR + leftOperand + rightOperand;
//
//
//
// }
// }
//
//
return prefix;
}
string Expression::postToIn(string myExpression){
stack Stack;
string infix = ""; // Initialize postfix as empty string.
string leftParenthesis = "(";
string rightParenthesis = ")";
string leftValue;
string rightValue;
string myOperator;
string currentinfix;
for(int i = 0;i< myExpression.length();i++){
if(isOperand(myExpression[i])){
Stack.push(myExpression[i]);
}
else if (isOperator(convertThis[i])){
rightValue = Stack.top();
Stack.pop();
leftValue = Stack.top();
Stack.pop();
myOperator = convertThis[i];
infix = leftParenthesis + leftValue + myOperator + rightValue + rightParenthesis;
}
//this function prints only 23+... but as soon as I go past this like abc+- for example we get errors.
}
return infix;
}
string Expression::preToIn(string expression){
}
double Expression::evaluate(){
}
void Expression::printResult(){
cout << postToIn(convertThis);
}
int main(){
string convertThis;
int choice;
string str;
cout << "|-----Here is my conversion menu-----|" << endl;
cout << "|----What are you converting to?-----|" << endl << endl;
cout << "1- Infix to postfix" << endl;
cout << "2- Infix to prefix" << endl;
cout << "3- postfix to infix?" << endl;
cout << "4- prefix to infix?" << endl;
//cin >> choice;
//cin.ignore();
cout << "Now enter the expression you want to convert ";
getline(cin,str);
Expression myexp(str, 1);
myexp.printResult();
}
Explanation / Answer
#include<iostream>
#include<stack>
#include<string>
string InfixToPostfix(string exp);
int HasHiPrecedence(char operator1, char operator2);
bool IsOperator(char C);
bool IsOperand(char C);
int main()
{
string exp;
cout<<"Enter Infix Exp ";
getline(cin,exp);
string postfix = InfixToPostfix(exp);
cout<<"Output = "<<postfix<<" ";
}
string InfixToPostfix(string exp)
{
stack<char> S;
string postfix = "";
for(int i = 0;i< exp.length();i++) {
if(exp[i] == ' ' || exp[i] == ',') continue;
else if(IsOperator(exp[i]))
{
while(!S.empty() && S.top() != '(' && HasHiPrecedence(S.top(),exp[i]))
{
postfix+= S.top();
S.pop();
}
S.push(exp[i]);
}
else if(IsOperand(exp[i]))
{
postfix +=exp[i];
}
else if (exp[i] == '(')
{
S.push(exp[i]);
}
else if(exp[i] == ')')
{
while(!S.empty() && S.top() != '(') {
postfix += S.top();
S.pop();
}
S.pop();
}
}
while(!S.empty()) {
postfix += S.top();
S.pop();
}
return postfix;
}
bool IsOperand(char C)
{
if(C >= '0' && C <= '9') return true;
if(C >= 'a' && C <= 'z') return true;
if(C >= 'A' && C <= 'Z') return true;
return false;
}
bool IsOperator(char C)
{
if(C == '+' || C == '-' || C == '*' || C == '/' || C== '$')
return true;
return false;
}
int IsRightAssociative(char op)
{
if(op == '$') return true;
return false;
}
int GetOperatorWeight(char op)
{
int weight = -1;
switch(op)
{
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
case '$':
weight = 3;
}
return weight;
}
int HasHiPrecedence(char op1, char op2)
{
int op1Weight = GetOperatorWeight(op1);
int op2Weight = GetOperatorWeight(op2);
if(op1Weight == op2Weight)
{
if(IsRightAssociative(op1)) return false;
else return true;
}
return op1Weight > op2Weight ? true: false;
}
#include<iostream>
#include<stack>
#include<string>
string InfixToPostfix(string exp);
int HasHiPrecedence(char operator1, char operator2);
bool IsOperator(char C);
bool IsOperand(char C);
int main()
{
string exp;
cout<<"Enter Infix Exp ";
getline(cin,exp);
string postfix = InfixToPostfix(exp);
cout<<"Output = "<<postfix<<" ";
}
string InfixToPostfix(string exp)
{
stack<char> S;
string postfix = "";
for(int i = 0;i< exp.length();i++) {
if(exp[i] == ' ' || exp[i] == ',') continue;
else if(IsOperator(exp[i]))
{
while(!S.empty() && S.top() != '(' && HasHiPrecedence(S.top(),exp[i]))
{
postfix+= S.top();
S.pop();
}
S.push(exp[i]);
}
else if(IsOperand(exp[i]))
{
postfix +=exp[i];
}
else if (exp[i] == '(')
{
S.push(exp[i]);
}
else if(exp[i] == ')')
{
while(!S.empty() && S.top() != '(') {
postfix += S.top();
S.pop();
}
S.pop();
}
}
while(!S.empty()) {
postfix += S.top();
S.pop();
}
return postfix;
}
bool IsOperand(char C)
{
if(C >= '0' && C <= '9') return true;
if(C >= 'a' && C <= 'z') return true;
if(C >= 'A' && C <= 'Z') return true;
return false;
}
bool IsOperator(char C)
{
if(C == '+' || C == '-' || C == '*' || C == '/' || C== '$')
return true;
return false;
}
int IsRightAssociative(char op)
{
if(op == '$') return true;
return false;
}
int GetOperatorWeight(char op)
{
int weight = -1;
switch(op)
{
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
case '$':
weight = 3;
}
return weight;
}
int HasHiPrecedence(char op1, char op2)
{
int op1Weight = GetOperatorWeight(op1);
int op2Weight = GetOperatorWeight(op2);
if(op1Weight == op2Weight)
{
if(IsRightAssociative(op1)) return false;
else return true;
}
return op1Weight > op2Weight ? true: false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.