Home › Forums › C Programming › please help me in finding out the bug in this program
- This topic has 2 replies, 2 voices, and was last updated 15 years, 3 months ago by GWILouisaxwzkla.
Viewing 2 reply threads
- AuthorPosts
- August 16, 2009 at 4:42 am #2222FaithMullinParticipant
- August 16, 2009 at 6:33 am #3603FaithMullinParticipant
no one is there to help me out…….
- August 16, 2009 at 3:26 pm #3604GWILouisaxwzklaParticipant
heres an infix to postfix program I have laying around:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266<br /><br />#include < conio.h ><br />#include < math.h ><br />#include < string.h ><br />#include < stdlib.h ><br />#include < stdio.h ><br /><br />enum Boolean { False = 0 , True = 1 } ;<br />const int GREATER = 1 ;<br />const int NOTGREATER = 0 ;<br />int cnt = 0 , cntcol = 0 , postcnt = 0 ;<br /><br />//<hr class="bbcode_rule" />struct Stack<br />{<br />int info ;<br />Stack * previous;<br />} * top;<br /><br />//<hr class="bbcode_rule" />void Push ( int number )<br />{<br />Stack * current;<br />current = new ( Stack ) ;<br />if ( current == 0 )<br />{<br />printf ( "n allocation error!!!!! " );<br />return;<br />}<br />current -> info = number ;<br />current -> previous = top ;<br />top = current ;<br />}<br /><br />//<hr class="bbcode_rule" />int Pop ()<br />{<br />int temp ;<br />Stack * current = top;<br />temp = current -> info ;<br />top = top -> previous ;<br />delete current ;<br />return temp ;<br />}<br /><br />//<hr class="bbcode_rule" />int StackTop ()<br />{<br />return top -> info ;<br />}<br /><br />//<hr class="bbcode_rule" />Boolean isEmpty()<br />{<br />if ( top == NULL )<br />return True;<br />else<br />return False ;<br />}<br /><br />//<hr class="bbcode_rule" />Boolean isOperand ( char ch )<br />{<br />if ( ch >= '0' && ch <= '9' ) //if is a number<br />return True;<br />else<br />return False ;<br />}<br /><br />//<hr class="bbcode_rule" />void Infix ( char infix[][10])<br />{<br />char c ;<br />while ( c != 13 )<br />{<br />c = getch() ;<br />putch(c);<br />if ( c >= '0' && c <= '9' )<br />{<br />infix [cnt][cntcol] = c ;<br />cntcol++ ;<br />}<br />else if ( c == '+'|| c == '-'|| c == '*'|| c == '/')<br />{<br />infix [cnt][cntcol] = '' ;<br />cnt++ ;<br />infix [cnt][0] = c ;<br />infix [cnt][1] = '' ;<br />cnt++ ;<br />cntcol = 0 ;<br />}<br />else if ( c == 13 )<br />infix[cnt][cntcol] = '' ;<br />}<br />}<br /><br /><br />//<hr class="bbcode_rule" />int Calculate (int op1, char opr, int op2)<br />{<br />int ans ;<br />switch ( opr )<br />{<br />case '+' : ans = op1 + op2 ; break ;<br />case '-' : ans = op1 - op2 ; break ;<br />case '*' : ans = op1 * op2 ; break ;<br />case '/' : ans = op1 / op2 ; break ;<br />case '^' : ans = pow(op1, op2) ;<br />}<br />return ans ;<br />}<br />//<hr class="bbcode_rule" />int Evaluate ( char postfix[][10] )<br />{<br />for ( int i = 0 ; i <= cnt ; i ++ )<br />{<br />char ch = postfix<i>[0] ;<br />char ch1[10] ;<br />strcpy ( ch1 ,postfix </i><i> ) ;<br />// cout <<endl<< atoi ( ch1) ;<br />if ( isOperand ( ch ) == True )<br />Push ( atoi(ch1) );<br />else<br />{<br />int op2 = Pop () ;<br />int op1 = Pop () ;<br />int ans = Calculate ( op1, ch, op2 ) ;<br />// cout << ans << endl ;<br />Push ( ans ) ;<br />}<br />}<br />int ans = Pop () ;<br />return ans ;<br />}<br /><br />// </i><hr class="bbcode_rule" />int CheckP (char cur, char st)<br />{<br />int a = NOTGREATER ;<br /><br />if ( cur == '(' )<br />a = GREATER ;<br />else if ( cur != '(' && st == '(' )<br />a = GREATER ;<br />else if ( cur != '(' && st != '(' )<br />{<br />if ( ( cur == '*' || cur == '/' ) && ( st == '+' || st == '-' ) )<br />a = GREATER ;<br />}<br />return a ;<br /><br />}<br /><br /><br />//<hr class="bbcode_rule" />void infixToPostfix ( char infix[][10], char postfix[][10] )<br />{<br />int postcol = 0 ;<br />for ( int i = 0 ; i <= cnt ; i ++ )<br />{<br />// strcpy(postfix<i>, infix </i><i>) ;<br />// cout << postfix</i><i> ;<br />char ch;<br />ch = infix</i><i>[0] ;<br />if ( isOperand ( ch ) == True )<br />strcpy ( postfix [postcnt++] , infix </i><i> ) ;<br />else<br />{<br />if ( isEmpty() == True )<br />Push ( int(ch) ) ;<br />else<br />{<br />while ( CheckP( ch, StackTop() ) != GREATER && isEmpty() != True )<br />{<br />char c ;<br />if ( ch == ')' )<br />{<br />do<br />{<br />c = char ( Pop() ) ;<br />if ( c != '(')<br />{<br />postfix [postcnt][postcol] = c ;<br />postfix [postcnt][postcol+1] = '' ;<br />postcnt++ ;<br />}<br />}while ( c != '(' && isEmpty() != True);<br />break ;<br />}<br />else<br />c = char ( Pop() ) ;<br />if ( c != '(' && c != ')' )<br />{<br />postfix [postcnt][postcol] = c ;<br />postfix [postcnt][postcol+1] = '' ;<br />postcnt++ ;<br />}<br />}<br />if ( ch != ')' )<br />Push ( int(ch) ) ;<br />}<br />}<br />}<br /><br />while ( isEmpty () != True )<br />{<br />char c = char ( Pop () ) ;<br />postfix [postcnt][postcol] = c ;<br />postfix [postcnt][postcol+1] = '';<br />postcnt++ ;<br />}<br />// postfix[postcnt][postcol] = '';<br />}<br /><br />//</i><hr class="bbcode_rule" />void Postfix ( char postfix[][10] )<br />{<br />char ch[10] ;<br />for ( int i = 0; i <= postcnt; i++ )<br />{<br />strcpy(ch , postfix<i> ) ;<br />printf ( "%c" ,ch );<br />}<br />}<br /><br />// </i><hr class="bbcode_rule" />void main()<br />{<br />//clrscr();<br />top = NULL ;<br /><br />char infix [ 50 ][ 10 ] ;<br />char postfix [ 50 ][ 10 ] ;<br /><br />//gotoxy ( 5, 8 ) ;<br />printf ( "Enter Arithematic Expression ...................... : " );<br />Infix ( infix ) ;<br /><br />infixToPostfix ( infix, postfix ) ;<br />//gotoxy ( 5, 10 ) ;<br />printf ( "Arithematic Expression in Postfix Notation......... : " );<br />Postfix ( postfix ) ;<br />int ans = Evaluate ( postfix ) ;<br />//gotoxy (5, 11 ) ;<br />printf ( "The Answer of Expression .......................... : " );<br />printf ( "%i", ans );<br /><br />getch();<br />}<br />//<hr class="bbcode_rule" />
- AuthorPosts
Viewing 2 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.