Pages: 12
I have tried to understand the concept of linked lists and I have read the assigned chapter 2 times. My teacher is a little laid back when it comes to teaching! This is only a portion of my program. This function is supposed to add 2 binary numbers 11101+1101 and store the result in the temp list. The answer I get is 10000.I don't think that it is adding the carry. If there is anything wrong with the way I have asked this question, please let me know!

binary operator+( binary num1, binary num2)
{
binary temp;
int carry, sum;
nodetype *current1, *current2, *newnode;
carry = 0;
while(current1!=NULL && current2!=NULL)
{
newnode=new nodetype;
sum = current1->info + current2->info + carry;
carry = carry/2;
}

while(current1!=NULL)
{
newnode=new nodetype;

sum = current1->info + carry;
carry = carry/2;

}
while (current2!=NULL)
{
newnode=new nodetype;

sum = current2->info + carry;
carry = carry/ 2;

}

if (carry)
{
newnode=new nodetype;
newnode->info=carry;
}

return temp;

}
`carry = carry/2;`
Should it not be `carry = sum/2;` ?
That is what I had at first and my teacher changed it! I will try this.

Yea it didn't work. Should I post all of my code? I don't think that it is possible that any other code has errors.

One thing that I notice is that whenever I change the sum or the carry. The out put sum is 10000 no matter what! Something is ignoring those 2 variables.
Last edited on

`carry = sum - (sum%2);` ?
That doesn't work either. Is there possibly something masking the carry? LIke whatever I do, I get this in the outfile.

1 1 1 0 1
1 1 0 1
1 0 0 0 0
Try 0 0 0 0 0 with 0 0 0 0 0
I'm sorry, come again?

Never mind I understand ha. I will try this!

I get 0 0 0 0 0 .
Last edited on
I don't see what's wrong in your code.
You should try to debug it step by step.

( btw, the correct formula is `carry = sum/2;` )
Would posting the whole program help? When you say debug, you mean trace through everything right?

Is my reverselist ok?

void reverselist(nodetype*& mylist)
{
nodetype *templist, *tempnode;

templist = NULL;
while(mylist !=NULL)
{
tempnode = mylist;

templist = tempnode;

}
mylist = templist;

}
Last edited on
Yep, I mean trace through everything.
If it's not too big, posting the whole program would indeed help.

Hehe...I'll let you determine the size ;)

#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
#include "binary.h"

ostream & operator<<(ostream & outfile, const binary & outclass)
{
nodetype *current;
while(current!=NULL)
{
outfile<<current->info<<" ";
}

return outfile;
}
istream & operator>>(istream & infile, binary & inclass)
{

nodetype *newnode,*last;
char ch;
int intch;
last=NULL;
infile>>ws;
infile.get(ch);

while( isdigit(ch)and infile)
{

intch = ch -'0';

newnode= new nodetype;
newnode->info=intch;
{

}
else
{

}
last=newnode;
infile.get(ch);

}

return infile;
}
binary operator+( binary num1, binary num2)
{
binary temp;
int carry, sum;
nodetype *current1, *current2, *newnode;
carry = 0;
while(current1!=NULL && current2!=NULL)
{
newnode=new nodetype;
sum = current1->info + current2->info + carry;
carry = sum/2;
}

while(current1!=NULL)
{
newnode=new nodetype;

sum = current1->info + carry;
carry = sum/2;

}
while (current2!=NULL)
{
newnode=new nodetype;

sum = current2->info + carry;
carry = sum/2;

}

if (carry)
{
newnode=new nodetype;
newnode->info=carry;
}

return temp;

}
binary::binary()
{
count=0;

}
binary::binary(const binary & inclass)
{
nodetype *tail_ptr;

}
binary::~binary()
{

}
const binary & binary::operator =(const binary & otherlist)
{
nodetype *tail_ptr;
if(this != &otherlist)
return otherlist;

}
int binary::getcount() const
{

}
{
nodetype *removeptr;
{
delete removeptr;
}
}
void list_copy(const nodetype*source_ptr, nodetype*& head_ptr, nodetype*& tail_ptr)
{
nodetype *temp;
tail_ptr=NULL;
if(source_ptr==NULL)
return;

while(source_ptr !=NULL)
{
temp= new nodetype;
temp->info=source_ptr->info;
}
}
void reverselist(nodetype*& mylist)
{
nodetype *templist, *tempnode;

templist = NULL;
while(mylist !=NULL)
{
tempnode = mylist;

templist = tempnode;

}
mylist = templist;

}

