class work help

We did this program in class and I couldnt get the progrma to print the binary numbers added together.

The point of this program is to add binarys. But their cant be any 2 or 3 so you have to change that when you add but i keep getting the error that it wont write. Can anyone help?

#include<vector>
#include<iostream>

using namespace std;

class Binary
{
private:
vector<int>digits;
public:
void input();
void display();
void operator+(Binary);
};
void Binary::input()
{
int x;
cout<<"Please type in your binary digt: ";
cin>>x;
digits.push_back(x);

}
void Binary::operator +(Binary t)
{
int a;
int b;
int u;
int x=digits.size();
int y=t.digits.size();
vector<int>c;
vector<int>d;
vector<int>z;
if(y>x)
{
a=y;
b=x;
u=a-b-1;
for(int i=0; i<a; i++)
{
c[i]=digits[i];
d[i]=t.digits[i];
}
}
if(x>y)
{
a=x;
b=y;
u=a-b-1;
for(int i=0; i<a; i++)
{
c[i]=t.digits[i];
d[i]=digits[i];
}
}
c.resize(a);
int r=b-1;
for(int i=a-1; i>0; i--)
{
if(i<=u)
{
c[i]=0;
}
else
{
c[i]=c[r];
r--;
}
}
if(x==y)
{
a=x;
b=y;
}
z.resize(a);
for(int i=a-1; i>0; i--)
{
z[i]=digits[i]+ t.digits[i];
if(z[i]==2)
{
z[i]=0;
z[i-1]+1;
}
if(z[i]==3)
{
z[i]=1;
z[i-1]+1;
}

}
if(z[0]==3)
{
z[0]=11;
}
else if(z[0]==2)
{
z[0]=10;
}
cout<<"Sum: ";
for(int i=0; i<z.size(); i++)
{
cout<<z[i];
}
}

void Binary::display()
{
cout<<"Binary: ";
for(int i=0; i<digits.size(); i++)
{
cout<<digits[i];
}
cout<<endl;

}
int main()
{
Binary t, tt;
vector<int>digits;
cout<<"Hello welcome to AddBinary."<<endl;
cout<<endl;
cout<<"(1) Add a digit to binary 1."<<endl;
cout<<"(2) Add a digit to binary 2."<<endl;
cout<<"(3) Exit."<<endl;
cout<<"Would you like to:";
int x;
while (x!=4)
{
cin>>x;
if(x==1)
{
t.input();
}
if(x==2)
{
tt.input();
}
if(x==3)
{
t.display();
tt.display();
t+tt;
}
if(x==4)
{
cout<<"Did I get an A?"<<endl;
}
}

}
Last edited on
Would you explain this part of your code:
1
2
3
4
5
{
  t.display();
  tt.display();
  t+tt;
}
Last edited on
Once I get the two binaries from the user I display both of them and then I add. But when I try to add the binaries and display their seems to be a problem with my + operation.
That piece of code does not attempt to display the result of the addition.

What problem? What error?


Would you write:
1
2
3
{
  2+4;
}

That t+tt may or may not do the correct thing, but it does not behave like integers do. Unintuitive.

The program builds fine but I wont add the binaries so the problem has to be in my + operation. If you could just look over that and make sure that I'm not missing any steps that would be great.
1
2
3
4
> if(z[0]==3)
> {
>     z[0]=11; // *** a binary digit must be either 0 or 1 (it can't be 10 or 11)
> }


This is one way of doing the addition. The code would be simpler with a std::deque<> (reverse not required)
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
#include <iostream>
#include <vector>
#include <algorithm>

struct binary
{
    void display() const { for( int d : digits ) std::cout << d ; std::cout << '\n' ; }
    binary operator+ ( const binary& that ) const ;

    /*private:*/ std::vector<int> digits ;
};

binary binary::operator+ ( const binary& that ) const
{
   auto n = std::max( digits.size(), that.digits.size() ) ; // larger size
   n += 1 ; // may overflow, so one more

   std::vector<int> first( digits.rbegin(), digits.rend() ) ;
   // first now contains the first set of digits in reverse order
   first.resize(n) ; // make its size equal to the larger size (add zeroes at the end)

   std::vector<int> second( that.digits.rbegin(), that.digits.rend() ) ;
   // second now contains the second set of digits in reverse order
   second.resize(n) ; // make its size equal to the larger size (add zeroes at the end)

   binary result ;
   result.digits.resize(n) ;

   // simulate hand-addition with carry
   // but left to right because digits are reversed
   int carry = 0 ;
   for( std::size_t i = 0 ; i < first.size() ; ++i ) // for each digit
   {
       int& d = result.digits[i] ;
       d = first[i] + second[i] + carry ; // add the two digits, carry
       // a digit must be either 0 or 1; so
       if( d > 1 ) { carry = d / 2 ; d %= 2 ; } // adjust for overflow
       else carry = 0 ;
   }

   // if the most significant (in reverse order) digit is zero, remove it
   if( result.digits.back() == 0 ) result.digits.pop_back() ;
   // reverse the digits of the result
   std::reverse( result.digits.begin(), result.digits.end() ) ;

   return result ;
}

int main()
{
    const binary a { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } } ;
    a.display() ;

    const binary b { { 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } } ;
    b.display() ;

    const binary c = a + b ;
    c.display() ;
}

http://ideone.com/KIEHCw

Now, write your own code for operator+()
Last edited on
I don't really understand some of this code could you just explain where some of my code went wrong. please
> could you just explain where some of my code went wrong. please

I've already explained it. Right at the beginning of my earlier post.

Topic archived. No new replies allowed.