- #1
Kingyou123
- 98
- 0
Homework Statement
I'm trying to create a infix to prefix converter and prefix to infix converter. I have used a example in my textbook, but it's for a infix to postfix conversion, I figured if I reversed the equation it would give the prefix. Right idea?
2. The attempt at a solution
Code:
string infixToPrefix(string expression)
{ //FINISH ME!
stack<char> S; //holds operators
stack<char>output; //display like in class
string prefix = "";
char ch;
i = 0;
//remember to read backwards the expression
reverse(expression.begin(), expression.end()); //should be good
while (expression.length != '\0')
{
ch = expression[i];
i++;
s1[0] = ch;
s1[1] = '\0';
if (ch >= 'a' && ch <= 'z')
{
s.push(s1);
}
else
{
s.pop(s2);
s.pop(s3);
strcpy(temp, "(");
strcat(temp, s3);
strcat(temp, s1);
strcat(temp, s2);
strcat(temp, ")");
s.push(temp);
}
}
return prefix;
}
Rest of the code
Code:
#include <iostream>
#include <stack>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
string infixToPrefix(string expression);
string prefixToInfix(string expression);
int isp(char ch);
int icp(char ch);
bool isOperand(char C);
void menuOption(int &choice);
int main()
{
string expression;
string output;
int choice=-1;
menuOption(choice);
while(choice>=1){
cin.ignore();
switch(choice){
//FINISH ME!
case 1 :
cout << "Enter your infix expression: " << endl;
break;
//Add a switch statement that is for case 1
//case 1 is enter infix expression (for prefix)
//get the equtation and put it into expression variable
//call the infixToPrefix function and save the outcome in the output variable
case 2 :
cout << "Enter your prefix expression: " << endl;
break;
///Add a switch statement htat is for case 2
//case 2 is enter prefix expression (for infix)
// and follow the same steps like case 1 but for prefixToInfix
case 0:
cout << "you have quit application \n";
default:
cout << "please rekey..1 for infix to prefix, 2 for prefix to infix, or 0 to quit : ";
break;
//Add a default function that is Not a valide choice try again
//set choice to -1 again
//and then call menuOption(choice)
}
cout<<"Outcome of Conversion: "<<output<<endl;
menuOption(choice);
}
return 0;
}string prefixToInfix(string expression){
//FINISH ME!
string infix="";
stack<string> S;
string op1,op2,ch;
return infix;
}
string infixToPrefix(string expression)
{ //FINISH ME!
stack<char> S; //holds operators
stack<char>output; //display like in class
string prefix = "";
char ch;
i = 0;
//remember to read backwards the expression
reverse(expression.begin(), expression.end()); //should be good
while (expression.length != '\0')
{
ch = prefix[i];
i++;
s1[0] = ch;
s1[1] = '\0';
if (ch >= 'a' && ch <= 'z')
{
s.push(s1);
}
else
{
s.pop(s2);
s.pop(s3);
strcpy(temp, "(");
strcat(temp, s3);
strcat(temp, s1);
strcat(temp, s2);
strcat(temp, ")");
s.push(temp);
}
}
return prefix;
}
void menuOption(int &choice){
cout<<"\n\n===Menu=== \n";
//FINISH ME!
cout << " 1. Infix to prefix Conversion \n 2. Prefix to Infix Conversion \n 0 to quit \n";
//Look at the example output to know how to finish up this menu
cout<<"Choice: ";
cin >> choice;
}
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;
}
int isp(char ch){
switch(ch)
{
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '^': return 3;
case '(': return 0;
}
return -1;
}
int icp(char ch){
switch(ch)
{
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '^': return 3;
case '(': return 4;
}
return -1;
}
/*
string reverseString(string equation) // test if string is a valid return type
{
reverse(equation.begin(), equation.end());
return equation;
} //https://www.quora.com/How-can-I-reverse-a-string-or-a-sentence-in-C++
*/