-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongInt.java
286 lines (257 loc) · 11 KB
/
LongInt.java
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// LongInt ADT for unbounded integers
public class LongInt {
// Attributes
String value;
boolean isPositive;
// Constructor
public LongInt() {}
public LongInt(String value) {
this.value = value;
this.isPositive = value.charAt(0) != '-';
}
// Getters and setters
public String getValue() {
return value;
}
public boolean isPositive() {
return isPositive;
}
public void setValue(String value) {
this.value = value;
this.isPositive = value.charAt(0) != '-';
}
public void setPositive(boolean positive) {
isPositive = positive;
}
public void pushValue(String pushedString){
this.value = pushedString + this.value;
}
private String invertString(String str) {
StringBuilder stringBuilder = new StringBuilder(str);
stringBuilder.reverse();
return stringBuilder.toString();
}
// returns 'this' + 'opnd'; Both inputs remain intact.
public LongInt add(LongInt opnd) {
// Result
LongInt result = new LongInt();
String result_string = "";
// We take the string values and store it in new variables as we need to maintain them intact
String thisValue = this.getValue();
String opndValue = opnd.getValue();
String tmp;
int carry = 0;
// Take out the sign for operations
if(!this.isPositive) thisValue = thisValue.substring(1);
if(!opnd.isPositive) opndValue = opndValue.substring(1);
// One number is positive and the other is negative
if((!this.isPositive && opnd.isPositive) || (this.isPositive && !opnd.isPositive)) {
// Check which number is larger depending on the size and the sign
// Change places with tmp if necessary
if(thisValue.length() == opndValue.length()) {
for(int i = 0; i < thisValue.length(); i++) {
if((thisValue.charAt(i)-'0') < (opndValue.charAt(i)-'0') && !opnd.isPositive){
result.setPositive(false);
tmp = thisValue;
thisValue = opndValue;
opndValue = tmp;
}
if((thisValue.charAt(i)-'0') > (opndValue.charAt(i)-'0') && !this.isPositive) result.setPositive(false);
}
}else if(thisValue.length() > opndValue.length() && !this.isPositive) result.setPositive(false);
else{
if(!opnd.isPositive) result.setPositive(false);
tmp = thisValue;
thisValue = opndValue;
opndValue = tmp;
}
// SUBTRACTION
for(int i = opndValue.length() - 1; i >= 0; i--) {
int sub = ((thisValue.charAt(i+thisValue.length()-opndValue.length()) - (int)'0') - (opndValue.charAt(i) - (int)'0') - carry);
if(sub >= 0) carry = 0;
else {
sub += 10;
carry = 1;
}
result_string += String.valueOf(sub);
}
for(int i = thisValue.length() - opndValue.length()-1; i >= 0; i--) {
if(thisValue.charAt(i) == '0' && carry > 0) result_string += "9";
int subString = ((int)(thisValue.charAt(i)) - (int)'0') - carry;
if(subString <= 0 && i == 0 ) result_string += String.valueOf(subString);
}
// Result is ZERO
if(result_string.equals("0")){
result.setValue("0");
return result;
}
// Result ends in ZERO but has additional zeros to be deleted
String invertedString = invertString(result_string);
int zeros = 0;
while(zeros < invertedString.length()-1 && invertedString.charAt(zeros) == '0') zeros++;
// Assignment and return
invertedString = invertedString.substring(zeros);
if(!result.isPositive()) { invertedString = "-" + invertedString; }
result.setValue(invertedString);
return result;
}
// Compare the numbers based on length, all the other cases are considered above
if(thisValue.length() > opndValue.length()) {
tmp = thisValue;
thisValue = opndValue;
opndValue = tmp;
}
// ADDITION
carry = 0;
for(int i = thisValue.length()-1; i >= 0; i--) {
int sum = ((thisValue.charAt(i)-'0') + (opndValue.charAt(i+opndValue.length() - thisValue.length())-'0') + carry);
result_string += (char)(sum % 10 + '0');
carry = sum / 10;
}
for(int i = opndValue.length()-thisValue.length()-1; i >= 0; i--) {
int sum = ((opndValue.charAt(i)-'0') + carry);
result_string += (char)(sum % 10 + '0');
carry = sum / 10;
}
// ADD overflow if necessary and return
if(carry != 0) result_string += (char)(carry + '0');
if(this.isPositive) result.setValue(invertString(result_string));
else result.setValue(invertString(result_string));
return result;
}
// returns 'this' - 'opnd'; Both inputs remain intact.
public LongInt subtract(LongInt opnd) {
LongInt result = new LongInt();
String result_string = "";
String thisValue = this.getValue();
String opndValue = opnd.getValue();
String tmp;
// Removing signs
if(!this.isPositive) thisValue = thisValue.substring(1);
if(!opnd.isPositive) opndValue = opndValue.substring(1);
if((!this.isPositive && opnd.isPositive) || (this.isPositive && !opnd.isPositive)) {
boolean flag = false;
if (thisValue.length() < opndValue.length()) {
tmp = thisValue;
thisValue = opndValue;
opndValue = tmp;
}
LongInt num1 = new LongInt(thisValue);
LongInt num2 = new LongInt(opndValue);
if((!this.isPositive && flag) || (!opnd.isPositive && flag)){
if (!this.isPositive && flag) {
num2 = new LongInt("-" + opndValue);
}
if (!opnd.isPositive && flag)
num1 = new LongInt("-" + thisValue);
}
result = num2.add(num1);
if(!this.isPositive) { result_string = "-" + result_string; }
result.value = result_string;
return result;
}
if(this.isPositive && opnd.isPositive) {
if(thisValue.length() < opndValue.length()) {
result.setPositive(true);
tmp = thisValue;
thisValue = opndValue;
opndValue = tmp;
} else if(thisValue.length() == opndValue.length()) {
for(int i = 0; i < thisValue.length(); i++) {
if((thisValue.charAt(i) - '0') > (opndValue.charAt(i) - '0')) result.setPositive(true);
else if((thisValue.charAt(i) - '0') < (opndValue.charAt(i) - '0')) {
result.setPositive(false);
tmp = thisValue;
thisValue = opndValue;
opndValue = tmp;
}
}
}
}else{
if(thisValue.length() > opndValue.length()) result.setPositive(false);
else if(thisValue.length() < opndValue.length()) {
result.setPositive(true);
tmp = thisValue;
thisValue = opndValue;
opndValue = tmp;
}else {
for(int i = 0; i < thisValue.length(); i++) {
if((thisValue.charAt(i) - '0') > (opndValue.charAt(i)) - '0') result.setPositive(false);
else if((thisValue.charAt(i) - '0') < (opndValue.charAt(i)) - '0') {
result.setPositive(true);
tmp = thisValue;
thisValue = opndValue;
opndValue = tmp;
}
}
}
}
// SUBTRACTION
int carry = 0;
for(int i = opndValue.length() - 1; i >= 0; i--) {
int digitSub = ((thisValue.charAt(i+thisValue.length()-opndValue.length()) - (int)'0') - (opndValue.charAt(i) - (int)'0') - carry);
if(digitSub >= 0) carry = 0;
else{
digitSub += 10;
carry = 1;
}
result_string += String.valueOf(digitSub);
}
for(int i = thisValue.length() - opndValue.length()-1; i >= 0; i--) {
if(thisValue.charAt(i) == '0' && carry > 0) result_string += "9";
int digitSub = ((int)(thisValue.charAt(i)) - (int)'0') - carry;
if(i > 0 || digitSub > 0) result_string += String.valueOf(digitSub);
carry = 0;
}
if(result_string.equals("0")) return new LongInt("0");
int zeros = 0;
while(zeros < invertString(result_string).length()-1 && invertString(result_string).charAt(zeros) == '0') zeros++;
if(!result.isPositive) { result.setValue("-" + invertString(result_string).substring(zeros)); }
else result.setValue(invertString(result_string).substring(zeros));
return result;
}
// returns 'this' * 'opnd'; Both inputs remain intact.
public LongInt multiply(LongInt opnd) {
LongInt result = new LongInt();
// Multiplication by 0
if(this.getValue().equals("0") || opnd.value.equals("0")) {
result.setValue("0");
return result;
}
String thisValue = this.getValue();
String opndValue = opnd.getValue();
String str_result = "";
// Taking the signs for operations
if(!this.isPositive) thisValue = thisValue.substring(1);
if(!opnd.isPositive) opndValue = opndValue.substring(1);
// MULTIPLICATION
int num1 = 0;
int num2;
int[] resultArray = new int[thisValue.length() + opndValue.length()];
for(int i = opndValue.length()-1; i >= 0; i--) {
int carry = 0;
int digit1 = opndValue.charAt(i) - '0';
num2 = 0;
for(int j = thisValue.length()-1; j >= 0; j--) {
int digit2 = thisValue.charAt(j) - '0';
int digitSum = digit1 * digit2 + carry + resultArray[num1+num2];
resultArray[num1 + num2] = digitSum % 10;
carry = digitSum / 10;
num2++;
}
resultArray[num1 + num2] += carry;
num1++;
}
// Ignore zeros at the right
int i = resultArray.length-1;
while(i >= 0 && resultArray[i] == 0) i--;
while(i >= 0) str_result += (resultArray[i--]);
if((!this.isPositive && opnd.isPositive) || ((this.isPositive && !opnd.isPositive))) result.setValue("-" + str_result);
else result.setValue(str_result);
return result;
}
// print the value of 'this' element to the standard output.
public void print() {
System.out.println(this.getValue());
}
}