From ae27da1ce4c4ab0cc18f05d567e6438b35bb757d Mon Sep 17 00:00:00 2001 From: ankur2811 <56405200+ankur2811@users.noreply.github.com> Date: Thu, 10 Oct 2019 23:06:47 +0530 Subject: [PATCH] Create MPOSTFIX.c --- MPOSTFIX.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 MPOSTFIX.c diff --git a/MPOSTFIX.c b/MPOSTFIX.c new file mode 100644 index 0000000..4150a8a --- /dev/null +++ b/MPOSTFIX.c @@ -0,0 +1,110 @@ +#include +#include +#include +char stack[50]; +int top=-1,size=50; +char infix[50],postfix[50]; +void push(char stack[],char val) +{ +if(top==size-1) +printf("\nstack overflow"); +else +{ +top++; +stack[top]=val; +} +} +char pop(char stack[]) +{ +char val=' '; +if(top==-1) +printf("\nstack underflow"); +else +{ +val=stack[top]; +top--; +} +return val; +} +void infixtopost(char given[],char update[]) +{ +int i=0,j=0; +char flag; +strcpy(update," "); +while(given[i]!='\0') +{ +if(given[i]=='(') +{ +push(stack,given[i]); +i++; +} +else if(given[i]==')') +{ +while((top!=-1)&&(stack[top]!='(')) +{ +update[j]=pop(stack); +j++; +} +if(top==-1) +{ +printf("\n incorrect expression"); +return; +} +flag=pop(stack); +i++; +} +else if(given[i]=='^'||given[i]=='+'||given[i]=='-'||given[i]=='*'||given[i]=='/'||given[i]=='%') +{ +while((top!=-1)&&(stack[top]!='(')&&(priority(stack[top])>priority(given[i]))) +{ +update[j]=pop(stack); +j++; +} +push(stack,given[i]); +i++; +} +else if(isalnum(given[i])) +{ +update[j]=given[i]; +j++; +i++; +} +else +{ +printf("\n incorrect element"); +} +} +while((top!=-1)&&(stack[top]!='(')) +{ +update[j]=pop(stack); +j++; +} +update[j]='\0'; +} +int priority(char sym) +{ +if(sym=='^') +return 3; +else if(sym=='/'||sym=='%'||sym=='*') +return 2; +else if(sym=='+'||sym=='-') +return 1; +else +return 0; +} + +void main() +{ +clrscr(); +printf("*****CONVERSION OF INFIX TO POSTFIX EXPRESSION*****"); +printf("\n *****MADE BY ANKUR******"); +printf("\nENTER INFIX EXPRESSION:\n"); +gets(infix); +strcpy(postfix," "); +infixtopost(infix,postfix); +printf("\nPOSTFIX EXPRESSION:\n"); +puts(postfix); +getch(); +} + +