I need help the Data Structure in c++ error infixToPostfix here is my source cod
ID: 3928541 • Letter: I
Question
I need help the Data Structure in c++ error infixToPostfix
here is my source code
#include
#include
#include
using namespace std;
template
class infixToPostfix
{
string infx;
string pfx;
public:
infixToPostfix(string);
void getlnfix(string);
void showlnfix();
void convertToPostfix(stack&);
bool precedence(char, char);
void showPostfix();
};
template
infixToPostfix::infixToPostfix(string str)
{
getlnfix(str);
pfx = "";
}
template
void infixToPostfix::getlnfix(string str)
{
infx = str;
}
template
void infixToPostfix::showlnfix()
{
cout << "infix expression : " << infx << endl;
}
template
void infixToPostfix::convertToPostfix(stack &objStack)
{
int i = 0;
while (infx[i] !='/0')
{
switch (infx[i])
{
case'A':
case'B':
case'C':
case'D':
case'E':
case'F':
case'G':
case'H':
pfx += infx[i];
break;
case'(':
objStack.push(infx[i]);
break;
case')':
while (objStack.top() !='(')
{
pfx += objStack.top();
objStack.pop();
}
if (objStack.top() == '(')
objStack.pop();
break;
case'+':
case'-':
case'*':
case'/':
while (precedence(objStack.top(),infx[i]))
{
pfx += objStack.top();
objStack.pop();
}
objStack.push(infx[i]);
break;
}
i++;
}
}
template
bool infixToPostfix::precedence(char opr1, char opr2)
{
switch (opr1)
{
case'+':
if (opr2 == '+' || opr2 == '-')
return true;
else if (opr2 == '*' || opr2 == '/')
return false;
break;
case'-':
if (opr2 == '+' || opr2 == '-')
return true;
else if (opr2 == '*' || opr2 == '/')
return false;
break;
case'*':
if (opr2 == '+' || opr2 == '-')
return false;
else if (opr2 == '*' || opr2 == '/')
return true;
break;
case'/':
if (opr2 == '+' || opr2 == '-')
return false;
else if (opr2 == '*' || opr2 == '/')
return true;
break;
}
}
template
void infixToPostfix::showPostfix()
{
cout << "Equivalent Postfix expression :" << pfx ;
}
//////////////////////////////////////////
template
int main()
{
string str;
cout << "Enter the infix expression: ";
cin >> str;
infixToPostfix obj = new infixToPostfix(str);
stack objStack;
objStack.initializeStack();
obj.convertToPostfix(objStack);
obj.showPostfix();
return 0;
Explanation / Answer
Here is the error free code for you:
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
template<class T>
class infixToPostfix
{
string infx;
string pfx;
public:
infixToPostfix(string);
void getlnfix(string);
void showlnfix();
void convertToPostfix(stack<T>&);
bool precedence(char, char);
void showPostfix();
};
template<class T>
infixToPostfix<T>::infixToPostfix(string str)
{
getlnfix(str);
pfx = "";
}
template<class T>
void infixToPostfix<T>::getlnfix(string str)
{
infx = str;
}
template<class T>
void infixToPostfix<T>::showlnfix()
{
cout << "infix expression : " << infx << endl;
}
template<class T>
void infixToPostfix<T>::convertToPostfix(stack<T> &objStack)
{
int i = 0;
while (infx[i] !='')
{
switch (infx[i])
{
case'A':
case'B':
case'C':
case'D':
case'E':
case'F':
case'G':
case'H':
pfx += infx[i];
break;
case'(':
objStack.push(infx[i]);
break;
case')':
while (objStack.top() !='(')
{
pfx += objStack.top();
objStack.pop();
}
if (objStack.top() == '(')
objStack.pop();
break;
case'+':
case'-':
case'*':
case'/':
while (precedence(objStack.top(),infx[i]))
{
pfx += objStack.top();
objStack.pop();
}
objStack.push(infx[i]);
break;
}
i++;
}
}
template<class T>
bool infixToPostfix<T>::precedence(char opr1, char opr2)
{
switch (opr1)
{
case'+':
if (opr2 == '+' || opr2 == '-')
return true;
else if (opr2 == '*' || opr2 == '/')
return false;
break;
case'-':
if (opr2 == '+' || opr2 == '-')
return true;
else if (opr2 == '*' || opr2 == '/')
return false;
break;
case'*':
if (opr2 == '+' || opr2 == '-')
return false;
else if (opr2 == '*' || opr2 == '/')
return true;
break;
case'/':
if (opr2 == '+' || opr2 == '-')
return false;
else if (opr2 == '*' || opr2 == '/')
return true;
break;
}
return false;
}
template<class T>
void infixToPostfix<T>::showPostfix()
{
cout << "Equivalent Postfix expression :" << pfx ;
}
//////////////////////////////////////////
int main()
{
string str;
cout << "Enter the infix expression: ";
cin >> str;
infixToPostfix<char> obj(str);
stack<char> objStack;
//objStack.initializeStack();
obj.convertToPostfix(objStack);
obj.showPostfix();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.