Bài số nhị phân của bạn viết rối quá, mình debug cho bạn 1 hồi thành ra viết lại từ đầu luôn. Góp ý cho bạn về cách đặt tên biến, chịu khó đặt nó dài thêm 1 tí và có ý nghĩa thì sau này có lấy ra sử dụng lại cũng dễ đọc hơn.
Binary.h
PHP Code:
#include <iostream>
#include <cctype> // for isdigit()
#include <iomanip>
using namespace std;
class Binary{
private :
int *integer; /*Array of Binary number*/
int length; /*and Its length*/
public:
/*Default constructor*/
Binary():length(5){
integer = new int[length];
for(int x = 0; x < length; x++){
integer[x] = 0;
}
}
Binary(int value);//convert a value into Binary number(e.g Binary Ob(1313); )
Binary(const char *ss);//convert a string value (e.g Binary Ob("30131"); )
Binary(const Binary &original);//copy constructor
~Binary();//destructor
/*overloading operator '=' */
Binary &operator = (const Binary ©);
friend istream& operator >> (istream &is, Binary &ob);
friend ostream& operator << (ostream &os, Binary &ob);
void convert_to_decimal();
void convert_to_binary();
Binary operator + (Binary& ob);
int take_power(int degree, int num);
int take_decimal_length(int number);
int take_binary_length(int number);
};
Binary.cpp
PHP Code:
/*Conversion Constructor I*/
Binary::Binary(const char *ss)
{
length = strlen(ss);
integer = new int[length];
for(int x = 0, y = 0; x < length; x++, y++){
if(isdigit(ss[y])){
integer[x] = ss[y] - '0';
}
}
}
/*Conversion Constructor II*/
Binary::Binary(int value)
{
length = take_decimal_length(value);
integer = new int[length];
for(int x = length; value != 0 && x >= 0; x--){
integer[x] = value % 10;
}
}
/*Destructor*/
Binary::~Binary()
{
delete[] integer;
}
/*Functional function*/
void Binary::convert_to_binary()
{
int real = 0;
for(int x = length-1; x >= 0; x--){
real = real + integer[length-1-x]*take_power(x, 10);
}
length = take_binary_length(real);
delete[] integer;
integer = new int[length];
for(int x = 0; x < length && real != 0; x++){
integer[length-1-x] = real % 2;
real = real / 2;
}
}
void Binary::convert_to_decimal()
{
int decimal = 0;
for(int x = length-1; x >= 0; x--){
decimal = decimal + integer[(length-1)-x]*take_power(x, 2);
}
length = take_decimal_length(decimal);
delete[] integer;
integer = new int[length];
for(int x = 0; x < length && decimal != 0; x++){
integer[length-1-x] = decimal % 10;
decimal = decimal / 10;
}
}
/*END*/
Binary &Binary::operator = (const Binary &original)
{
if(&original != this){
if(length != original.length){
length = original.length;
delete []integer;
integer = new int[original.length];
}
for(int x = 0; x < length; x++){
integer[x] = original.integer[x];
}
}
return *this;//enable cascading
}
Binary::Binary(const Binary ©):length(copy.length)
{
integer = new int[length];
for(int x = 0; x < length; x++)
integer[x] = copy.integer[x];
}
Binary Binary::operator + (Binary &ob)
{
ob.convert_to_decimal();
convert_to_decimal();
/*----------------Take decimal number of 2 side--------------------*/
int left = 0;
for(int x = length-1; x >= 0; x--){
left = left + integer[length-1-x]*take_power(x, 10);
}
int right = 0;
for(int x = ob.length-1; x >= 0; x--){
right = right + ob.integer[ob.length-1-x]*take_power(x, 10);
}
/*-----------------End []------------------------------------------*/
int sum = right + left;
Binary temp;
temp.length = take_decimal_length(right+left);
for(int x = 0; x < temp.length && sum != 0; x++){
temp.integer[temp.length-1-x] = sum % 10;
sum = sum / 10;
}
temp.convert_to_binary();
return temp;//return copy of temp object
}
int Binary::take_power(int degree, int num)
{
int result = 1;
for(int x = 1; x <= degree; x++){
result *= num;
}
return result;
}
int Binary::take_decimal_length(int number)
{
int count = 0;
do{
number = number / 10;
count++;
}while(number != 0);
return count;
}
int Binary::take_binary_length(int number)
{
int count = 0;
do{
number = number / 2;
count++;
}while(number != 0);
return count;
}
istream& operator >> (istream& input, Binary& ob)
{
cout << "Enter length of Binary number : ";
input >> ob.length;
ob.integer = new int[ob.length];
for(int x = 0; x < ob.length; x++){
cout << "Element (0,1) [" << x << "] = ";
input >> ob.integer[x];
cout << '\n';
}
return input;
}
ostream& operator << (ostream& output, Binary& ob)
{
output << '\n';
for(int x = 0; x < ob.length; x++){
output << ob.integer[x] << setw(2);
}
return output;
}
Main.cpp
PHP Code:
int main()
{
Binary A; cin >> A;
Binary B; cin >> B;
Binary C;
C = A + B;
cout << C;
system("pause");
return 0;
}
Phần quá tải toán tử thực ra nó dễ, chỉ có cái syntax là màu mè hù doạ người ta thôi. Bạn chịu khó đọc kĩ là ok hết. Mình có viết lại hàm take_power thay cho hàm "pow" của thư viện #include <cmath>, không hiểu sao máy mình nó điên sao á, không dùng được nên mình viết lại luôn.