write a Number class which will allow you to do arithmetic with unlimited digits and accuracy. As an example, this class was used to compute 1000! as:
40238726007709377354370243
3923003985
7193748642
1071463254
3799910429
9385123986
2902
05920442084869694048004799
8861019719
6058631666
8729948085
5890132382
9669944590
9974
24504087073759918823627727
1887325197
7950595099
5276120874
9754624970
4360141827
8094
64649629105639388743788648
7337119181
0458257836
4784997701
2476632889
8359557354
3251
31853239584630755574091142
6241747434
9347553428
6465766116
6779739666
8820291207
3791
43853719588249808126867838
3745597317
4613608537
9534524221
5865932019
2809087829
7308
43139284440328123155861103
6976801357
3042161687
4760967587
1348312025
4785893207
6716
91324484262361314125087802
0800026168
3151027341
8279777047
8463586817
0164365024
1536
91398281264810213092761244
8963599287
0511496497
5419909342
2215668325
7208082133
3186
11681155361583654698404670
8975602900
9505376164
7584772842
1889679646
2449451607
6535
34081989013854424879849599
5331910172
3355556602
1394503997
3628075013
7837615307
1277
61926849034352625200015888
5351473316
1170210396
8175921510
9077880193
9317811419
4545
25722386554146106289218796
0223838971
4760885062
7686296714
6674697562
9112340824
3920
81601537808898939645182632
4367161676
2179168909
7799119037
5403127462
2289988005
1954
44414282012187361745992642
9565817466
2830295557
0299024324
1531816172
1046583203
6786
90611726015878352075151628
4225540265
1704833042
2614397428
6933061690
8979684825
9012
54583271682264580665267699
5865268227
2807075781
3918581788
8965220816
4348344825
9932
66043367660176999612831860
7883861502
7946595513
1156552036
0939881806
1213855860
0301
43569452722420634463179746
0594682573
1037900840
2443243846
5657245014
4028218852
5247
09351906209290231364932734
9756551395
8720559654
2287497740
1141334696
2715422845
8623
77387538230483865688976461
9273838149
0014076731
0446640259
8994902222
2176590433
9901
88601856652648506179970235
6193897017
8600408118
8972991831
1021171229
8459016419
2106
88843871218556461249607987
2290851929
6819372388
6426148396
5738229112
3125024186
6493
53143970137428531926649875
3372189406
9428143411
8520158014
1233448280
1505139969
4290
15348307764456909907315243
3278288269
8646027898
6432113908
3506217095
0025973898
6355
42771967428222487575867657
5234422020
7573630569
4988250879
6892816275
3848863396
9099
59826280956121450994871701
2445164612
6037902930
9120889086
9420285106
4018215439
9457
15680594187274899809425474
2173582401
0636774045
9574178516
0829230135
3580818400
9699
63725242305608559037006242
7124341690
9004153690
1059339838
3577793941
0970027753
4720
00000000000000000000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000
00000000000000000000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000
00000000000000000000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000
00000000
You will write a (rather feeble) calculator with only a few operations which will do basic arithmetic with these numbers. Running the calculator may give a display like:
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> e
value: 3.141592653589793238462643
3832795028
8419716939
937511
3.141592653589793238462643
3832795028
8419716939
937511
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> m
value: -20000
-62831.8530717958647692528
6766559005
7683943387
9875022000
0
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> a
value: .853071
-62831.0000007958647692528
6766559005
7683943387
9875022
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> c
0
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> e
value: .0000000000000000000000001
.0000000000000000000000001
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> r
-.000000000000000000000000
1
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> m
value: 200000
-.000000000000000000020000
0
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> s
value: -1
.99999999999999999998
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
-> q
You will note that your program will not do division. You can be thankful for this.
The Number class
Numbers will be stored in doubly-linked lists (do not use generics here). Each node will have an int value field which will hold one digit (0 through 9) and two pointer fields, prev and next. The Number class will have five fields:
private Node low, high;
private int digitCount = 0;
private int decimalPlaces = 0;
private boolean negative = false;
high points to the high-order digit's node, low points to the low-order digit's node, digitCount is the number of digits stored in the list, decimalPlaces is the number of digits (nodes) after the decimal place and negative gives the sign. For example, the number 3.1416 would be represented as:
A number of operations will be provided by public methods:
* public Number(). A constructor which makes an "empty" Number (with no digits).
* public Number(String str). A constructor which takes a String representation of a number (e.g. "-21.507"). Calls accept.
* public void accept(String str). Builds a list representation of the number represented by the string.
* public Number add(Number n). Returns a Number which represents "this + n".
* public Number subtract(Number n). Returns a Number which represents "this - n".
* public Number multiply(Number n). Returns a Number which represents "this * n".
* public void reverseSign(). Reverses the value of negative.
* public String toString(). This returns a String representation of the number. It allows you to display a Number using System.out.print().
There will be no other public methods, but you may provide any private methods you wish.
write methods to manipulate the doubly linked list
need private void insertLow(int digit) and private void insertHigh(int digit) to create and insert new nodes at each end of the list.
write (private) Number addAbsolute(Number n), Number subtractAbsolute(Number n) and int compareToAbsolute(Number n) methods which will perform operations disregarding signs (i.e. the negative field). These can then be called by the public Number add(Number n) and public Number subtract(Number n) methods. The trickiest part of these methods is aligning the decimal points.
create a new (empty) Number sum
int carry = 0
thisPtr = low
nPtr = n.low
while (thisPtr != null)
add the values stored in the pointed to nodes plus the carry to
get int newDigit
store newDigit % 10 in a new node inserted at the head of sum
sum.digitCount++
store newDigit / 10 in carry
thisPtr = thisPtr.getPrev()
nPtr = nPtr.getPrev()
if (carry != 0)
store carry (1) in new node at the head of sum
sum.digitCount++
set appropriate value for decimalPlaces in sum
create a new (empty) Number difference
int borrow = 0;
thisPtr = low
nPtr = n.low
while (thisPtr != null)
subtract the pointed to digits and subtract borrow from the result
to get int newDigit
if (newDigit < 0)
newDigit += 10
borrow = 1
else
borrow = 0
store newDigit in a new node inserted at the head of difference
difference.digitCount++
set appropriate value for decimalPlaces in difference
This code must be adjusted to align decimal points and account for different digit counts.
Multiplication is done in a slightly different way from pencil and paper. We read the first Number to be multiplied from-right-to-left and the second from-left-to-right using "Horner's method."
create an empty Number product
let d be the high-order digit of n
do
multiply (all of) this by d in a right-to-left manner (with a carry)
similar to addAbsolute() to get a Number partialProduct
multiply product by 10 (just append a new low-order digit 0)
use addAbsolute() to add partialProduct to product
let d be the next digit to the right in n
until you have used all digits of n
decimalPlaces for product will be the sum of decimalPlaces for this and n
set product.negative to get the appropriate sign
How can I start with program? Can you help me.