Suma y resta binaria en c++
Necesito hacer un programa en c ++ que me de dos opciones la suma y la resta de numeros binarios..
si alguien me puede ayudar, se lo agradeceria mucho!!
si alguien me puede ayudar, se lo agradeceria mucho!!
//bin es una lista de arreglos con el digito binario en cada pos con un metodo longitud y otro obtener
es para que cojas la idesa que este forma de programacion no se adapta a tu problema
int Binario::ConverToDecimal()
{
int L = bin.LONGITUD(),x = 0,c;
c = L - 1;
for(int j = 1;j < L + 1;j++)
x += bin.OBTENER(j) * pow(2,c--);
return x;
}
String Binario::CtoBinario(int Dec)
{
String nb;
while(Dec != 0)
{
nb = ((String)Dec % 2) + nb;
Dec /= 2;
}
return nb;
}
Binario Binario::operator+(const Binario &nCBin)
{
int L1 = this->ConverToDecimal(),L2 = nCBin.ConverToDecimal(),Sum = 0;
Sum = L1 + L2;
String resp = CtoBinario(Sum);
return Binario(resp);
}
//*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*--*--*
Binario Binario::operator-(const Binario &nCBin)
{
int L1 = this->ConverToDecimal(),L2 = nCBin.ConverToDecimal(),Sum = 0;
if(L1 > L2)return nCBin.operator -((*this));
Sum = L1 - L2;
String resp = CtoBinario(Sum);
return Binario(resp);
}
es para que cojas la idesa que este forma de programacion no se adapta a tu problema
int Binario::ConverToDecimal()
{
int L = bin.LONGITUD(),x = 0,c;
c = L - 1;
for(int j = 1;j < L + 1;j++)
x += bin.OBTENER(j) * pow(2,c--);
return x;
}
String Binario::CtoBinario(int Dec)
{
String nb;
while(Dec != 0)
{
nb = ((String)Dec % 2) + nb;
Dec /= 2;
}
return nb;
}
Binario Binario::operator+(const Binario &nCBin)
{
int L1 = this->ConverToDecimal(),L2 = nCBin.ConverToDecimal(),Sum = 0;
Sum = L1 + L2;
String resp = CtoBinario(Sum);
return Binario(resp);
}
//*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*--*--*
Binario Binario::operator-(const Binario &nCBin)
{
int L1 = this->ConverToDecimal(),L2 = nCBin.ConverToDecimal(),Sum = 0;
if(L1 > L2)return nCBin.operator -((*this));
Sum = L1 - L2;
String resp = CtoBinario(Sum);
return Binario(resp);
}
