-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcList.cpp
More file actions
135 lines (124 loc) · 4.43 KB
/
CalcList.cpp
File metadata and controls
135 lines (124 loc) · 4.43 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
/*
Author of this file: Mihir K Patel
Purpose: Following is a source file for CalcList.hpp. It implements the definitions for
constructors and destructors and abstract functions.
*/
#include "CalcList.hpp"
/*Constructor that makes a cursor node object*/
CalcList::CalcList() :CalcListInterface() {
cursor = new CalcNode;
cursor->nodes_operator = ADDITION;
cursor->nodes_operand = 0.0;
cursor->nodes_total = 0.0;
cursor->next = cursor;
}
/*Destroys the CalcList objects*/
CalcList::~CalcList() {
while (!is_empty()) { remove_node(); } /*Recursive call*/
}
/*Adds a node in the beginning*/
void CalcList::add_node(const double& element_para) {
++steps;/*Increment the operation counter*/
CalcNode* before_crsr = new CalcNode; /*Temprory node which will get added in beginning*/
before_crsr->next = nullptr;
before_crsr->nodes_total = 0.0;
before_crsr->nodes_operand = 0.0;
before_crsr->nodes_operator = ADDITION;
if (cursor == nullptr) { /*If the list is empty, make a node, set its next to itself*/
before_crsr->nodes_total = element_para;
cursor = before_crsr;
cursor->next = before_crsr;
}
else { /*Or just put the node and make it cursor's next*/
before_crsr->next = cursor->next;
before_crsr->nodes_total = cursor->next->nodes_total;
cursor->next = before_crsr;
}
}
/*remove a node, if no node in the list, throws an error*/
void CalcList::remove_node(){
if (is_empty()) { throw ("Remove function is not valid since no node present in the list."); }
else {
--steps; /*Decrement an operation*/
CalcNode* old = cursor->next; /*Get the node-to-be-removed in a temprory pointer object*/
if (old == cursor) { cursor = nullptr; } /*if happens to be the cursor itself, jst set to null*/
else {
cursor->next = old->next;
}
delete old; /*Remove the temprory*/
}
}
/*Returns the total till it's called*/
double CalcList::total() const{
return cursor->next->nodes_total; /*Gets the total from a particular node*/
}
/*Adds a node, then allows an operation on it*/
void CalcList::newOperation(const FUNCTIONS func, const double operand) {
add_node(0); /*Add a default node first*/
switch (func) { /*then update the newly default node with operator, its unique total, & operand*/
case ADDITION:
cursor->next->nodes_operator = ADDITION;
cursor->next->nodes_operand = operand;
cursor->next->nodes_total += operand;
break;
case SUBTRACTION:
cursor->next->nodes_operator = SUBTRACTION;
cursor->next->nodes_operand = operand;
cursor->next->nodes_total -= operand;
break;
case MULTIPLICATION:
cursor->next->nodes_operator = MULTIPLICATION;
cursor->next->nodes_operand = operand;
cursor->next->nodes_total *= operand;
break;
case DIVISION: /*Throw an error if divison is bad*/
if (operand >= 0 && operand < 1.00) { removeLastOperation(); throw("Divide by zero."); }
else {
cursor->next->nodes_operator = DIVISION;
cursor->next->nodes_operand = operand;
cursor->next->nodes_total /= operand;
break;
}
default: /*Throw if enum is missing*/
throw ("Bad arithmetic operation. Use Addition, Substraction, Multiplication, or Divison.");
break;
}
}
/*nested function that removes the last node*/
void CalcList::removeLastOperation() {
remove_node();
}
/*Converts past operations to string and returns it*/
std::string CalcList::toString(unsigned short precision) const {
std::stringstream ss; /*String stream will get past operation in string buffer*/
std::string node_data_to_string = ""; /*This variable will get the converted buffer data*/
int step_back = steps; /*Temprory variable that holds the number of steps*/
CalcNode* node_ptr = new CalcNode; /*Temprory variable that holds cursor->next data*/
node_ptr = cursor->next;
ss.precision(precision); /*sets the numerical into decimal points*/
/*Simple output that mimics the example in pdf of PP1*/
while(node_ptr->nodes_total != 0){
ss << step_back << ": ";
ss << std::fixed << node_ptr->next->nodes_total; /*one before that*/
if (node_ptr->nodes_operator == ADDITION) {
ss << "+";
}
else if (node_ptr->nodes_operator == SUBTRACTION) {
ss << "-";
}
else if (node_ptr->nodes_operator == MULTIPLICATION) {
ss << "*";
}
else if (node_ptr->nodes_operator == DIVISION) {
ss << "/";
}
ss << node_ptr->nodes_operand;
ss << "=";
ss << node_ptr->nodes_total; /*One at that*/
ss << std::endl;
node_ptr = node_ptr->next;
--step_back;
}
node_data_to_string = ss.str();/*Convert and return*/
return node_data_to_string;
}