Hello guys,
#include<stdio.h> void prefix(); void postfix(); char stack[50]; int s[50]; char post[50],pre[50]; int p = -1; int top = -1; int rtop = -1; int xtop=-1; xpush(int elem) { s[++xtop]=elem; } int xpop() { return(s[xtop--]); } void push(char x) { stack[++top] = x; } char pop() { if(top == -1) return -1; else return stack[top--]; } int priority(char x) { if(x == '(') return 0; if(x == '+' || x == '-') return 1; if(x == '*' || x == '/') return 2; } void main() { int choice; do { printf("\n\n----MENU----\n1.INFIX - POSTFIX\n2.INFIX - PREFIX\n3.EXIT\n"); printf("Enter your choice\n"); scanf("%d",&choice); switch(choice) { case 1 : postfix();break; case 2 : prefix();break; case 3 : printf("~~~~~ Thank You ~~~~~\n");break; default: printf("INVALID INPUT !!!!\n"); } }while(choice!=3); } void postfix() { p=-1; char exp[20]; char *e, x; printf("Enter the expression :: "); scanf("%s",exp); printf("\nPostfix expression :: "); e = exp; while(*e != '\0') { if(isalnum(*e)) { printf("%c",*e); post[++p]=*e; } else if(*e == '(') push(*e); else if(*e == ')') { while((x = pop()) != '(') { printf("%c", x); post[++p]=x; } } else { while(priority(stack[top]) >= priority(*e)) { char a; a= pop(); printf("%c",a); post[++p]=a; } push(*e); } e++; } while(top != -1) { char b; b=pop(); printf("%c",b); post[++p]=b; } int i=0,op1,op2; /*Evaluation*/ char ch; while(i != (p+1)) { ch=post[i]; i++; if(isdigit(ch)) xpush(ch-'0'); else { op2=xpop(); op1=xpop(); switch(ch) { case '+':xpush(op1+op2);break; case '-':xpush(op1-op2);break; case '*':xpush(op1*op2);break; case '/':xpush(op1/op2);break; } } } printf("\n\nResult after Evaluation: %d\n",s[xtop]); } void prefix() { xtop=-1;rtop=-1;p=-1; int i=0,j=0; char exp[50],rexp[50]; char *e,*r, x; printf("Enter the expression :: "); scanf("%s",exp); while(exp[i]!='\0') i++; i--; while(i>=0) { if(exp[i]==')') { rexp[j++]='('; i--; } else if(exp[i]=='(') { rexp[j++]=')'; i--; } else rexp[j++]=exp[i--]; } rexp[j]='\0'; r=rexp; while(*r != '\0') { if(isalnum(*r)) { pre[++p]=*r; } else if(*r=='(') { push(*r); } else if(*r==')') { while((x=pop())!='(') pre[++p]=x; } else { while(priority(stack[top])>=priority(*r)) pre[++p]=pop(); push(*r); } r++; } while(top != -1) { pre[++p]=pop(); } int print=p; printf("\nPrefix expression :: "); while(print!=-1) printf("%c",pre[print--]); /*evaluation*/ i=0; j=0; int op1,op2; char ch; while(i!=(p+1)) { i++; ch=pre[j++]; if(isdigit(ch)) xpush(ch-'0'); else { op1=xpop(); op2=xpop(); switch(ch) { case '+':xpush(op1+op2);break; case '-':xpush(op1-op2);break; case '*':xpush(op1*op2);break; case '/':xpush(op1/op2);break; } } } printf("\n\nResult after Evaluation: %d\n",s[xtop]); } /* OUTPUT ----MENU---- 1.INFIX - POSTFIX 2.INFIX - PREFIX 3.EXIT Enter your choice 1 Enter the expression :: 8*5*(10-3) Postfix expression :: 85*103-* Result after Evaluation: 280 ----MENU---- 1.INFIX - POSTFIX 2.INFIX - PREFIX 3.EXIT Enter your choice 2 Enter the expression :: 8*8*(10-3) Prefix expression :: *8*8-103 Result after Evaluation: 448 ----MENU---- 1.INFIX - POSTFIX 2.INFIX - PREFIX 3.EXIT Enter your choice 1 Enter the expression :: 5*5-4*(4-3) Postfix expression :: 55*443-*- Result after Evaluation: 21 ----MENU---- 1.INFIX - POSTFIX 2.INFIX - PREFIX 3.EXIT Enter your choice 2 Enter the expression :: 5*5-4*(4-3) Prefix expression :: -*55*4-43 Result after Evaluation: 21 ----MENU---- 1.INFIX - POSTFIX 2.INFIX - PREFIX 3.EXIT Enter your choice 10 INVALID INPUT !!!! ----MENU---- 1.INFIX - POSTFIX 2.INFIX - PREFIX 3.EXIT Enter your choice 3 ~~~~~ Thank You ~~~~~ */
No comments:
Post a Comment