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!

Recommended Answers

All 10 Replies

> Write a program to implement linked lists to handle large integers.
Good for you, come back when you've made an effort.

And read this
http://www.daniweb.com/forums/announcement8-2.html

> Good challenge n happy coding!
Or just go away and fail the course. You're simply not up to completing it.
This is just a gentle stroll through the foothills, you haven't even seen the mountains yet.

This is for you: Click Here

This is for you: Click Here

Why even bother clicking it? You can see what it says just by hovering over it XD

>Why even bother clicking it? You can see what it says just by hovering over it XD
Oh yeah!! But I can bet you did opened it. Try hovering now

>Why even bother clicking it? You can see what it says just by hovering over it XD
Oh yeah!! But I can bet you did opened it. Try hovering now

yeah ~i just wanna make it my home page so it's gonna be a kinda life-time warnning thing ~ u k now :)

>Why even bother clicking it? You can see what it says just by hovering over it XD
Oh yeah!! But I can bet you did opened it. Try hovering now

OK,Here is the code i want to read the list in order to figure out some basic relationship between list and its elements this code dosen't seem work :

#include <iostream>
#include <list>
#include <cstring>
#include <conio.h>
using namespace std;

int main()
{
list<string> listA="12345678901234567890";
list<string> listB="987654321";
list<int> ::iterator p;


cout << "The contents of " << listA << " : ";
for(p = listA.begin(); p!= listA.end(); p++)
cout << *p << " ";
cout<<endl;

getch();



return 0; 
}

conio.h is an EXTINCT header file.And we don't even want to save it.
Daniweb has several discussions regarding not to use:

getch();
system("pause");

for looking at the output.
But instead to use:

cin.get();

You are doing nothing but printing a string here:

for(p = listA.begin(); p!= listA.end(); p++)
cout << *p << " ";

Instead try to get numeric value from it and manipulate it because you want to build a calculator remember?

conio.h is an EXTINCT header file.And we don't even want to save it.
Daniweb has several discussions regarding not to use:

getch();
system("pause");

for looking at the output.
But instead to use:

cin.get();

You are doing nothing but printing a string here:

for(p = listA.begin(); p!= listA.end(); p++)
cout << *p << " ";

Instead try to get numeric value from it and manipulate it because you want to build a calculator remember?

what i read from book is that list is a kinda object that consist of a group of object in the same type.Since i have to store the data as string (ohter type is just not enough to store large integer). How do i convert the element to integer digit by digit and calculate them and convert back to string ? Does index play a role here? how to use it ?i just dont know where to start

It was you who started with the string.I would have used :

list<int> integer_list;
list<int>::iterator i;
for(i=integer_list.begin(); i != integer_list.end(); ++i){...}

instead.
for my work.Well you can get some help here.

Yes of course,use the indexes for traversal and other work.What else do you use it for by the way??? :?:

This is almost like a Implementation that I have made a few days ago. Though My Objective was not on Addition and Subtraction But Multiplication of Large Positive Integers.

mimio, For doing this ,
I think the main objective here is to teach addition or subtraction to the computer.

Just think about How Humans Add Or Subtract Numbers.

for example :
2350
+1345
---------
3695
---------

Now what will our objective be?

First we need to get in the number from input.
-Then put each character into your list.

That would do the input part.

Now when it comes to Addition we will need to add The corresponding digits from the end of the loop.
You will also need to find a way for handling values above 10 once 2 corresponding digits are added.

Those will be your objectives.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.