To use linked lists in a class. To implement binary numbers using a class. Creat
ID: 3530506 • Letter: T
Question
To use linked lists in a class. To implement binary numbers using a class. Create a linked list to store binary numbers one bit at a time. Use unordered list in Chapter 18. If the input number is 11010, it should be stored with each bit in a separate node. As you read the number (one character at a time), store each digit at the tail of a linked list. You will need to translate the character into a number. Overload the >> operator as a friend function to read in the number as follows: input" number; // Store the number in a linked list. Overload the 1 carry 0 0 + 1 + 1 -> 0 carry 1 1 + 1 + 1 -> 1 carry 1 1 + 1 + (0) -> 0 carry 1 1 + (0) + (0) -> 1 carry 0 (The final carry will need an extra node at the end of the list.) You can simplify the class from the book by not using a template class. You can use a typedef for the data type as follows: typedef int datatype Read and write to disk. The main program will need an End-of-File loop. The input will consist of two numbers on a single line, with a space in between. The output will be the sum for each set of two numbers. Code the sum and carry functions as eloquently as possible, that is, use low-level or high-level logic rather than if else structures. Consider creating a doubly-linked list. TO HAND IN: Hard copy of the C ++ code including the specification and Implementation files Hard copy of the output A thumb drive Data for Program ^ indicates a space 101101^1001 1^11111 1011^1001110 /* Class for binary numbers program Programmer Sterling */ #include #include using namespace std; // struct for linked list struct nodeType { int info; nodeType * link; }: class binary { friend ostream & operatorExplanation / Answer
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
#include "binary.h"
ostream & operator<<(ostream & outfile, const binary & outclass)
{
nodetype *current;
current=outclass.head_ptr;
while(current!=NULL)
{
outfile<<current->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);
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
{
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.