-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReallyBig.java
More file actions
82 lines (68 loc) · 2.82 KB
/
Copy pathReallyBig.java
File metadata and controls
82 lines (68 loc) · 2.82 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
import java.util.Scanner;
public class ReallyBig {
private static boolean debug = true;
public static ReallyBigDecimal add(ReallyBigDecimal num1, ReallyBigDecimal num2){
ReallyBigDecimal ret = null;
if(num1.getSign()=='-' && num2.getSign()=='+')
ret = subtract(num2,num1);
if(num2.getSign()=='-' && num1.getSign()=='+')
ret = subtract(num1, num2);
else{
ret = new ReallyBigDecimal();
int len1 = num1.getLength();
int len2 = num2.getLength();
int i=0, j=0, ans=0, carry=0,temp=0;
while(true){
try {
// if(i>len1 || j>len2){
// if(carry==0){
// String allElse = "";
// if(i>len1 && j>len2) break;
// else if(i>len1 && j<len2) allElse = num2.getAllFromStartTo(j-1);
// else if(i<len1 && j>len2) allElse = num1.getAllFromStartTo(i-1);
// ret.addAtFront(allElse);
// break;
// }
// }
if(j>=len2 && carry==0){
if(i<=len1)
ret.addAtFront(num1.getAllFromStartTo(len1-i));
break;
}
if(i>=len1 && carry==0){
if(j<=len2)
ret.addAtFront(num2.getAllFromStartTo(len2-j));
break;
}
if(i<len1)
temp = num1.getFromBack(i);i++;
if(j<len2)
temp += num2.getFromBack(j);j++;
if(carry!=0) temp+= carry;
ans = temp%10;
carry = temp/10;
ret.addAtFront(ans+"");
temp=0;
} catch (Exception e) {
if(debug == true) System.out.println("\n============\n Error\n============\n");
return null;
}
}
}
return ret;
}
private static ReallyBigDecimal subtract(ReallyBigDecimal num1, ReallyBigDecimal num2) {
return null;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1st Number : ");
ReallyBigDecimal num1 = new ReallyBigDecimal(sc.nextLine());
System.out.println("Enter 2nd Number : ");
ReallyBigDecimal num2 = new ReallyBigDecimal(sc.nextLine());
ReallyBigDecimal answer = ReallyBig.add(num1, num2);
if(answer == null) System.out.println("Its Null");
else answer.displayStatus();
sc.close();
}
}