A Simple Calculator Write a simple calculator program using a DYNAMIC stack in C
ID: 3760201 • Letter: A
Question
A Simple Calculator Write a simple calculator program using a DYNAMIC stack in C++. The calculator should handle addition, subtraction, division, multiplication and parentheses expressions. The add() function pops the first two values off the stack, adds them together, and pushes the sum onto the stack. The sub() function pops the first two values off the stack, subtracts the second value from the first, and then pushes the difference onto the stack. The mult() pops the top two values off the stack, multiplies them, and pushes their product onto the stack. The div() pops the top two values off the stack, divides the second value by the first, and pushes the quotient onto the stack. Your program must use inheritance. Be sure to provide descriptive comments for every line of code. To help guide your algorithm design, sample prefix notation input with answers are given below: + 2 4 => 2 + 4 = 6 * 8 ( + 7 12) => 8 * ( 7 + 12 ) = 152 ( + 7 ( * 8 12 ) ( * 2 (+ 9 4) 7 ) 3 ) => 7+8*12+2*(9+4)*7+3 = 148
Explanation / Answer
Complete Program:
File: DynamicStack.h
#ifndef DYNAMICSTACK_H
#define DYNAMICSTACK_H
struct Node
{
int value;
Node *next;
};
class DynamicStack
{
public:
DynamicStack();
~DynamicStack();
void push(int);
void pop(int &);
bool isEmpty();
private:
Node *top;
};
#endif
File: DynamicStack.cpp
#include "DynamicStack.h"
#include <iostream>
using namespace std;
DynamicStack::DynamicStack()
{
top = NULL;
}
DynamicStack::~DynamicStack()
{
Node *nodePtr, *nextNode;
nodePtr = top;
while(nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
void DynamicStack::push(int number)
{
Node *newNode;
newNode = new Node;
newNode->value = number;
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else
{
newNode->next = top;
top = newNode;
}
}
void DynamicStack::pop(int &number)
{
Node *temp;
if (isEmpty())
{
cout << "Stack is empty" << endl;
}
else
{
number = top->value;
temp = top->next;
delete top;
top = temp;
}
}
bool DynamicStack::isEmpty()
{
if(!top)
return true;
else
return false;
}
File: Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "DynamicStack.h"
class Calculator : public DynamicStack
{
public:
Calculator();
void add();
void sub();
void mul();
void div();
};
#endif
File: Calculator.cpp
#include "Calculator.h"
Calculator::Calculator() : DynamicStack()
{}
void Calculator::add()
{
int first, sum;
pop(sum);
pop(first);
sum += first;
push(sum);
}
void Calculator::sub()
{
int first, diff;
pop(diff);
pop(first);
diff -= first;
push(diff);
}
void Calculator::mul()
{
int first, prod;
pop(prod);
pop(first);
prod *= first;
push(prod);
}
void Calculator::div()
{
int first, quot;
pop(quot);
pop(first);
quot /= first;
push(quot);
}
File: Driver.cpp
#include "Calculator.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
Calculator stack;
string prefix;
string revPref = "";
cout << "Enter a valid prefix notation: ";
getline(cin, prefix);
for(int i = prefix.length() - 1; i >= 0; i--)
{
revPref = revPref + prefix[i];
}
int size = revPref.length();
int result;
for(int i = 0; i < size; i++)
{
char ch = revPref[i];
if(isdigit(ch))
{
stack.push(ch - '0');
}
else if(ch == '+')
{
stack.add();
}
else if(ch == '-')
{
stack.sub();
}
else if(ch == '*')
{
stack.mul();
}
else if(ch == '/')
{
stack.div();
}
else{}
}
stack.pop(result);
cout << "Result: " << result << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.