-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBigInt.java
More file actions
144 lines (113 loc) · 3.17 KB
/
BigInt.java
File metadata and controls
144 lines (113 loc) · 3.17 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
package ssj.algorithm.math;
import com.google.common.base.Preconditions;
import ssj.algorithm.collections.LinkedList;
import ssj.algorithm.string.StringBuilder;
import java.util.Comparator;
/**
* 使用链表实现BigInt
* Created by shenshijun on 15/2/3.
*/
public class BigInt implements Comparator<BigInt> {
private LinkedList<Character> _values;
private boolean _sign;
public BigInt(int origin) {
_values = new LinkedList<>();
_sign = (origin >= 0);
addTailBit(Math.abs(origin));
}
public BigInt() {
_values = new LinkedList<>();
_sign = true;
addTailBit(0);
}
public void setPositive() {
_sign = true;
}
public void setNegative() {
_sign = false;
}
public int bitCount() {
return _values.size();
}
public void setBit(int index, int bit) {
Preconditions.checkPositionIndex(index, bitCount());
Preconditions.checkArgument(bit >= 0 && bit < 10);
_values.set(index, intToChars(bit)[0]);
}
private char[] intToChars(int number) {
String number_string = String.valueOf(number);
return number_string.toCharArray();
}
public void addTailBit(int n) {
Preconditions.checkArgument(n >= 0);
for (char c : intToChars(n)) {
_values.add(c);
}
}
public void addHeadBit(int n) {
Preconditions.checkArgument(n >= 0);
for (char c : intToChars(n)) {
_values.appendHead(c);
}
}
@Override
public int compare(BigInt o1, BigInt o2) {
// return o1.sub(o2);
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BigInt bigInt = (BigInt) o;
if (_sign != bigInt._sign) return false;
if (!_values.equals(bigInt._values)) return false;
return true;
}
@Override
public int hashCode() {
int result = _values.hashCode();
result = 31 * result + (_sign ? 1 : 0);
return result;
}
public boolean isPositive() {
return _sign;
}
public boolean isNegative() {
return !isPositive();
}
public BigInt add(BigInt other) {
BigInt result = new BigInt();
if (other.isNegative() && isPositive()) {
other.setPositive();
return sub(other);
} else if (other.isPositive() && isNegative()) {
// other.add()
}
return null;
}
public BigInt sub(BigInt other) {
return null;
}
public BigInt mul(BigInt other) {
return null;
}
public BigInt div(BigInt other) {
return null;
}
private LinkedList<Character> _add(LinkedList<Character> other) {
return null;
}
private LinkedList<Character> _sub(LinkedList<Character> other) {
return null;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(_values.size());
sb.add(_sign ? '+' : '-');
for (Character c : _values) {
sb.add(c);
}
return sb.toString();
}
}