Write a C program that convert expressions that are in infix notation to postfix
ID: 668810 • Letter: W
Question
Write a C program that convert expressions that are in infix notation to postfix notation. The expressions might contain parentheses and these operators: +, -, *, /, =. In program #2, you will evaluate that postfix expression.
Error Handling
· Your code must handle errors like the following:
o Missing "("
o Missing ")"
o Additional errors are handled for extra credit.
· When an error is encountered, your code for convertToPostfix should return a non-zero value to the driver. Your program must not terminate.
The output should show a warning for each poorly formed expression, but do not terminate the program (simply skip to the next expression). Make certain you free up allocated memory (e.g., stack).
Explanation / Answer
Program code:
// infix.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
using namespace std;
const int MAX = 50 ;
//infix class
class infix
{
//instance variables
private :
char target[MAX], stack[MAX] ;
char *s, *t ;
int top ;
//instance methods
public :
infix( ) ;
void setexpr ( char *str ) ;
char* getTarget();
void push ( char c ) ;
char pop( ) ;
void convert( ) ;
int priority ( char c ) ;
void show( ) ;
} ;
//constructor of infix
infix :: infix( )
{
top = -1 ;
strcpy ( target, "" ) ;
strcpy ( stack, "" ) ;
t = target ;
s = "" ;
}
//definition of function setexpr
void infix :: setexpr ( char *str )
{
s = str ;
}
//definition of getTarget function
char* infix ::getTarget()
{
return target;
}
//definition of push function
void infix :: push ( char c )
{
if ( top == MAX )
cout << " Stack is full " ;
else
{
top++ ;
stack[top] = c ;
}
}
//definition of pop function
char infix :: pop( )
{
if ( top == -1 )
{
return -1 ;
}
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}
//definition of convert function
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 = '' ;
}
//definition of priority function
int infix :: priority ( char c )
{
if ( c == '^' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}
//definition of show function
void infix :: show( )
{
cout << target ;
}
//postfix class
class postfix
{
//instance variables
private :
int stack[MAX] ;
int top, nn ;
char *s ;
//instance methods
public :
postfix( ) ;
void setexpr ( char *str ) ;
void push ( int item ) ;
int pop( ) ;
void calculate( ) ;
void show(char ) ;
int getValue();
} ;
//constructor
postfix :: postfix( )
{
top = -1 ;
}
//definition of setexpr function
void postfix :: setexpr ( char *str )
{
s = str ;
}
//definition of push function
void postfix :: push ( int item )
{
if ( top == MAX - 1 )
cout << endl << "Stack is full" <<endl;
else
{
top++ ;
stack[top] = item ;
}
}
//definition of getValue function
int postfix::getValue()
{
return nn;
}
//definition of pop function
int postfix :: pop( )
{
if ( top == -1 )
{
return NULL ;
}
int data = stack[top] ;
top-- ;
return data ;
}
//definition of calculate function
void postfix :: calculate( )
{
int n1, n2, n3 ;
char ch, ch1;
while ( *s )
{
if ( *s == ' ' || *s == ' ' )
{
s++ ;
continue ;
}
if ( isdigit ( *s ) )
{
nn = *s - '0' ;
push ( nn ) ;
}
else
{
n1 = pop( ) ;
if(top==-1)
{
push(n1);
break;
}
n2 = pop( ) ;
switch ( *s )
{
case '+' :
n3 = n2 + n1 ;
break ;
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '=' :
n3 = n1;
break ;
default :
cout << "Unknown operator"<<endl ;
system("pause");
}
push( n3 ) ;
}
s++ ;
}
}
//definition of show function
void postfix :: show(char c)
{
nn = pop ( ) ;
if(c=='-')
nn=-1*nn;
if(c==' ')
nn=nn;
cout << " Result is: " << nn<<endl ;
}
//main function definition
void main( )
{
//declare the required variables
char expr[MAX] ;
ifstream myFile("InputText.txt");
//open the file if the file exist
if(myFile)
{
//get the line from the text file
myFile.getline ( expr, MAX ) ;
//loop till the pointer reaches the end of the file
while(!myFile.eof())
{
//declare two class objects
infix q ;
postfix p;
//get the expression
cout << " An expression in infix form: " ;
myFile.getline ( expr, MAX ) ;
cout<<expr<<endl;
q.setexpr ( expr ) ;
q.convert( ) ;
//print the postfix expression
cout << "The postfix expression is: " ;
q.show( ) ;
char *str=q.getTarget();
int length=strlen(str);
string strng(str);
char st[1024];
strng=strng.substr(0,strng.length());
strcpy(st, strng.c_str());
p.setexpr(st);
//calculate the value
p.calculate();
if(str[strlen(str)-1]=='-')
p.show('-');
else
p.show(' ');
cout<<" ------------------------------------------------"<<endl;
}
}
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.