-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigNum.h
201 lines (179 loc) · 8.64 KB
/
BigNum.h
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// FILENAME: BigNum.h
// This is the definition of the BigNum class.
// CONSTRUCTORS and DESTRUCTORS for the BigNum class
//
// BigNum() : Default constructor for the BigNum class. A positive BigNum with default value 0
// is created after this constructor.
//
// BigNum(int num) : A BigNum with the same value and sign as num is created after this function.
//
// BigNum(const string& strin) : A BigNum with the corresponding integer value of string[] is
// created after this function.
//
// BigNum(const BigNum& anotherBigNum) : A BigNum with the corresponding interger value of string[] is
// created after this function.
//
// ~BigNum() : Dynamic memory allocated for the BigNum is released after this function.
//
// PUBLIC MEMBER FUNCTIONS of the BigNum class:
//
// BigNum& operator=(const BigNum& anotherBigNum) : The BigNum has been assigned the same value
// as anotherBigNum after this function is executed.
//
// BigNum& operator+=(const BigNum& addend) : Addend has been added to this BigNum after this
// function is executed.
//
// BigNum& operator-=(const BigNum& subtractand) : Subtractand has been subtracted from this
// BigNum after this function is executed.
//
// BigNum& operator*=(const BigNum& multiplicand) : This BigNum has been multiplied by multiplicand
// and the result is stored in this BigNum after this function is executed.
//
// BigNum& operator/=(const BigNum& divisor) : This BigNum has been divided by divisor and the
// result has been stored in this BigNum after this function is executed.
//
// BigNum& operator%=(const BigNum& divisor) : This BigNum has been taken modulus by divisor and
// the result has been stored in this BigNum after this function is executed.
//
// BigNum& operator++() : This BigNum has been incremented by 1 after this function is executed.
//
// BigNum& operator--() : This BigNum has been decremented by 1 after this function is executed.
//
// Private MEMBER FUNCTIONS of the BigNum class:
//
// void resize(unsigned int n) : Size of the memory allocated for this BigNum has been changed to n
// after this function is executed. If n is smaller than the length of this BigNum, this BigNum
// will not be resized. The n value will be chosen for efficiency; resizing should generally double the
// number's capacity.
//
// BigNum& diff(const BigNum& a, const BigNum& b) : Precondition is that both this BigNum
// and subtractand are positive, and also that this BigNum >= subtractand. The result of subtraction
// has been returned after this function is executed.
//
// BigNum& sum(const BigNum& a, const BigNum& b) : Precondition is that both this BigNum
// and addend are positive. The result of addition has been returned after this function is executed.
//
// BigNum& mult(const BigNum& a, const BigNum& b) : Precondition is that both this BigNum
// and addend are positive. The result of multiplication has been returned after this function is executed.
//
// FRIEND FUNCTIONS of the BigNum class:
// friend std::ostream& operator<<(std::ostream &os, const BigNum& bignum) : This bignum has been
// output to the console after this function is executed.
//
// friend std::istream& operator>>(std::istream &is, BigNum& bignum) : This BigNum entered from the
// console has been stored in bignum after this function is executed.
//
// BigNum operator+(const BigNum& a, const BigNum& b): BigNum a has been added to BigNum b, and
// neither a nor b has been changed and the result has been returned.
//
// BigNum operator-(const BigNum& a, const BigNum& b) : BigNum b has been subtracted from BigNum a
// and neither a nor b has been changed and the result has been returned after
// this function is executed.
//
// BigNum operator*(const BigNum& a, const BigNum& b) : BigNum a has been multiplied by BigNum b and
// neither a nor b has been changed and the result has been returned after
// this function is executed.
//
// BigNum operator/(const BigNum& a, const BigNum& b) : BigNum a has been divided by BigNum b and neither a
// nor b has been changed and the result has been returned after this function
// is executed.
//
// BigNum operator%(const BigNum& a, const BigNum& b) : BigNum a has been taken modulus by BigNum b and neither
// a nor b has been changed and the result has been returned after this function
// is executed.
//
// BigNum factorial() : Factorial of this BigNum has been returned after this function is executed.
//
// bool operator>(const BigNum& a, const BigNum& b) : True will be returned if BigNum a is bigger than
// BigNum b, otherwise false will be returned. BigNums a and b will not change.
//
// bool operator>=(const BigNum& a, const BigNum& b) : True will be returned if BigNum a is greater
// than or equal to anotherBigNum, otherwise false will be returned. BigNums a and b will not change.
//
// bool operator<(const BigNum& anotherBigNum) : True will be returned if if BigNum a is smaller than
// BigNum b, otherwise false will be returned. BigNums a and b will not change.
//
// bool operator<=(const BigNum& anotherBigNum) : True will be returned if if BigNum a is smaller than
// or equal to BigNum b, otherwise false will be returned. BigNums a and b will not change.
//
// bool operator==(const BigNum& anotherBigNum) : True will be returned if BigNum a is equal to
// BigNum b, otherwise false will be returned. BigNums a and b will not change.
//
// bool operator!=(const BigNum& anotherBigNum) : True will be returned if BigNum a is not equal to
// BigNum b, otherwise false will be returned. BigNums a and b will not change.
//
#ifndef HW3_BIGNUM_H
#define HW3_BIGNUM_H
#include <cstdlib> // Provides unsigned int
#include <iostream> // Provides istream and ostream
#include <cstring>
namespace HW3
{
class BigNum
{
public:
// CONSTRUCTORS and DESTRUCTORS
BigNum();
BigNum(int num);
BigNum(const std::string& strin);
BigNum(const BigNum& anotherBigNum);
~BigNum();
// MEMBER FUNCTIONS
BigNum& operator=(const BigNum& anotherBigNum);
BigNum& operator+=(const BigNum& addend);
BigNum& operator-=(const BigNum& subtractand);
BigNum& operator*=(const BigNum& multiplicand);
BigNum& operator/=(const BigNum& divisor);
BigNum& operator%=(const BigNum& divisor);
BigNum& operator++(); // overload prefix increment
BigNum& operator--(); // overload prefix decrement
// small method used in testing
char* printBigNum() const {
int i = 0;
int offset = 0;
char* result;
if (!(positive)){
result = new char[used + 2];
result[0] = '-';
offset = 1;
}else{
result = new char[used + 1];
}
for (i = used - 1; i >= 0 ; --i)
{
result[used - 1 - i + offset] = digits[i] + 48;
}
result[used + offset] = '\0';
return result;
}
// FRIEND FUNCTIONS
friend std::ostream& operator<<(std::ostream& os, const BigNum& bignum);
friend std::istream& operator>>(std::istream& is, BigNum& bignum);
friend BigNum operator+(const BigNum& a, const BigNum& b);
friend BigNum operator-(const BigNum& a, const BigNum& b);
friend BigNum operator*(const BigNum& a, const BigNum& b);
friend BigNum operator/(const BigNum& a, const BigNum& b);
friend BigNum operator%(const BigNum& a, const BigNum& b);
friend BigNum factorial(const BigNum &a);
friend bool operator>(const BigNum& a, const BigNum& b);
friend bool operator>=(const BigNum& a, const BigNum& b);
friend bool operator<(const BigNum& a, const BigNum& b);
friend bool operator<=(const BigNum& a, const BigNum& b);
friend bool operator==(const BigNum& a, const BigNum& b);
friend bool operator!=(const BigNum& a, const BigNum& b);
private:
unsigned int *digits; // Pointer to the array of BigNum digits
bool positive; // Indicates the sign of BigNum (true for positive, false for negative)
unsigned int used; // Length of the non-garbage array for BigNum digits
unsigned int capacity; // Size of the memory allocated for this BigNum
static const unsigned int DEFAULT_CAPACITY = 20;
// not optional
void resize(unsigned int n);
// optional helper functions
void trim();
BigNum& diff(const BigNum& a, const BigNum& b);
BigNum& sum(const BigNum& a, const BigNum& b);
BigNum& mult(const BigNum& a, const BigNum& b);
};
}
#endif