This is a c++ program. Please follow all directions The postfix calculator. This
ID: 654397 • Letter: T
Question
This is a c++ program. Please follow all directions
The postfix calculator. This is our second complex project and you can use functions you developed for the little calculator: skipBlanks, getNumber and doOperation. Use a string to hold a line of input. Use a special array to hold ints (often called stack). You may get input from a file or the console.
Your processor does int arithmetic using the usual operations in the fashion of an hp calculator, sometimes called Polish postfix notation. It should be able to handle an entire line of input, blank separators between int values and operators are allowed but not required. For each line of input, generate the postfix evaluation, or an error. Be sure to show all error-handling.
Example 1: 20 20 * 10 10 + -
For example 1, the answer is 380.
Example 2: 10 10 ++
For example 2, the processor outputs ERROR: too many operators.
Example 3: 10 10 10 & *
For example 3, the processor outputs illegal character.
Example 4: 10 10
For example 4, output is missing operator, or too many operands.
Example 5: 20 0 /
For example 5, output is division by zero error.
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include "IntStack.h"
using std::cout;
using std::cin;
int noConfirmationExecution();
int confirmationExecution();
void calcOperation(IntStack&, char);
int main()
{
int executionType;
int calcTotal;
cout << ">>> Execution Type: ";
cout << "(1) Values entered all at once (with no confirmations) ";
cout << "(2) Values entered one at a time (with confirmations) ";
cin >> executionType;
if (executionType == 1)
calcTotal = noConfirmationExecution();
else if (executionType == 2)
calcTotal = confirmationExecution();
cout << " *** Total: " << calcTotal << " ";
system("PAUSE");
return 0;
}
int noConfirmationExecution()
{
IntStack stack(20);
char item = ' ';
cout << " >> Items ('s' to stop): ";
cin >> item;
while (item != 's')
{
int i = atoi(&item);
if (item != '+' && item != '-' && item != '*' && item != '/')
stack.push(i);
else if (item == '+' || item == '-' || item == '*' || item == '/')
calcOperation(stack, item);
cin >> item;
}
int total;
stack.pop(total);
return total;
}
int confirmationExecution()
{
IntStack stack(20);
int number;
char _continue = 'Y';
char anotherNumber;
char anotherOperator;
cout << " > Number: ";
cin >> number;
stack.push(number);
do
{
do
{
anotherNumber = 'n';
cout << " > Number: ";
cin >> number;
stack.push(number);
cout << " - Another Number (y/n)? ";
cin >> anotherNumber;
} while (anotherNumber != 'n');
do
{
anotherOperator = 'n';
char _operator;
cout << " >> Operator: ";
cin >> _operator;
calcOperation(stack, _operator);
cout << " - Another Operator (y/n)? ";
cin >> anotherOperator;
} while (anotherOperator != 'n');
cout << " - Continue (y/n)? ";
cin >> _continue;
} while (_continue != 'n');
int total;
stack.pop(total);
return total;
}
void calcOperation(IntStack &stack, char _operator)
{
int num1, num2;
stack.pop(num2);
stack.pop(num1);
if (_operator == '+')
stack.push(num1 + num2);
else if (_operator == '-')
stack.push(num1 - num2);
else if (_operator == '*')
stack.push(num1 * num2);
else if (_operator == '/')
stack.push(num1 / num2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.