NOTE: You must allow for an infinite number of digits to be added, so a user cou
ID: 3863564 • Letter: N
Question
NOTE: You must allow for an infinite number of digits to be added, so a user could add for example, " a 1 with 1 million zeroes behind it plus a 8 with 300 random numbers behind it"
Design a class named largeIntegers such that an object of this class can store any number of digits (i.e. more than 20 digits). Add operations to add, subtract, multiply, and compare integers stored in two objects and overload the corresponding operators. You should also have functions to set, retrieve, and print the objects. Write a client program to test your implementation.
I asked a similar question beforehand and everybody looks past the fact that you need to add well over 20 digits and I am confused about how to go about this, so any help would be great.
Explanation / Answer
Dear friend ,
Here are my suggestions hope they will be useful for you.
To accept any length integer we can create linked list of entered digit of any length.
It would be great if we can create doubly linkedlist so that we can travel from last digit.
Eg:
Say if we want to accept integer 1234567893233
So we will create linked list of number like
Head1->1->2->3->4->5->6->7->8->9->3->2->3-3->NULL
Another integer is 232345623423
Head2->2>3>2>3>4->5->6->2->3->4->2->3->NULL
TO add two number we will start from end digit
Head1->1->2->3->4->5->6->7->8->9->3->2->3->3->NULL
Head2->0->2->2>3>2>3>4->5->6->2->3->4->2->3->NULL
So we need to add element of first linked list +element of second linked list +carry
(carry will be zero initially)
So digit in answer is say Ans_Digit=(element of first linked list +element of second linked list +carry)%10
And carry would be (element of first linked list +element of second linked list +carry) % 10
Traverse linked linked list in backword directon. Until head of any linkedlist is reached.
Similar approach can be taken for subtraction and comparison function.
Hope this approach helps you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.