2. Stack. Write a program that uses the stack class (you can use the stack type defined in the
lectures, or you can define your own stack type) to solve a problem. The problem contains two
tasks, the first one is to convert an infix arithmetic expression into a postfix arithmetic
expression; the second task is to evaluate arithmetic expressions written in postfix form.
In infix form, the operator of an arithmetic statement is in-between every pair of operands. For
example:
1.5 + 2.3
2.0 * (3.3 + 4.5)
(2.1 + 3.0) * 4.8
In postfix form, the operands of an arithmetic statement appear followed by the operator. One of
the virtues of postfix form is that expressions can be written without the need for parentheses.
Here are some examples of arithmetic expressions written in postfix form:
1.5 2.3 + // Equivalent to 1.5 + 2.3
2.0 3.3 4.5 + * // Equivalent to 2.0 * (3.3 + 4.5)
2.1 3.3 + 4.8 * // Equivalent to (2.1 + 3.0) * 4.8
Please write a C++ program that uses an operator stack to convert an infix arithmetic expression
that the user enters into a postfix arithmetic expression, and then use a float stack to evaluate
postfix arithmetic.
Your program should support all five of the arithmetic operators, +, - , *, and /.
When users input the infix arithmetic expressions, the operands, operators and parenthesis
symbols are separated by spaces. The character # marks the end of the expression.
The infix arithmetic expressions only contain operands, operators and parenthesis. The operands
are float numbers, and the operators are +, -, * and /. If there are letters in the infix arithmetic
expressions, the “invalid expression” message should be displayed.
Here is an algorithm for evaluating an arithmetic expression written in postfix form with the aid
of a stack of floats.
• Scan the expression from left to right.
• When encounter an operand or a float, push it on the stack.
• When encounter an arithmetic operator, pop the top two numbers off the stack and use them as
operands for the indicated operation. Push the resulting number back on the stack.
• when you reach the end of the expression, the result of the calculation should be the sole item
on the stack.
Please show that that the program is working correctly by converting the following infix
arithmetic expressions and then computing the obtained postfix arithmetic expressions:
6.5 * 6.5 - 4 * 1.5 * 1.44 #
3.1 * (2.5 + 4.7 * 1.6) + 8.6 #
70.0 - (10.5 + 6) / 0.5 + 18.2 #
The following steps will convert an infix expression into a postfix expression:
1. Create an empty string stack called opStack for keeping operators. Create an empty string
array called postfixEx for postfix expression.
2. read the infix expression left to right.
2.1. If the string is an operand, append it to the postfixEx array.
2.2. If the string is a left parenthesis, push it on the opStack.
2.3. If the string is a right parenthesis, pop the opStack, append each operator to the postfixEx
array, until you see the corresponding left parent.
2. Stack. Write a program that uses the stack class (you can use.pdf
1. 2. Stack. Write a program that uses the stack class (you can use the stack type defined in the
lectures, or you can define your own stack type) to solve a problem. The problem contains two
tasks, the first one is to convert an infix arithmetic expression into a postfix arithmetic
expression; the second task is to evaluate arithmetic expressions written in postfix form.
In infix form, the operator of an arithmetic statement is in-between every pair of operands. For
example:
1.5 + 2.3
2.0 * (3.3 + 4.5)
(2.1 + 3.0) * 4.8
In postfix form, the operands of an arithmetic statement appear followed by the operator. One of
the virtues of postfix form is that expressions can be written without the need for parentheses.
Here are some examples of arithmetic expressions written in postfix form:
1.5 2.3 + // Equivalent to 1.5 + 2.3
2.0 3.3 4.5 + * // Equivalent to 2.0 * (3.3 + 4.5)
2.1 3.3 + 4.8 * // Equivalent to (2.1 + 3.0) * 4.8
Please write a C++ program that uses an operator stack to convert an infix arithmetic expression
that the user enters into a postfix arithmetic expression, and then use a float stack to evaluate
postfix arithmetic.
Your program should support all five of the arithmetic operators, +, - , *, and /.
When users input the infix arithmetic expressions, the operands, operators and parenthesis
symbols are separated by spaces. The character # marks the end of the expression.
The infix arithmetic expressions only contain operands, operators and parenthesis. The operands
are float numbers, and the operators are +, -, * and /. If there are letters in the infix arithmetic
expressions, the “invalid expression” message should be displayed.
Here is an algorithm for evaluating an arithmetic expression written in postfix form with the aid
of a stack of floats.
• Scan the expression from left to right.
• When encounter an operand or a float, push it on the stack.
• When encounter an arithmetic operator, pop the top two numbers off the stack and use them as
operands for the indicated operation. Push the resulting number back on the stack.
• when you reach the end of the expression, the result of the calculation should be the sole item
on the stack.
2. Please show that that the program is working correctly by converting the following infix
arithmetic expressions and then computing the obtained postfix arithmetic expressions:
6.5 * 6.5 - 4 * 1.5 * 1.44 #
3.1 * (2.5 + 4.7 * 1.6) + 8.6 #
70.0 - (10.5 + 6) / 0.5 + 18.2 #
The following steps will convert an infix expression into a postfix expression:
1. Create an empty string stack called opStack for keeping operators. Create an empty string
array called postfixEx for postfix expression.
2. read the infix expression left to right.
2.1. If the string is an operand, append it to the postfixEx array.
2.2. If the string is a left parenthesis, push it on the opStack.
2.3. If the string is a right parenthesis, pop the opStack, append each operator to the postfixEx
array, until you see the corresponding left parenthesis, discard the pair of parentheses.
2.4. If the string is an operator, *, /, +, or -, first remove any operators already on the opStack
that have higher or equal precedence and append them to the postfixEx array, then push the
current operator on the opStack.
3. When the input infix expression has been completely processed, check the opstack, any
operators still on the stack can be removed and appended to the end of the postfixEx array.
2. Stack. Write a program that uses the stack class (you can use the stack type defined in the
lectures, or you can define your own stack type) to solve a problem. The problem contains two
tasks, the first one is to convert an infix arithmetic expression into a postfix arithmetic
expression; the second task is to evaluate arithmetic expressions written in postfix form.
In infix form, the operator of an arithmetic statement is in-between every pair of operands. For
example:
1.5 + 2.3
2.0 * (3.3 + 4.5)
(2.1 + 3.0) * 4.8
In postfix form, the operands of an arithmetic statement appear followed by the operator. One of
the virtues of postfix form is that expressions can be written without the need for parentheses.
Here are some examples of arithmetic expressions written in postfix form:
1.5 2.3 + // Equivalent to 1.5 + 2.3
3. 2.0 3.3 4.5 + * // Equivalent to 2.0 * (3.3 + 4.5)
2.1 3.3 + 4.8 * // Equivalent to (2.1 + 3.0) * 4.8
Please write a C++ program that uses an operator stack to convert an infix arithmetic expression
that the user enters into a postfix arithmetic expression, and then use a float stack to evaluate
postfix arithmetic.
Your program should support all five of the arithmetic operators, +, - , *, and /.
When users input the infix arithmetic expressions, the operands, operators and parenthesis
symbols are separated by spaces. The character # marks the end of the expression.
The infix arithmetic expressions only contain operands, operators and parenthesis. The operands
are float numbers, and the operators are +, -, * and /. If there are letters in the infix arithmetic
expressions, the “invalid expression” message should be displayed.
Here is an algorithm for evaluating an arithmetic expression written in postfix form with the aid
of a stack of floats.
• Scan the expression from left to right.
• When encounter an operand or a float, push it on the stack.
• When encounter an arithmetic operator, pop the top two numbers off the stack and use them as
operands for the indicated operation. Push the resulting number back on the stack.
• when you reach the end of the expression, the result of the calculation should be the sole item
on the stack.
Please show that that the program is working correctly by converting the following infix
arithmetic expressions and then computing the obtained postfix arithmetic expressions:
6.5 * 6.5 - 4 * 1.5 * 1.44 #
3.1 * (2.5 + 4.7 * 1.6) + 8.6 #
70.0 - (10.5 + 6) / 0.5 + 18.2 #
The following steps will convert an infix expression into a postfix expression:
1. Create an empty string stack called opStack for keeping operators. Create an empty string
array called postfixEx for postfix expression.
2. read the infix expression left to right.
2.1. If the string is an operand, append it to the postfixEx array.
2.2. If the string is a left parenthesis, push it on the opStack.
2.3. If the string is a right parenthesis, pop the opStack, append each operator to the postfixEx
4. array, until you see the corresponding left parenthesis, discard the pair of parentheses.
2.4. If the string is an operator, *, /, +, or -, first remove any operators already on the opStack
that have higher or equal precedence and append them to the postfixEx array, then push the
current operator on the opStack.
3. When the input infix expression has been completely processed, check the opstack, any
operators still on the stack can be removed and appended to the end of the postfixEx array.
2. Stack. Write a program that uses the stack class (you can use the stack type defined in the
lectures, or you can define your own stack type) to solve a problem. The problem contains two
tasks, the first one is to convert an infix arithmetic expression into a postfix arithmetic
expression; the second task is to evaluate arithmetic expressions written in postfix form.
In infix form, the operator of an arithmetic statement is in-between every pair of operands. For
example:
1.5 + 2.3
2.0 * (3.3 + 4.5)
(2.1 + 3.0) * 4.8
In postfix form, the operands of an arithmetic statement appear followed by the operator. One of
the virtues of postfix form is that expressions can be written without the need for parentheses.
Here are some examples of arithmetic expressions written in postfix form:
1.5 2.3 + // Equivalent to 1.5 + 2.3
2.0 3.3 4.5 + * // Equivalent to 2.0 * (3.3 + 4.5)
2.1 3.3 + 4.8 * // Equivalent to (2.1 + 3.0) * 4.8
Please write a C++ program that uses an operator stack to convert an infix arithmetic expression
that the user enters into a postfix arithmetic expression, and then use a float stack to evaluate
postfix arithmetic.
Your program should support all five of the arithmetic operators, +, - , *, and /.
When users input the infix arithmetic expressions, the operands, operators and parenthesis
symbols are separated by spaces. The character # marks the end of the expression.
The infix arithmetic expressions only contain operands, operators and parenthesis. The operands
are float numbers, and the operators are +, -, * and /. If there are letters in the infix arithmetic
expressions, the “invalid expression” message should be displayed.
Here is an algorithm for evaluating an arithmetic expression written in postfix form with the aid
of a stack of floats.
• Scan the expression from left to right.
• When encounter an operand or a float, push it on the stack.
• When encounter an arithmetic operator, pop the top two numbers off the stack and use them as
operands for the indicated operation. Push the resulting number back on the stack.
5. • when you reach the end of the expression, the result of the calculation should be the sole item
on the stack.
Please show that that the program is working correctly by converting the following infix
arithmetic expressions and then computing the obtained postfix arithmetic expressions:
6.5 * 6.5 - 4 * 1.5 * 1.44 #
3.1 * (2.5 + 4.7 * 1.6) + 8.6 #
70.0 - (10.5 + 6) / 0.5 + 18.2 #
The following steps will convert an infix expression into a postfix expression:
1. Create an empty string stack called opStack for keeping operators. Create an empty string
array called postfixEx for postfix expression.
2. read the infix expression left to right.
2.1. If the string is an operand, append it to the postfixEx array.
2.2. If the string is a left parenthesis, push it on the opStack.
2.3. If the string is a right parenthesis, pop the opStack, append each operator to the postfixEx
array, until you see the corresponding left parenthesis, discard the pair of parentheses.
2.4. If the string is an operator, *, /, +, or -, first remove any operators already on the opStack
that have higher or equal precedence and append them to the postfixEx array, then push the
current operator on the opStack.
3. When the input infix expression has been completely processed, check the opstack, any
operators still on the stack can be removed and appended to the end of the postfixEx array.
The following steps will convert an infix expression into a postfix expression:
1. Create an empty string stack called opStack for keeping operators. Create an empty string
array called postfixEx for postfix expression.
2. read the infix expression left to right.
2.1. If the string is an operand, append it to the postfixEx array.
2.2. If the string is a left parenthesis, push it on the opStack.
2.3. If the string is a right parenthesis, pop the opStack, append each operator to the postfixEx
array, until you see the corresponding left parenthesis, discard the pair of parentheses.
2.4. If the string is an operator, *, /, +, or -, first remove any operators already on the opStack
that have higher or equal precedence and append them to the postfixEx array, then push the
current operator on the opStack.
6. 3. When the input infix expression has been completely processed, check the opstack, any
operators still on the stack can be removed and appended to the end of the postfixEx array.
Solution
#include
#include
#include
#include
using namespace std;
int eval(int op1, int op2, char operate) {
switch (operate) {
case '*': return op2 * op1;
case '/': return op2 / op1;
case '+': return op2 + op1;
case '-': return op2 - op1;
default : return 0;
}
}
int getpre(char ch) {
switch (ch) {
case '/':
case '*': return 2;
case '+':
case '-': return 1;
default : return 0;
}
}
void in2pos(char infix[], char postfix[], int size) {
stack s;
int pre;
int i = 0;
int k = 0;
char ch;
while (i < size) {
ch = infix[i];
7. if (ch == '(') {
s.push(ch);
i++;
continue;
}
if (ch == ')') {
while (!s.empty() && s.top() != '(') {
postfix[k++] = s.top();
s.pop();
}
if (!s.empty()) {
s.pop();
}
i++;
continue;
}
pre = getpre(ch);
if (pre == 0) {
postfix[k++] = ch;
}
else {
if (s.empty()) {
s.push(ch);
}
else {
while (!s.empty() && s.top() != '(' &&
pre <= getpre(s.top())) {
postfix[k++] = s.top();
s.pop();
}
s.push(ch);
}
}
i++;
}
while (!s.empty()) {
8. postfix[k++] = s.top();
s.pop();
}
postfix[k] = 0;
}
int evalpos(char postfix[], int size) {
stack s;
int i = 0;
char ch;
int val;
while (i < size) {
ch = postfix[i];
if (isdigit(ch)) {
s.push(ch-'0');
}
else {
int op1 = s.top();
s.pop();
int op2 = s.top();
s.pop();
val = eval(op1, op2, ch);
s.push(val);
}
i++;
}
return val;
}
int main() {
const int max = 10;
char infix[max] = {0};
int f;
cout << "Please enter infix expression: " << endl;
cin>>infix;
int size = strlen(infix);
char postfix[size];
in2pos(infix,postfix,size);