1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#include <iostream>
#include <fstream>
#include <cctype>
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<<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);
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;
}
|