Home › Forums › C Programming › plzzz help…….it doesn’t evaluate right……!
- This topic has 3 replies, 4 voices, and was last updated 3 years, 11 months ago by M. Saqib.
Viewing 3 reply threads
- AuthorPosts
- March 3, 2010 at 6:31 pm #2244WeldonGarrisonParticipantC123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252#include <stdio.h>#include <ctype.h>#define MAX 50#define EMPTY -1struct stack{int data[MAX];int top;};int isempty(struct stack *s){return (s->top == EMPTY) ? 1 : 0;}void emptystack(struct stack* s){s->top = EMPTY;}void push(struct stack* s,int item){if(s->top == (MAX-1)){printf("nSTACK FULL");}else{++s->top;s->data[s->top]=item;}}int pop(struct stack* s){int ret=EMPTY;if(s->top == EMPTY)printf("nSTACK EMPTY");else{ret= s->data[s->top];--s->top;}return ret;}void display(struct stack s){while(s.top != EMPTY){printf("n%d",s.data[s.top]);s.top--;}}int isoperator(char e){if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%')return 1;elsereturn 0;}int priority(char e){int pri = 0;if(e == '*' || e == '/' || e =='%')pri = 2;else{if(e == '+' || e == '-')pri = 1;}return pri;}int evaluate(char *postfix){char *p;struct stack stk;int op1,op2,result;emptystack(&stk);p = &postfix[0];while(*p != ''){/* removes tabs and spaces */while(*p == ' ' || *p == 't'){p++;}/* if is digit */if(isdigit(*p)){push(&stk,*p - 48);}else{/* it is an operator */op2 = pop(&stk);op1 = pop(&stk);switch(*p){case '+':result = op1 + op2;break;case '-':result = op1 - op2;break;case '/':result = op1 / op2;break;case '*':result = op1 * op2;break;case '%':result = op1 % op2;break;default:printf("nInvalid Operator");return 0;}push(&stk,result);}p++;}result = pop(&stk);return result;}void infix2postfix(char* infix, char * postfix, int insertspace){char *i,*p;struct stack X;char n1;emptystack(&X);i = &infix[0];p = &postfix[0];while(*i){while(*i == ' ' || *i == 't'){i++;}if( isdigit(*i) || isalpha(*i) ){while( isdigit(*i) || isalpha(*i)){*p = *i;p++;i++;}/*SPACE CODE*/if(insertspace){*p = ' ';p++;}/*END SPACE CODE*/}if( *i == '(' ){push(&X,*i);i++;}if( *i == ')'){n1 = pop(&X);while( n1 != '(' ){*p = n1;p++;/*SPACE CODE*/if(insertspace){*p = ' ';p++;}/*END SPACE CODE*/n1 = pop(&X);}i++;}if( isoperator(*i) ){if(isempty(&X))push(&X,*i);else{n1 = pop(&X);while(priority(n1) >= priority(*i)){*p = n1;p++;/*SPACE CODE*/if(insertspace){*p = ' ';p++;}/*END SPACE CODE*/n1 = pop(&X);}push(&X,n1);push(&X,*i);}i++;}}while(!isempty(&X)){n1 = pop(&X);*p = n1;p++;/*SPACE CODE*/if(insertspace){*p = ' ';p++;}/*END SPACE CODE*/}*p = '';}int main(void){char infix[MAX], postfix[MAX], result[MAX];printf("Enter expression: ");fflush(stdin);gets(infix);infix2postfix(&infix[0],&postfix[0],1);printf("nPostfix expression is: %s",&postfix[0]);result[0]=postfix[0];printf("n %s = %d.",&infix[0],evaluate(&result[0]));return 0;}
- March 5, 2010 at 11:20 pm #3642GWILouisaxwzklaParticipant
Heres a postfix to infix program I have on my drive ( hope this helps….. ):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264#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" /> - April 5, 2012 at 3:10 pm #3643PSBMaira490593Participant
Hi,
please run it again in new window and check the variable that you have declared are right or not.
thanks.
Debra Fine - January 24, 2021 at 3:10 am #8960M. SaqibKeymaster
OK
- AuthorPosts
Viewing 3 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.