-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwholeNumber.cpp
98 lines (87 loc) · 2.53 KB
/
wholeNumber.cpp
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
#include "wholeNumber.h"
#include "list.h"
#include <iostream>
#include <iomanip>
std::ostream &operator<<(std::ostream &out, const WholeNumber &rhs)
{
int num = 0; // count for the comma
// traverse through the nodes
for (custom::list<int>::const_iterator it = rhs.data.cbegin(); it != rhs.data.cend(); ++it, ++num)
{
if (num > 0)
{ // comma if there is another set of 3
std::cout << ",";
std::cout << std::setfill('0') << std::setw(3) << *it;
}
else
{ // no comma if there isn't another set of 3
std::cout << *it;
}
}
// output the numbers in each node
return out;
}
// fetch a given number
std::istream &operator>>(std::istream &in, WholeNumber &rhs)
{
int value;
std::cin >> value;
if (std::cin)
{
rhs.data.push_back(value);
char separator;
while (std::cin >> separator >> value && separator == ',')
{
rhs.data.push_back(value);
}
}
if (!std::cin.eof())
{
std::cerr << "format error in input" << std::endl;
}
return in;
std::ostream &operator<<(std::ostream &out,
const WholeNumber &rhs);
// fetch a given number
std::istream &operator>>(std::istream &in, WholeNumber &rhs);
return in;
}
// add onto a given whole number
WholeNumber &WholeNumber::operator+=(const WholeNumber &rhs)
{
if (rhs.data.empty())
return *this;
if (this->data.empty())
{
data = rhs.data;
return *this;
}
WholeNumber newNumber;
newNumber.data.clear();
list_type::reverse_iterator it1 = data.rbegin();
list_type::const_reverse_iterator it2 = rhs.data.crbegin();
int carryOver = 0;
while (it1 != data.rend() || it2 != rhs.data.crend())
{
int v1 = it1 == data.rend() ? 0 : *(it1++);
int v2 = it2 == data.crend() ? 0 : *(it2++);
int total = v1 + v2 + carryOver;
int newValue = total % 1000;
carryOver = total >= 1000 ? 1 : 0;
newNumber.data.push_front(newValue);
}
//if we had a carry over left at the end, then we
//need to add a new bucket for it
if (carryOver > 0)
newNumber.data.push_front(carryOver);
this->data = newNumber.data;
return *this;
}
// assignment operator for numbers
WholeNumber &WholeNumber::operator=(unsigned int value)
{
//bad implementation here, assumes that it's less than 3 digits or the rest won't work.
data.clear();
data.push_back(value);
return *this;
}