-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcList.cpp
More file actions
146 lines (122 loc) · 4.99 KB
/
CalcList.cpp
File metadata and controls
146 lines (122 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "CalcList.hpp"
// Constructor
CalcList::CalcList() : CalcListInterface(){
// Default values
current = new CalcNode;
current->nodeOperator = ADDITION;
current->nodeOperand = 0.0;
current->nodeTotal = 0.0;
current->next = current;
}
// Destructor
CalcList::~CalcList(){
while(!isEmpty()) removeNode();
}
// Add node
void CalcList::addNode(const double& rTotal){
++counter; // Increment the counter for number of operations
CalcNode* temp = new CalcNode; // Create a temporary node which will get added at the beginning
temp->nodeOperator = ADDITION;
temp->nodeOperand = 0.0;
temp->nodeTotal = 0.0;
temp->next = nullptr;
// If the linked list is empty, make a node and set its next to itself
if(current == nullptr){
temp->nodeTotal = rTotal;
current = temp;
current->next = temp;
}
else{
temp->next = current->next;
temp->nodeTotal = current->next->nodeTotal;
current->next = temp;
}
}
// Remove node
void CalcList::removeNode(){
if (isEmpty()) { throw ("No existing nodes, cannot remove."); }
else {
--counter; // Decrement operation counter
CalcNode* old = current->next; // Store node to be removed in a temprory pointer object
if (old == current) { current = nullptr; } // If node to be removed happens to be the current node, set to null
else {
current->next = old->next;
}
delete old;
}
}
// Return total
double CalcList::total() const{
return current->next->nodeTotal; // Gets the total from any node
}
// Adds operation to the CalcList and updates the total
void CalcList::newOperation(const FUNCTIONS func, const double operand){
addNode(0); // Add a default node first
switch(func){ // Update the newly added default node with operator, operand, and total
case ADDITION:
current->next->nodeOperator = ADDITION;
current->next->nodeOperand = operand;
current->next->nodeTotal += operand;
break;
case SUBTRACTION:
current->next->nodeOperator = SUBTRACTION;
current->next->nodeOperand = operand;
current->next->nodeTotal -= operand;
break;
case MULTIPLICATION:
current->next->nodeOperator = MULTIPLICATION;
current->next->nodeOperand = operand;
current->next->nodeTotal *= operand;
break;
case DIVISION: // Throw an error if divison is bad
if(operand >= 0 && operand < 1.00) { removeLastOperation(); throw("Divide by zero."); }
else {
current->next->nodeOperator = DIVISION;
current->next->nodeOperand = operand;
current->next->nodeTotal /= operand;
break;
}
default: // Throw if enum is missing
throw ("Bad arithmetic operation. Use Addition, Substraction, Multiplication, or Divison.");
break;
}
}
// Remove last node
void CalcList::removeLastOperation(){
removeNode();
}
// Returns a string of the list of operations completed formatted with a fixed point precision
std::string CalcList::toString(unsigned short precision) const{
std::stringstream ss; // Using stringstream to get the previous operation in the string buffer
std::string nodeData = ""; // Store converted buffer data in this variable
int step = counter; // Temporary variable that holds the number of operations to display
CalcNode* nodePtr = new CalcNode; // Temporary variable that holds current->next data
nodePtr = current->next;
ss.precision(precision); // Sets precision
// Format the string as instructed
while(nodePtr->nodeTotal != 0){
ss << step << ": ";
ss << std::fixed << nodePtr->next->nodeTotal;
if(nodePtr->nodeOperator == ADDITION){
ss << "+";
}
else if(nodePtr->nodeOperator == SUBTRACTION){
ss << "-";
}
else if(nodePtr->nodeOperator == MULTIPLICATION){
ss << "*";
}
else if(nodePtr->nodeOperator == DIVISION){
ss << "/";
}
ss << nodePtr->nodeOperand;
ss << "=";
ss << nodePtr->nodeTotal;
ss << std::endl;
nodePtr = nodePtr->next;
--step;
}
// Convert string buffer to stream object and return
nodeData = ss.str();
return nodeData;
}