My postfix program essentially allows the user two input options to calculate a postfix equation:
- Confirmations after each input (after an input, the user is ask for another number, operator, or final result)
- No confirmation after each input (user is only asked for a number of inputs (values and operators), and the final result is shown after the loop)
My original attempt at this program used the first option because I had trouble getting my program to tell the difference between a value and an operator (both are treated as ASCII values, which is normal). However, I used a better method (the second option) after learning about atoi()
. Alongside these two functions, another function does all of the calculations for both functions.
How could this program be improved for clarity and effectiveness? Should I even bother offering both options, even just for comparison's sake? I'm also not sure how much input validation I should provide, if it's really necessary.
NOTE: The IntStack
class was written by an instructor of mine, and I'm just using it for simplicity and learning. As it is part of my own written code, I'm including it here so that my code's meaning is not lost. As it's not my own code, please do not review it. Only review my own code.
Postfix.cpp
#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:\n\n";
cout << "(1) Values entered all at once (with no confirmations)\n";
cout << "(2) Values entered one at a time (with confirmations)\n\n";
cin >> executionType;
if (executionType == 1)
calcTotal = noConfirmationExecution();
else if (executionType == 2)
calcTotal = confirmationExecution();
cout << "\n\n*** Total: " << calcTotal << "\n\n\n";
system("PAUSE");
return 0;
}
int noConfirmationExecution()
{
IntStack stack(20);
char item = ' ';
cout << "\n>> Items ('s' to stop):\n\n";
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 << "\n> Number: ";
cin >> number;
stack.push(number);
do
{
do
{
anotherNumber = 'n';
cout << "\n> Number: ";
cin >> number;
stack.push(number);
cout << "\n- Another Number (y/n)? ";
cin >> anotherNumber;
} while (anotherNumber != 'n');
do
{
anotherOperator = 'n';
char _operator;
cout << "\n>> Operator: ";
cin >> _operator;
calcOperation(stack, _operator);
cout << "\n- Another Operator (y/n)? ";
cin >> anotherOperator;
} while (anotherOperator != 'n');
cout << "\n- 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);
}
IntStack.h
#ifndef INTSTACK_H #define INTSTACK_H class IntStack { private: int *stackArray; int stackSize; int top; public: IntStack(int); IntStack(const IntStack &); ~IntStack(); void push(int); void pop(int &); bool isFull() const; bool isEmpty() const; }; #endif
IntStack.cpp
#include <iostream> #include "IntStack.h" using namespace std; IntStack::IntStack(int size) { stackSize = size; stackArray = new int[size]; top = -1; } IntStack::IntStack(const IntStack &obj) { // Create the stack array. if (obj.stackSize > 0) stackArray = new int[obj.stackSize]; else stackArray = NULL; // Copy the stackSize attribute. stackSize = obj.stackSize; // Copy the stack contents. for (int count = 0; count < stackSize; count++) stackArray[count] = obj.stackArray[count]; // Set the top of the stack. top = obj.top; } IntStack::~IntStack() {delete [] stackArray;} void IntStack::push(int num) { if (isFull()) { cout << "The stack is full.\n"; } else { top++; stackArray[top] = num; } } void IntStack::pop(int &num) { if (isEmpty()) { cout << "The stack is empty.\n"; } else { num = stackArray[top]; top--; } } bool IntStack::isFull() const { bool status; if (top == stackSize - 1) status = true; else status = false; return status; } bool IntStack::isEmpty() const { bool status; if (top == -1) status = true; else status = false; return status; }