From 01c697bbd0419f8eaecef84e9289b4a5e01237b1 Mon Sep 17 00:00:00 2001 From: sarthakmaggu Date: Mon, 7 Oct 2019 15:00:54 +0530 Subject: [PATCH] Infix to postfix --- 11_Infixtopostfix/Infixtopostfix.c | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 11_Infixtopostfix/Infixtopostfix.c diff --git a/11_Infixtopostfix/Infixtopostfix.c b/11_Infixtopostfix/Infixtopostfix.c new file mode 100644 index 0000000..98fd71a --- /dev/null +++ b/11_Infixtopostfix/Infixtopostfix.c @@ -0,0 +1,53 @@ +#include +#include +char arr[20]; +int top = -1; +int precedence(char ele){ + if(ele == '+' || ele == '-') + return 1; + if(ele == '*' || ele == '/') + return 2; + if(ele == '^') + return 3; + else + return 0; +} +int operand(char ele){ + if(ele == '+' || ele == '/' || ele == '-' || ele == '*' || ele == '^') + return 0; + else + return 1;} +void push(char ele){ + if(top == 19){ + printf("Overflow \n"); + } + else + arr[++top] = ele; +} +char peek(){ + return arr[top]; +} +int main(){ + char expression[20],output[20]; + scanf("%s", expression); + int i =0,j=0; + while(expression[i] != '\0'){ + if(operand(expression[i])) + output[j++] = expression[i++]; + else{ + if(precedence(expression[i]) > precedence(peek())) + push(expression[i++]); + else{ + output[j++] = peek(); + top--; + } + } + } + while(top != -1){ + output[j++] = peek(); + top--; + } + output[j] = '\0'; + printf("%s", output); + return 0; +} \ No newline at end of file