Home Forums C Programming please help me in finding out the bug in this program Re: Re: please help me in finding out the bug in this program

#3604
GWILouisaxwzkla
Participant

heres an infix to postfix program I have laying around:



#include < conio.h >
#include < math.h >
#include < string.h >
#include < stdlib.h >
#include < stdio.h >

enum Boolean { False = 0 , True = 1 } ;
const int GREATER = 1 ;
const int NOTGREATER = 0 ;
int cnt = 0 , cntcol = 0 , postcnt = 0 ;

//
struct Stack
{
int info ;
Stack * previous;
} * top;

//
void Push ( int number )
{
Stack * current;
current = new ( Stack ) ;
if ( current == 0 )
{
printf ( "n allocation error!!!!! " );
return;
}
current -> info = number ;
current -> previous = top ;
top = current ;
}

//
int Pop ()
{
int temp ;
Stack * current = top;
temp = current -> info ;
top = top -> previous ;
delete current ;
return temp ;
}

//
int StackTop ()
{
return top -> info ;
}

//
Boolean isEmpty()
{
if ( top == NULL )
return True;
else
return False ;
}

//
Boolean isOperand ( char ch )
{
if ( ch >= '0' && ch <= '9' ) //if is a number
return True;
else
return False ;
}

//
void Infix ( char infix[][10])
{
char c ;
while ( c != 13 )
{
c = getch() ;
putch(c);
if ( c >= '0' && c <= '9' )
{
infix [cnt][cntcol] = c ;
cntcol++ ;
}
else if ( c == '+'|| c == '-'|| c == '*'|| c == '/')
{
infix [cnt][cntcol] = '' ;
cnt++ ;
infix [cnt][0] = c ;
infix [cnt][1] = '' ;
cnt++ ;
cntcol = 0 ;
}
else if ( c == 13 )
infix[cnt][cntcol] = '' ;
}
}


//
int Calculate (int op1, char opr, int op2)
{
int ans ;
switch ( opr )
{
case '+' : ans = op1 + op2 ; break ;
case '-' : ans = op1 - op2 ; break ;
case '*' : ans = op1 * op2 ; break ;
case '/' : ans = op1 / op2 ; break ;
case '^' : ans = pow(op1, op2) ;
}
return ans ;
}
//
int Evaluate ( char postfix[][10] )
{
for ( int i = 0 ; i <= cnt ; i ++ )
{
char ch = postfix[0] ;
char ch1[10] ;
strcpy ( ch1 ,postfix
) ;
// cout < if ( isOperand ( ch ) == True )
Push ( atoi(ch1) );
else
{
int op2 = Pop () ;
int op1 = Pop () ;
int ans = Calculate ( op1, ch, op2 ) ;
// cout << ans << endl ;
Push ( ans ) ;
}
}
int ans = Pop () ;
return ans ;
}

//

int CheckP (char cur, char st)
{
int a = NOTGREATER ;

if ( cur == '(' )
a = GREATER ;
else if ( cur != '(' && st == '(' )
a = GREATER ;
else if ( cur != '(' && st != '(' )
{
if ( ( cur == '*' || cur == '/' ) && ( st == '+' || st == '-' ) )
a = GREATER ;
}
return a ;

}


//
void infixToPostfix ( char infix[][10], char postfix[][10] )
{
int postcol = 0 ;
for ( int i = 0 ; i <= cnt ; i ++ )
{
// strcpy(postfix, infix ) ;
// cout << postfix
;
char ch;
ch = infix
[0] ;
if ( isOperand ( ch ) == True )
strcpy ( postfix [postcnt++] , infix
) ;
else
{
if ( isEmpty() == True )
Push ( int(ch) ) ;
else
{
while ( CheckP( ch, StackTop() ) != GREATER && isEmpty() != True )
{
char c ;
if ( ch == ')' )
{
do
{
c = char ( Pop() ) ;
if ( c != '(')
{
postfix [postcnt][postcol] = c ;
postfix [postcnt][postcol+1] = '' ;
postcnt++ ;
}
}while ( c != '(' && isEmpty() != True);
break ;
}
else
c = char ( Pop() ) ;
if ( c != '(' && c != ')' )
{
postfix [postcnt][postcol] = c ;
postfix [postcnt][postcol+1] = '' ;
postcnt++ ;
}
}
if ( ch != ')' )
Push ( int(ch) ) ;
}
}
}

while ( isEmpty () != True )
{
char c = char ( Pop () ) ;
postfix [postcnt][postcol] = c ;
postfix [postcnt][postcol+1] = '';
postcnt++ ;
}
// postfix[postcnt][postcol] = '';
}

//

void Postfix ( char postfix[][10] )
{
char ch[10] ;
for ( int i = 0; i <= postcnt; i++ )
{
strcpy(ch , postfix ) ;
printf ( "%c" ,ch );
}
}

//

void main()
{
//clrscr();
top = NULL ;

char infix [ 50 ][ 10 ] ;
char postfix [ 50 ][ 10 ] ;

//gotoxy ( 5, 8 ) ;
printf ( "Enter Arithematic Expression ...................... : " );
Infix ( infix ) ;

infixToPostfix ( infix, postfix ) ;
//gotoxy ( 5, 10 ) ;
printf ( "Arithematic Expression in Postfix Notation......... : " );
Postfix ( postfix ) ;
int ans = Evaluate ( postfix ) ;
//gotoxy (5, 11 ) ;
printf ( "The Answer of Expression .......................... : " );
printf ( "%i", ans );

getch();
}
//