|
| 1 | +#include<iostream> |
| 2 | +#include<stack> |
| 3 | +using namespace std; |
| 4 | +// defines the Boolean function for operator, operand, equalOrhigher precedence and the string conversion function. |
| 5 | +bool IsOperator(char); |
| 6 | +bool IsOperand(char); |
| 7 | +bool eqlOrhigher(char, char); |
| 8 | +string convert(string); |
| 9 | + |
| 10 | +int main() |
| 11 | +{ |
| 12 | +string infix_expression, postfix_expression; |
| 13 | +int ch; |
| 14 | +do |
| 15 | +{ |
| 16 | +cout << " Enter an infix expression: "; |
| 17 | +cin >> infix_expression; |
| 18 | + postfix_expression = convert(infix_expression); |
| 19 | + cout << "\n Your Infix expression is: " << infix_expression; |
| 20 | +cout << "\n Postfix expression is: " << postfix_expression; |
| 21 | +cout << "\n \t Do you want to enter infix expression (1/ 0)?"; |
| 22 | +cin >> ch; |
| 23 | +//cin.ignore(); |
| 24 | +} while(ch == 1); |
| 25 | +return 0; |
| 26 | +} |
| 27 | + |
| 28 | +// define the IsOperator() function to validate whether any symbol is operator. |
| 29 | +/* If the symbol is operator, it returns true, otherwise false. */ |
| 30 | +bool IsOperator(char c) |
| 31 | +{ |
| 32 | +if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^' ) |
| 33 | +return true; |
| 34 | +return false; |
| 35 | +} |
| 36 | + |
| 37 | +// IsOperand() function is used to validate whether the character is operand. |
| 38 | +bool IsOperand(char c) |
| 39 | +{ |
| 40 | +if( c >= 'A' && c <= 'Z') /* Define the character in between A to Z. If not, it returns False.*/ |
| 41 | +return true; |
| 42 | +if (c >= 'a' && c <= 'z') // Define the character in between a to z. If not, it returns False. */ |
| 43 | +return true; |
| 44 | +if(c >= '0' && c <= '9') // Define the character in between 0 to 9. If not, it returns False. */ |
| 45 | +return true; |
| 46 | +return false; |
| 47 | +} |
| 48 | +// here, precedence() function is used to define the precedence to the operator. |
| 49 | +int precedence(char op) |
| 50 | +{ |
| 51 | +if(op == '+' || op == '-') /* it defines the lowest precedence */ |
| 52 | +return 1; |
| 53 | +if (op == '*' || op == '/') |
| 54 | +return 2; |
| 55 | +if(op == '^') /* exponent operator has the highest precedence * |
| 56 | +return 3; |
| 57 | +return 0; |
| 58 | +} |
| 59 | +/* The eqlOrhigher() function is used to check the higher or equal precedence of the two operators in infix expression. */ |
| 60 | +bool eqlOrhigher (char op1, char op2) |
| 61 | +{ |
| 62 | +int p1 = precedence(op1); |
| 63 | +int p2 = precedence(op2); |
| 64 | +if (p1 == p2) |
| 65 | +{ |
| 66 | +if (op1 == '^' ) |
| 67 | +return false; |
| 68 | +return true; |
| 69 | +} |
| 70 | +return (p1>p2 ? true : false); |
| 71 | +} |
| 72 | + |
| 73 | +/* string convert() function is used to convert the infix expression to the postfix expression of the Stack */ |
| 74 | +string convert(string infix) |
| 75 | +{ |
| 76 | +stack <char> S; |
| 77 | +string postfix =""; |
| 78 | +char ch; |
| 79 | + |
| 80 | +S.push( '(' ); |
| 81 | +infix += ')'; |
| 82 | + |
| 83 | +for(int i = 0; i<infix.length(); i++) |
| 84 | +{ |
| 85 | +ch = infix[i]; |
| 86 | + |
| 87 | +if(ch == ' ') |
| 88 | +continue; |
| 89 | +else if(ch == '(') |
| 90 | +S.push(ch); |
| 91 | +else if(IsOperand(ch)) |
| 92 | +postfix += ch; |
| 93 | +else if(IsOperator(ch)) |
| 94 | +{ |
| 95 | +while(!S.empty() && eqlOrhigher(S.top(), ch)) |
| 96 | +{ |
| 97 | +postfix += S.top(); |
| 98 | +S.pop(); |
| 99 | +} |
| 100 | +S.push(ch); |
| 101 | +} |
| 102 | +else if(ch == ')') |
| 103 | +{ |
| 104 | +while(!S.empty() && S.top() != '(') |
| 105 | +{ |
| 106 | +postfix += S.top(); |
| 107 | +S.pop(); |
| 108 | +} |
| 109 | +S.pop(); |
| 110 | +} |
| 111 | +} |
| 112 | +return postfix; |
| 113 | +} |
0 commit comments