Need Imediate help please

#include<iostream>
#include<conio>
#include<cstring>

class decimal {
private:
int dec;
public:
decimal();
decimal(int);
void input();
decimal operator+(decimal) const;
decimal operator*(decimal) const;
decimal operator++();
decimal operator++(int);
decimal operator--() ;
decimal operator--(int) ;
};

/*
Decimal Class Function Definitions
*/

/* Constructors : */

decimal :: decimal() : dec(0)
{}

decimal :: decimal(int a1) : dec(a1)
{}

void decimal :: input()
{
cout<<"Enter A decimal Number : ";
cin>>dec;
}

/* Operator Overloading Functions : */

decimal decimal :: operator+(decimal num) const
{
return decimal(dec + num.dec);
}

decimal decimal :: operator*(decimal num) const
{
return decimal(dec * num.dec);
}

decimal decimal :: operator++()
//prefix increment
{
return decimal(++dec);
}

decimal decimal :: operator++(int)
//postfix increment
{
return decimal(dec++);
}

decimal decimal :: operator--()
//prefix decrement
{
return decimal(--dec);
}

decimal decimal :: operator--(int)
//postfix decrement
{
return decimal(dec--);
}

/*
Class Binary :
*/
enum {size=64};
class binary {
private :
int len;
char bin[size];
public:
binary() { strcpy(bin,""); }
binary(char b[],int leng=64){ strcpy(bin,b); len = leng; }

void input();
void print(){

for(int i=0; i<len; i++){
if(bin[i] == '0' || bin[i]=='1')
cout<<bin[i];
else
continue;}
}

friend binary operator+(binary, binary);
binary operator-(binary);
friend binary operator*(binary, binary);
};

void binary :: input()
{

cout<<"Enter a Binary Number : ";
cin.getline(bin,size);


}

void shift_given_right(char a[],char b[],char a1[],char b1[])
{
for(int i=size-1, j=strlen(a)-1; i>=size-strlen(a), j>=0; i--,j--)
a1[i] = a[j];
for(int i=size-1-strlen(a); i>=0; i--)
a1[i] = '0';
for(int i=size-1, j=strlen(b)-1; i>=size-strlen(b), j>=0; i--,j--)
b1[i] = b[j];
for(int i=size-1-strlen(b); i>=0; i--)
b1[i] = '0';
}

void shiftSumLeft(char sum[],char sum1[],int length)
{

if(sum1[size-1-length] == '0')
for(int i=0, j=size-1-length+1; i<=length-1,j<=size-1; i++,j++)
sum[i] = sum1[j];
if(sum1[size-1-length] == '1')
for(int i=0, j=size-1-length; i<=length,j<=size-1; i++,j++)
sum[i] = sum1[j];
}

binary operator + (binary n1, binary n2)
{

char carry[size];
char a1[size], b1[size], sum[size], sum1[size];
int length;

if( strlen(n1.bin) > strlen(n2.bin) )
length = strlen(n1.bin);
else
length = strlen(n2.bin);

for(int i=size-1; i>=0; i--)
carry[i] = '0';

shift_given_right(n1.bin,n2.bin,a1,b1);

for(int i=size-1; i >= (size-1-length) ; i--)
{
if( a1[i] == '0' && b1[i] == '0' && carry[i] == '0' )
{
sum1[i] = '0';
carry[i-1] = '0';
}
if( a1[i] == '0' && b1[i] == '0' && carry[i] == '1' )
{
sum1[i] = '1';
carry[i-1] = '0';
}
if( a1[i] == '0' && b1[i] == '1' && carry[i] == '0' )
{
sum1[i] = '1';
carry[i-1] = '0';
}
if( a1[i] == '0' && b1[i] == '1' && carry[i] == '1' )
{
sum1[i] = '0';
carry[i-1] = '1';
}
if( a1[i] == '1' && b1[i] == '0' && carry[i] == '0' )
{
sum1[i] = '1';
carry[i-1] = '0';
}
if( a1[i] == '1' && b1[i] == '0' && carry[i] == '1' )
{
sum1[i] = '0';
carry[i-1] = '1';
}
if( a1[i] == '1' && b1[i] == '1' && carry[i] == '0' )
{
sum1[i] = '0';
carry[i-1] = '1';
}
if( a1[i] == '1' && b1[i] == '1' && carry[i] == '1' )
{
sum1[i] = '1';
carry[i-1] = '1';
}

}
shiftSumLeft(sum,sum1,length);
int len = strlen(sum);
return binary(sum,len);
}

void complement(char bin1[])
{

int count=0;

for(int i=size-1; i>=0; i--)
{
if(count>0)
{
if(bin1[i]=='1')
bin1[i]='0';
else
bin1[i]='1';
}
if(bin1[i]=='1')
count++;
}
}

binary binary :: operator-(binary b)
{
binary b1;
complement(b.bin);

b1 = bin + b;

int leng = strlen(b1.bin);
if(b1.bin[0] == '1')
b1.bin[0] = ' ';
return binary(b1.bin,leng);
}

binary shiftb1left(const char oldbin[],int len,int zero){
cout<<"zero : "<<zero<<endl;
char newbin[size]={'0'};
char ready[size]={'0'};
cout<<"OLD : ";
for(int i=0; i<size; i++)
cout<<oldbin[i];
cout<<endl;
for(int i=strlen(oldbin)-1,j=size-1-zero; i>=0, j>=size-strlen(oldbin)-zero; i--,j--)
newbin[j] = oldbin[i];

for(int i=size-1-zero; i<size; i++)
newbin[i]='0';
cout<<"NEW : ";
for(int i=0; i<size; i++)
cout<<newbin[i];
cout<<endl;
for(int i=size-zero-strlen(oldbin),j=0; i<size, j<strlen(oldbin)+zero; i++,j++)
ready[j] = newbin[i];
cout<<"READY : "<<ready<<endl;
return binary(ready);
}

char multi(const char a, const char b)
{
char result = '0';
if ( a == '0' && b == '0' )
result = '0';
else if( a == '0' && b == '1' )
result = '0';
else if( a == '1' && b == '0' )
result = '0';
else if( a == '1' && b == '1' )
result = '1';
return result;
}

binary operator*(binary a, binary b)
{
int count =0;
char mul[size];
binary b1("0"),b2("0");
int len = strlen(a.bin);
int len1 = strlen(b.bin);
for(int i=len1-1; i>=0; i--){
for(int j=len-1; j>=0; j--)
{
char res = multi(a.bin[j], b.bin[i]);
b1.bin[j] = res;
}
cout<<"B1: "<<b1.bin<<endl;
if(i==len1-1)
b2 = b1;
else{
count++;
int leng=strlen(b1.bin);
cout<<leng<<endl;
binary b3 = shiftb1left(b1.bin,leng,count);
b2 = b2 + b3;
}
}

return binary(b2.bin);
}




int main()
{
binary b1,b2,b3;
b1.input();
b2.input();
b3 = b1 + b2;
b3.print();
cout<<endl<<endl;
b3 = b1 - b2;
b3.print();
cout<<endl<<endl;
b3 = b1*b2;
b3.print();
getch();
}

the problem is that binary multiplication is not working properly.
when i input 101 and 101. it gives answer 0111 ... while the answer should be 11001.. and same goes for other inputs
1. Use code wrap
2. Indent

That code is very illegible the way it is typed right now.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
     cout << "This code is much more organized with indents and code wrap";
     cout << "\nPlease write your code like this";
}
Topic archived. No new replies allowed.