Here is the main program code for the most part.

infile>>binobj1;
while(infile)
{
infile>>binobj2;
outfile<<binobj1<<endl;
outfile<<binobj2<<endl;
outfile<<binobj1+binobj2<<endl;

infile>>binobj1;
}

I tried your code and it works correctly.
The problem comes from elsewhere.
Could you explain?

My infile is

11101 1101

and my outfile comes to be

1 1 1 0 1
1 1 0 1
1 0 0 0 0

my main is

using namespace std;
#include "binary.cpp"
#include <iostream>
#include <fstream>
int main()
{

ifstream infile;
ofstream outfile;
binary binobj1;
binary binobj2;
infile.open("program2in.txt");
if(!infile)
{
cout << "Failure to open program2in.txt." << endl;
system ("pause");
return 1;
}
outfile.open("program2out.txt");
if(!infile)
{
cout << "Failure to open program2out.txt." << endl;
system ("pause");
return 1;
}

infile>>binobj1;
while(infile)
{
infile>>binobj2;
outfile<<binobj1<<endl;
outfile<<binobj2<<endl;
outfile<<binobj1+binobj2<<endl;

infile>>binobj1;
}

infile.close();outfile.close();
system ("pause");
return 0;
}

Last edited on
I tried the code you posted and I found no problem with it.
The problem necessarily comes from the code you've not posted.

Here's the code I used:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256`` ``````#include #include #include using namespace std; struct nodetype { nodetype* link; int info; }; class binary { public: nodetype* head_ptr; int count; binary(); binary(const binary & inclass); ~binary(); const binary& operator =(const binary & otherlist); int getcount() const; }; void list_clear(nodetype*& head_ptr); void list_copy(const nodetype*source_ptr, nodetype*& head_ptr, nodetype*& tail_ptr); void reverselist(nodetype*& mylist); ostream & operator<<(ostream & outfile, const binary & outclass) { nodetype *current; current=outclass.head_ptr; while(current!=NULL) { outfile<info; current=current->link; } return outfile; } istream & operator>>(istream & infile, binary & inclass) { nodetype *newnode,*last; char ch; int intch; list_clear(inclass.head_ptr); inclass.head_ptr=NULL; last=NULL; infile>>ws; infile.get(ch); while( isdigit(ch)and infile) { intch = ch -'0'; newnode= new nodetype; newnode->info=intch; newnode->link=NULL; if (inclass.head_ptr==NULL) { inclass.head_ptr=newnode; } else { last->link=newnode; } last=newnode; infile.get(ch); } return infile; } binary operator+( binary num1, binary num2) { binary temp; int carry, sum; nodetype *current1, *current2, *newnode; reverselist(num1.head_ptr); reverselist(num2.head_ptr); cout << num1 << endl << num2 << endl; current1=num1.head_ptr; current2=num2.head_ptr; temp.head_ptr = NULL; carry = 0; while(current1!=NULL && current2!=NULL) { newnode=new nodetype; newnode->link = temp.head_ptr; temp.head_ptr=newnode ; sum = current1->info + current2->info + carry; temp.head_ptr->info = sum% 2; carry = sum/2; current1=current1->link; current2=current2->link; } while(current1!=NULL) { newnode=new nodetype; newnode->link = temp.head_ptr; temp.head_ptr=newnode ; sum = current1->info + carry; temp.head_ptr->info = sum%2; carry = sum/2; current1=current1->link; } while (current2!=NULL) { newnode=new nodetype; newnode->link = temp.head_ptr; temp.head_ptr=newnode ; sum = current2->info + carry; temp.head_ptr->info = sum%2; carry = sum/2; current2=current2->link; } if (carry) { newnode=new nodetype; newnode->link = temp.head_ptr; temp.head_ptr=newnode ; newnode->info=carry; } return temp; } binary::binary() { head_ptr=NULL; count=0; } binary::binary(const binary & inclass) { nodetype *tail_ptr; list_copy(inclass.head_ptr,head_ptr,tail_ptr); } binary::~binary() { list_clear(head_ptr); } const binary & binary::operator =(const binary & otherlist) { nodetype *tail_ptr; if(this != &otherlist) list_clear(head_ptr); list_copy(otherlist.head_ptr,head_ptr,tail_ptr); return otherlist; } int binary::getcount() const { return count; } void list_clear(nodetype*& head_ptr) { nodetype *removeptr; while(head_ptr!=NULL) { removeptr=head_ptr; head_ptr=head_ptr->link; delete removeptr; } } void list_copy(const nodetype*source_ptr, nodetype*& head_ptr, nodetype*& tail_ptr) { nodetype *temp; head_ptr=NULL; tail_ptr=NULL; if(source_ptr==NULL) return; head_ptr=new nodetype; head_ptr->link=NULL; head_ptr->info=source_ptr->info; tail_ptr=head_ptr; source_ptr=source_ptr->link; while(source_ptr !=NULL) { temp= new nodetype; temp->link=NULL; temp->info=source_ptr->info; tail_ptr->link=temp; tail_ptr=tail_ptr->link; source_ptr=source_ptr->link; } } void reverselist(nodetype*& mylist) { nodetype *templist, *tempnode; templist = NULL; while(mylist !=NULL) { tempnode = mylist; mylist = mylist->link; tempnode->link = templist; templist = tempnode; } mylist = templist; } int main(void) { binary a,b,c; cout << "Enter Bit stream 1:\n"; cin >> a; cout << '\n'; cout << "Enter Bit stream 2:\n"; cin >> b; cout << '\n'; cout << "You've entered " << a << " and " << b << endl; c = a + b; cout << "their sum is : " << c << endl; return 0; }``````
my main is

