From fc88ad6e570f3617d837db77a3075260429797de Mon Sep 17 00:00:00 2001 From: Shivansh Srivastava <42182955+Shivansh2407@users.noreply.github.com> Date: Tue, 29 Jan 2019 16:08:01 +0530 Subject: [PATCH] Create InfixToPostfix --- InfixToPostfix | 142 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 InfixToPostfix diff --git a/InfixToPostfix b/InfixToPostfix new file mode 100644 index 0000000..d16204e --- /dev/null +++ b/InfixToPostfix @@ -0,0 +1,142 @@ +// C++ program to convert an infix expression to a +// postfix expression using two precedence function +#include +using namespace std; + + +// to check if the input character +// is an operator or a '(' +int isOperator(char input) +{ + switch (input) { + case '+': + return 1; + case '-': + return 1; + case '*': + return 1; + case '^': + return 1; + case '%': + return 1; + case '/': + return 1; + case '(': + return 1; + } + return 0; +} + +// to check if the input character is an operand +int isOperand(char input) +{ + if (input >= 65 && input <= 90 + || input >= 97 && input <= 122) + return 1; + return 0; +} + +// function to return precedence value +// if operator is present in stack +int inPrec(char input) +{ + switch (input) { + case '+': + case '-': + return 2; + case '*': + case '%': + case '/': + return 4; + case '^': + return 5; + case '(': + return 0; + } +} + +// function to return precedence value +// if operator is present outside stack. +int outPrec(char input) +{ + switch (input) { + case '+': + case '-': + return 1; + case '*': + case '%': + case '/': + return 3; + case '^': + return 6; + case '(': + return 100; + } +} + +// function to convert infix to postfix +void inToPost(char* input) +{ + stack s; + + // while input is not NULL iterate + int i = 0; + while (input[i] != '\0') { + + // if character an operand + if (isOperand(input[i])) { + printf("%c", input[i]); + } + + // if input is an operator, push + else if (isOperator(input[i])) { + if (s.empty() + || outPrec(input[i]) > inPrec(s.top())) + s.push(input[i]); + else { + while (!s.empty() + && outPrec(input[i]) < inPrec(s.top())) { + printf("%c", s.top()); + s.pop(); + } + s.push(input[i]); + } + } + + // condition for opening bracket + else if (input[i] == ')') { + while (s.top() != '(') { + printf("%c", s.top()); + s.pop(); + + // if opening bracket not present + if (s.empty()) { + printf("Wrong input\n"); + exit(1); + } + } + + // pop the opening bracket. + s.pop(); + } + i++; + } + + // pop the remaining operators + while (!s.empty()) { + if (s.top() == '(') { + printf("\n Wrong input\n"); + exit(1); + } + printf("%c", s.top()); + s.pop(); + } +} + +// Driver code +int main() +{ + char input[] = "a+b*c-(d/e+f^g^h)"; + inToPost(input); + return 0; +}