1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <iostream>
#include <stack>
#include <string>
#include <cstring>
using namespace std;
// Function to verify if character is a number
bool
isNumber(char c)
{
if (c >= '0' && c <= '9')
return true;
return false;
}
// Function to verify if a character is an operator
bool
isOperator(char c)
{
if (c == '+' || c == '-' || c == '*' || c == '/')
return true;
return false;
}
// Function to perform an operation
int
PerformOperation(char operation, int operand1, int operand2)
{
if (operation == '+')
return operand1 + operand2;
else if (operation == '-')
return operand1 - operand2;
else if (operation == '*')
return operand1 * operand2;
else if (operation == '/')
return operand1 / operand2;
else
cout << "Unexpected Error" << endl;
return -1;
}
enum TokenType {Number, Operator};
// Read a token from "line", starting at "pos". On success, return
// true, update pos, and set the token and its type in "token" and
// "type". On failure, return false.
bool
nextToken(const string &line, size_t &pos, string &token, TokenType &type)
{
// Skip delimiters and return if you hit end of string.
// Notice the trick with strchr to see if the character is one of a set.
while (pos < line.size() && strchr(" ,\'\"", line[pos])) {
++pos;
}
if (pos == line.size()) return false;
token.clear();
if (isOperator(line[pos])) {
type = Operator;
token = line[pos++];
return true;
}
// It must be a number. Read it and return it.
while (pos < line.size() && isNumber(line[pos])) {
token += line[pos++];
}
type = Number;
return true;
}
// Function to evaluate Postfix expression and return output
int
EvaluatePostfix(string expression)
{
stack < int >S;
string token;
enum TokenType type;
size_t pos=0;
while (nextToken(expression, pos, token, type)) {
switch (type) {
case Operator:
{
// pop two elements from stack,
// perform operation and pushback the result
int operand2 = S.top();
S.pop();
int operand1 = S.top();
S.pop();
int result = PerformOperation(token[0], operand1, operand2);
S.push(result);
break;
}
case Number:
{
// Convert the token to a number and push it on the stack
int operand = atoi(token.c_str());
S.push(operand);
break;
}
}
}
return S.top();
}
// Convert the postfix Expression to an infix Expression
string
getInfix(string expression)
{
stack < string > s;
string token;
enum TokenType type;
size_t pos=0;
while (nextToken(expression, pos, token, type)) {
switch (type) {
case Operator:
{
string operand2 = s.top();
s.pop();
string operand1 = s.top();
s.pop();
s.push("(" + operand1 + token + operand2 + ")");
break;
}
case Number:
s.push(token);
break;
}
}
return s.top();
}
int
main()
{
string expression = ("'2','1','+','3','*'");
int result = EvaluatePostfix(expression);
string expression2 = ("\"4\",\"13\",\"5\",\"/\",\"+\"");
int result2 = EvaluatePostfix(expression2);
string expression3 = ("2 1 + 3 *");
int result3 = EvaluatePostfix(expression3);
string expression4 = ("4 13 5 / +");
int result4 = EvaluatePostfix(expression4);
cout << "Input: {" << expression << "}; Output: " << result << " Explanation: "
<< getInfix(expression) << endl;
cout << "Input: {" << expression2 << "}; Output: " << result2 << " Explanation: "
<< getInfix(expression2) << endl;
}
|