Hey guys appreciate u can help to do this:
Write a program to implement linked lists to handle large integers.
Since we cannot store big integers in any of the primitive types, the only way is to manage
each digit as a node in the linked list.
Take care that the index starts at the rightmost side of the string. Therefore, depending on
the length of the large integer, the digit at index 0 is the least significant digit of the large
integer. A visualization of a typical integer say 1,234,567,890 is as follows:
String 1 2 3 4 5 6 7 8 9 0
Index [9] [8] [7] [6] [5] [4] [3] [2] [1] [0]
Therefore, to represent two such numbers for the purpose of adding & subtracting them, we
need 2 similar linked lists, each list storing the numbers individually.
To create the list, you have to read the large integer as a string. This is because reading the
number as an integer or long type would not be possible as they are not enough to store the
large integer. After having the large integer as a string, extract each number and store as
them into the list.
Adding and Subtracting Big Integers
After you have the two large integers stored as lists, you can proceed to write the code for
adding and subtracting large integers. Since adding and subtracting is a commonly used
routine, create the operations in 2 separate functions. The functions should take in 2
operands as list objects, and return a 3rd list object containing the result.
The algorithm for adding is as follows:
For each object in list do
If list contains object at index i for both lists
add integers stored in the 2 objects and carry value
set carry to 0
If result > 10
set carry to 1
minus 10 from result
EndIf
insert result in new list
Else
add integer stored in longer list to carry
insert result in new list
EndIf
End for
The algorithm for subtracting is as follows:
For each object in list do
If list contains object at index i for both lists
subtract carry from first integer
set carry to 0
If first integer < second integer
set carry to 1
add 10 to first integer
EndIf
subtract second integer from first integer
insert result in new list
Else
subtract carry from first integer
insert result in new list
EndIf
End for
The above algorithms only serve as a guide and do not work for all cases. Hence, you need
to enhance the algorithms to ensure that the addition and subtraction works well for all cases.
Specifications
Your program will prompt the user to enter the name of a text file, computes the addition or
subtraction of large integers and display the results on screen.
As an illustration, suppose the input text file (test.txt) consists of the following data:
12345678901234567890 + 987654321
12345678901234567890 – 987654321
567 + 123
567 – 12
:
Your program shall display the following output:
12345678901234567890 + 987654321 = 12345678902222222211
12345678901234567890 – 987654321 = 12345678900246913569
567 + 123 = 690
567 – 12 = 555
:
Additional Notes:
• Program must be able to accept operands of varying length in the text file.
• For subtraction, if operand 1 is smaller than operand 2, you are to compute
accordingly, e.g. 7 – 9 = –2
Good challenge n happy coding!