D27 Infix To Postfix DSL
D27 Infix To Postfix DSL
D27 Infix To Postfix DSL
#define MAX_ARR_SIZE 30
using namespace std;
public:
int top;
char_stack() { top = -1; }
bool isempty() { return (top <= -1) ? true : false; }
bool isfull() { return (top == MAX_ARR_SIZE - 1) ? true : false; }
void push(char elmt) {
if (not isfull()) {
top++;
list[top] = elmt;
}
}
char peek() { return (top != -1) ? list[top] : ' '; }
char pop() {
if (not isempty()) {
top--;
return list[top + 1];
}
}
};
class stack {
int list[MAX_ARR_SIZE];
public:
int top;
stack() { top = -1; }
bool isempty() { return (top <= -1) ? true : false; }
bool isfull() { return (top == MAX_ARR_SIZE - 1) ? true : false; }
void push(int elmt) {
if (not isfull()) {
top++;
list[top] = elmt;
}
}
int pop() {
if (not isempty()) {
top--;
return list[top + 1];
}
}
void dispaly() {
for (int i = 0; i < top + 1; i++) {
cout << list[i] << " ";
}
cout << endl;
}
};
// postfix calculator
int evalPostfix(string exp) {
int max_var = 0;
for (int i = 0; i < exp.size(); i++) {
if (exp[i] >= 65 && exp[i] <= 90) {
if (exp[i] > max_var) {
max_var = exp[i];
}
} else if (exp[i] > 97 && exp[i] <= 122) {
if ((exp[i] - 32) > max_var) {
max_var = (exp[i] - 32);
}
}
}
if (!IS_VAL_SET || max_var > VAR_COUNT) {
setVariables(max_var - 64);
}
default:
values.push(VAR_VALS[int(exp[i] - 65)]);
break;
}
}
return values.pop();
}
int main() {
cout << "\n***\nNot Case Sensitive\nMaximum 30 characters\n***\n" << endl;
int ch;
bool while_ctrl = true;
string temp, LastInfixExp;
int ans;
while (while_ctrl) {
cout << "MENU\n1. Infix To Postfix\n2. Set Variable Values\n3. Postfix "
"Calculator\n4. Exit"
<< endl;
cout << "Enter Your Choice:";
cin >> ch;
switch (ch) {
case 1:
cout << "Enter a Infix Expression:";
cin >> temp;
LastInfixExp = infixToPostfix(temp);
cout << "Postfix Expression is :" << LastInfixExp << endl;
break;
case 2:
setVariables();
cout << "Variables set sucessfully" << endl;
break;
case 3:
if (LastInfixExp.size() != 0) {
cout << "Enter Postfix Expression(Enter 0 to use last "
"converted Expression):";
cin >> temp;
if (temp == "0") {
ans = evalPostfix(LastInfixExp);
} else {
ans = evalPostfix(temp);
}
cout << "Value of expression is " << ans << endl;
} else {
cout << "Enter Postfix Expression:";
cin >> temp;
ans = evalPostfix(temp);
cout << "Value of expression is " << ans << endl;
}
break;
case 4:
cout << "Thank You!" << endl;
while_ctrl = false;
break;
default:
cout << "Enter a Valid Choice:" << endl;
break;
}
}
return 0;
}
Expected Output:
Expression: (a+b/c*(d+e)-f)
Postfix: abc/de+*+f-