Hi i have this program that converts infix to postfix but i want to change it up
ID: 3547218 • Letter: H
Question
Hi i have this program that converts infix to postfix but i want to change it up to infix to prefix, im having trouble changing this code to work for i can use it as a infix to postfix or prefix program.
#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;
int priority(char a) {
int temp;
if (a == '^')
temp = 1;
else if (a == '*' || a == '/')
temp = 2;
else if (a == '+' || a == '-')
temp = 3;
return temp;
}
int main() {
string infix;
cout << "Enter an infix expression: " << endl;
getline(cin, infix);
stack<char> operator_stack;
stringstream output;
for (unsigned i = 0; i < infix.length(); i++) {
if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
while (!operator_stack.empty() && priority(operator_stack.top()) <= priority(infix[i])) {
output << operator_stack.top();
operator_stack.pop();
}
operator_stack.push(infix[i]);
} else if (infix[i] == '(') {
operator_stack.push(infix[i]);
} else if (infix[i] == ')') {
while (operator_stack.top() != '(') {
output << operator_stack.top();
operator_stack.pop();
}
operator_stack.pop();
} else {
output << infix[i];
}
}
while (!operator_stack.empty()) {
output << operator_stack.top();
operator_stack.pop();
}
cout <<"postfix : "<< output.str() << endl;
cin.ignore(numeric_limits<streamsize>::max(), ' ');
return 0;
}
Explanation / Answer
#include<iostream>
#include<conio.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<stack>
#include<ctype.h>
using namespace std;
bool isOperator(char ch);
// This function is to check
//whether given character in a infix expression
//string is a operator or not.
bool isOperator(char ch)
{
bool result = false;
if(ch == '+' ||ch == '-' ||ch == '/' ||ch == '*' || ch == '^')
{
result = true ;
}
return result;
}
// This function is to check
// whether operator from infix expression string
// is a higher proirity from operator stack or not.
// Oprator *, / has a same higher priority
// than oprator + -
// where as operator ^ has hightest priority among all.
bool isHigherPrio(char top, char index)
{
bool result = false;
if((top == '*' || top == '/') && (index == '+' || index == '-'))
{
result = true;
}
else
{
result = false;
}
if(top == '^')
{
result = true;
}
else if(index == '^')
{
result = false;
}
return result;
}
/*
This function is to check validation of
given infix expression
if all right paranthesis matches all
left paranthesis then its a valid expresion.
*/
bool validateExpression(string infixExpression)
{
stack<char>paranthesisStack;
bool result = false;
for(int index= infixExpression.length()-1; index >= 0; index--)
{
if(infixExpression[index]==')')
{
paranthesisStack.push(infixExpression[index]);
}
else if(infixExpression[index]=='(')
{
paranthesisStack.pop();
}
}
if(paranthesisStack.empty())
result = true;
return result;
}
int _tmain(int argc, _TCHAR* argv[])
{
stack<char>operatorStack;
stack<char>prefixResult;
string infixExpression;
do
{
cout<<"Enter an expression"<<endl;
cin>>infixExpression;
}
while(!validateExpression(infixExpression));
for(int index= infixExpression.length()-1; index >= 0; index--)
{
if(isalnum(infixExpression[index]))
{
prefixResult.push(infixExpression[index]);
}
else if( isOperator(infixExpression[index]) || (infixExpression[index]==')'))
{
while(!operatorStack.empty()&&isHigherPrio(operatorStack.top(),infixExpression[index]))
{
prefixResult.push(operatorStack.top());
operatorStack.pop();
}
operatorStack.push(infixExpression[index]);
}
}
else if(infixExpression[index] == '(')
{
char ch = operatorStack.top();
while(ch != ')')
{
prefixResult.push(operatorStack.top());
operatorStack.pop();
ch = operatorStack.top();
}
}
}
while(!operatorStack.empty())
{
if( ')' != operatorStack.top())
prefixResult.push(operatorStack.top());
operatorStack.pop();
}
while(!prefixResult.empty())
{
cout<<" "<<prefixResult.top();
prefixResult.pop();
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.