-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReallyBigDecimal.java
More file actions
137 lines (125 loc) · 4.11 KB
/
ReallyBigDecimal.java
File metadata and controls
137 lines (125 loc) · 4.11 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
import java.util.Scanner;
public class ReallyBigDecimal {
private String number;
private char sign;
private int length;
//Constructors
public ReallyBigDecimal(){
this.number = "";
this.sign = '+';
this.length = 0;
}
public ReallyBigDecimal(String _number){
assign(_number);
}
public ReallyBigDecimal(ReallyBigDecimal obj){
this.length = obj.getLength();
this.number = obj.getNumber();
this.sign = obj.getSign();
}
//Getters
public char getSign(){ return this.sign; }
public String getNumber(){ return this.number; }
public int getLength(){ return this.length; }
public int getFromBack(int i){
if(i>this.length || i==length) return -1;
else
return Integer.parseInt(""+ (this.number.charAt((this.length-1)-i)));
}
public int getFromFront(int i){
if(i>this.length || i==length) return -1;
else
return Integer.parseInt(""+ (this.number.charAt(i)));
}
public String getAllFromStartTo(int i){
return number.substring(0, i);
}
//Setters
public boolean setNumber(String _number){ return assign(_number); }
public boolean setSign(char sign){
if(sign=='+') this.sign = '+';
else if(sign=='-') this.sign = '-';
else return false;
return true;
}
public boolean assign(String _number){
if(ReallyBigDecimal.validate(_number)==false) {
throw new NumberFormatException("A Number was expected");
}
if(_number.length()>0 && _number.charAt(0)=='-'){
this.sign='-';
this.number = _number.substring(1, _number.length());
this.length = _number.length()-1;
}else if(_number.length()>0 && _number.charAt(0)=='+'){
this.sign = '+';
this.number = _number.substring(1, _number.length());
this.length = _number.length()-1;
}else if(_number.length()>0){
this.sign = '+';
this.number = _number;
this.length = _number.length();
}else if(_number.length()==0){
this.sign = '+';
this.number = "";
this.length = 0;
}
return true;
}
public boolean addAtBack(String i){
// if(i.equals("0")) return false;
if(this.length==0) assign(i);
else {
this.number = this.number + i; //(number already Present) + (New Number)
this.length += i.length();
}
return true;
}
public boolean addAtFront(String i){
// if(i.equals("0")) return false;
if(this.length==0) assign(i+"");
else {
this.number = i + this.number; //(New Number) + (number already Present)
this.length += (i+"").length();
}
return true;
}
//Display Methods
@Override
public String toString() {
if (this.sign == '-') {
return this.sign+this.number;
}else{
return this.number;
}
}
public void displayStatus() {
System.out.println("Sign : "+this.sign);
System.out.println("Number : "+this.number);
System.out.println("Length : "+this.length);
if (this.sign == '-') {
System.out.println("The Full Number : "+this.sign+""+this.number);
}
}
//Validators
public static boolean validate(String _number) {
int i;
if(_number.charAt(0)=='-' || _number.charAt(0)=='+')
i=1;
else
i=0;
for(;i<_number.length();i++)
if(Character.isDigit(_number.charAt(i))==false)
return false;
return true;
}
//Main Driver
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number : ");
ReallyBigDecimal num = new ReallyBigDecimal(sc.next());
num.displayStatus();
System.out.println("======================");
for(int i=0; i<num.length; i++) System.out.println("["+i+"] : "+num.getFromBack(i));
sc.close();
}
}