using namespace std;
#include "binary.cpp"
#include <iostream>
#include <fstream>
int main()
{

ifstream infile;
ofstream outfile;
binary binobj1;
binary binobj2;
infile.open("program2in.txt");
if(!infile)
{
cout << "Failure to open program2in.txt." << endl;
system ("pause");
return 1;
}
outfile.open("program2out.txt");
if(!infile)
{
cout << "Failure to open program2out.txt." << endl;
system ("pause");
return 1;
}

infile>>binobj1;
while(infile)
{
infile>>binobj2;
outfile<<binobj1<<endl;
outfile<<binobj2<<endl;
outfile<<binobj1+binobj2<<endl;

infile>>binobj1;
}

infile.close();outfile.close();
system ("pause");
return 0;
}
Maybe I am not clear.

11101
+ 1101

to get 01010 with a carry of 1 to display 101010.

I slightly modified the way you write files to make it more readable:
 ``1234567891011121314151617181920212223242526272829303132`` ``````int main() { ifstream infile; ofstream outfile; binary binobj1; binary binobj2; infile.open("program2in.txt"); if(!infile) { cout << "Failure to open program2in.txt." << endl; system ("pause"); return 1; } outfile.open("program2out.txt"); if(!infile) { cout << "Failure to open program2out.txt." << endl; system ("pause"); return 1; } while( (infile>>binobj1) && (infile>>binobj2) ) { outfile << binobj1 << " + " << binobj2 << " = " << binobj1 + binobj2 << endl; } infile.close();outfile.close(); system ("pause"); return 0; }``````

program2in.txt:
 ```10101010 01010101 11001100 11001100 1 10 01 10 1111 1 ```

program2out.txt:
 ```10101010 + 01010101 = 11111111 11001100 + 11001100 = 110011000 1 + 10 = 11 01 + 10 = 11 1111 + 1 = 10000 ```
For some reason, my outfile is blank.
You can detect outfile problems by replacing the line where I write in it with:
 ``123456`` ``````if( !(outfile << binobj1 << " + " << binobj2 << " = " << binobj1 + binobj2 << endl) ) { cout << "Problem writing program2out.txt." << endl; system ("pause"); return 1; }``````

If it's not a problem writing outfile, then I don't know.
Last edited on
Yes now the outfile displays + and = so it is not a problem with the outfile then?

I do not get a message saying that it is a problem with the out file so I guess it's not the outfile.

Here did I give you my .h file?

#include <iostream>
#include <cstdlib>
using namespace std;
struct nodetype
{
int info;
};
class binary
{
friend ostream & operator<<(ostream & outfile, const binary & outclass);
friend istream & operator>>(istream & infile, binary & inclass);
friend binary operator+( binary num1, binary num2);

public:
binary();
binary(const binary & inclass);
~binary();
const binary & operator =(const binary & otherlist);
int getcount() const;
private:
int count;
};
void list_copy(const nodetype*source_ptr, nodetype*& head_ptr, nodetype*& tail_ptr);

Last edited on
Pages: 12