Write an infix to postfix converter as described in chapter 6. Use code from cha
ID: 3673525 • Letter: W
Question
Write an infix to postfix converter as described in chapter 6. Use code from chapter 7 to implement a stack (can be array based or linked based).
Assume:
1. variables are single character from a to z (lowercase)
2. There are no unary minus or plus
3. There are no spaces
Put in error detection that would detect variables together (like bb+c), operators together (like b++c), or mismatched parenthesis. It is recommended to use a Boolean, like validExp, to signal that the expression is incorrect. Also recommended to use another Boolean to keep track of the type of character gotten from the string, like previousOperand for the fact that a variable had been processed and the next should be an operator: note, with parenthesis, check the previousOperand but do NOT set it. For instance, (a+b) is valid while a(+b) is not – checking that the previousOperand is true would generate an error for a(+b).
The program should input the infix expression, then either print an error or print the postfix expression. USE C++
Textbook is DATA ABSTRACTION AND PROBLEM SOLVING WITH C++
Explanation / Answer
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
const int MAX = 50 ;
class infix
{
private :
char target[MAX], stack[MAX] ;
char *s, *t ;
int top ;
public :
infix( ) ;
void setexpr ( char *str ) ;
void push ( char c ) ;
char pop( ) ;
void convert( ) ;
int priority ( char c ) ;
void show( ) ;
} ;
infix :: infix( )
{
top = -1 ;
strcpy ( target, "" ) ;
strcpy ( stack, "" ) ;
t = target ;
s = "" ;
}
void infix :: setexpr ( char *str )
{
s = str ;
}
void infix :: push ( char c )
{
if ( top == MAX )
cout << " Stack is full " ;
else
{
top++ ;
stack[top] = c ;
}
}
char infix :: pop( )
{
if ( top == -1 )
{
cout << " Stack is empty " ;
return -1 ;
}
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}
void infix :: convert( )
{
while ( *s )
{
if ( *s == ' ' || *s == ' ' )
{
s++ ;
continue ;
}
if ( isdigit ( *s ) || isalpha ( *s ) )
{
while ( isdigit ( *s ) || isalpha ( *s ) )
{
*t = *s ;
s++ ;
t++ ;
}
}
if ( *s == '(' )
{
push ( *s ) ;
s++ ;
}
char opr ;
if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '-' || *s == '$' )
{
if ( top != -1 )
{
opr = pop( ) ;
while ( priority ( opr ) >= priority ( *s ) )
{
*t = opr ;
t++ ;
opr = pop( ) ;
}
push ( opr ) ;
push ( *s ) ;
}
else
push ( *s ) ;
s++ ;
}
if ( *s == ')' )
{
opr = pop( ) ;
while ( ( opr ) != '(' )
{
*t = opr ;
t++ ;
opr = pop( ) ;
}
s++ ;
}
}
while ( top != -1 )
{
char opr = pop( ) ;
*t = opr ;
t++ ;
}
*t = '' ;
}
int infix :: priority ( char c )
{
if ( c == '$' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}
void infix :: show( )
{
cout << target ;
}
int main( )
{
char expr[MAX] ;
infix q ;
cout << " Enter an expression in infix form: " ;
cin.getline ( expr, MAX ) ;
q.setexpr ( expr ) ;
q.convert( ) ;
cout << " The postfix expression is: " ;
q.show( ) ;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.