-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigInteger.cpp
137 lines (123 loc) · 2.82 KB
/
BigInteger.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
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
#include <iostream>
#include "BigInteger.h"
using namespace std;
//This is the function implementation cpp file. No need to reclaim class headers.
void BigInteger::setNumber(string s){
//set sign
if (s[0] == '-'){
sign = '-';
s = s.substr(1);
}
else
sign = '+';
//set length
length = s.length();
//set value, reversely set for further use.
int j = 0;
for (int i = s.length() - 1; i >= 0; i--)
value[j++] = s[i] - '0';
}
string BigInteger::getNumber() const{
//output the number
string number;
number = number + (sign == '-' ? "-" : "");
for (int i = length - 1; i >= 0; i--)
number = number + char(value[i] + '0');
return number;
}
//friend function doesn't belong to the class.
BigInteger operator+(const BigInteger & a, const BigInteger & b){
/*four cases:
1. both a and b are positive.
2. both a and b are negative.
3. a is positive and b is negative
4. a is negative and b is positive.
*/
BigInteger c;
if (a.sign == '+' && b.sign == '+'){
//set sign
c.sign = '+';
//set value
int carry = 0, i;
for (i = 0; i < a.length || i < b.length; i++){
int tmp = (i < a.length ? a.value[i] : 0) + (i < b.length ? b.value[i] : 0) + carry;
c.value[i] = tmp % 10;
carry = tmp / 10;
}
if (carry > 0)
c.value[i++] = carry;
//set length
c.length = i;
}
else if (a.sign == '-' && b.sign == '-'){
BigInteger a2 = a;
a2.sign = '+';
BigInteger b2 = b;
b2.sign = '+';
c = a2 + b2;
c.sign = '-';
/* if not strict, no const a, b
a.sign = '+';
b.sign = '+';
c = a + b;
c.sign = '-';
*/
}
else if (a.sign == '+' && b.sign == '-'){
//set value
int carry = 0, i;
for (i = 0; i < a.length || i < b.length; i++){
//carry is either 0 or -1.
int tmp = (i < a.length ? a.value[i] : 0) + 10 - (i < b.length ? b.value[i] : 0) + carry;
c.value[i] = tmp % 10;
//carry = -1 means borrowed, carry = 0 means not borrowed.
carry = tmp / 10 - 1;
}
//set length, get rid of all the 0s in the front.
c.length = i;
while (c.value[c.length - 1] == 0 && c.length > 0)
c.length--;
c.length = c.length > 0 ? c.length : 1;
//check carry bit
if (carry == 0){
c.sign = '+';
}
else{
BigInteger d;
d.sign = '+';
d.length = i + 1;
//d is 000001
for (int j = 0; j < i; j++)
d.value[j] = 0;
d.value[i] = 1;
c.sign = '-';
c = d + c;
c.sign = '-';
}
}
else if (a.sign == '-' && b.sign == '+'){
c = b + a;
}
return c;
}
istream & operator>>(istream & cin, BigInteger & a){
string s;
cin >> s;
a.setNumber(s);
}
ostream & operator<<(ostream & cout, const BigInteger & a){
cout << a.getNumber();
}
BigInteger & operator++(BigInteger & a){
BigInteger b;
b.setNumber("1");
a = a + b;
return a;
}
BigInteger operator++(BigInteger & a, int usless){
BigInteger origin = a;
BigInteger one;
one.setNumber("1");
a = a + one;
return origin;
}