como diseñar un programa que transforme numeros decimales a binarios y al contrario
COMO HARIA LA ESTRUCTURA DE UN PROGRAMA EN C++ QUE CONVIERTA UN NUMERO DECIMAL EN UN NUMERO BINARIO Y AL CONTRARIO, UN NUMERO BINARIO A UN NUMERO BINARIO, USANDO UN MENU DE OPCIONES.
ES URGENTE.
GRACIAS.
ES URGENTE.
GRACIAS.
// program bases.cpp
// convcersion de unb numero decimal a binario, y binario a dcimal
// usando menu de opciones
#include <iomanip.h>
#include <iomanip.h>
#include <conio.h>
char GetChoice(char *msg)
{
cout << msg;
char choice = getch();
cout << endl;
return choice;
} // GET CHOICE
long GetNum(char *msg)
{
long num;
cout << "get a " << msg << " number : ";
cin >> num;
if(num < 0)
num = -num;
return num;
} // GET NUM
char *DecimalToBinary(long num)
{
static char str[120];
static int i = 0;
if(!num)
{
i = 0;
return NULL;
}
DecimalToBinary(num/2);
str[i] = '0' + num%2;
str[++i] = 0;
return str;
} // DECIMAL TO BINARY
int BinaryToDecimal(long num)
{
int result = 0,exp = 1;
while(num)
{
int x = num % 10;
if(x > 1)
return -1;
result = result + x*exp;
exp *= 2;
num /= 10;
}
return result;
} // BINARY TO DECIMAL
void main()
{
for(;GetChoice("another one ? - ESC for exit --->")!= 27;)
{
long num_in;
char *num_out;
int binum;
switch(GetChoice("d for decimal, b for binary : "))
{
case 'd' : cout << "decimal to binary : " << endl << endl;
num_in = GetNum("decimal");
num_out = DecimalToBinary(num_in);
cout << "binary number is : " << num_out << endl;
break;
case 'b' : cout << "binary to decimal : " << endl << endl;
num_in = GetNum("binary");
binum = BinaryToDecimal(num_in);
if(binum < 0)
cout << "illegal input" << endl;
else
cout << "decimal number is : " << binum << endl;
break;
} // SWITCH
} // FOR
cout << endl << endl << "end of program - good bye ! ! !" << endl;
} // MAIN
// convcersion de unb numero decimal a binario, y binario a dcimal
// usando menu de opciones
#include <iomanip.h>
#include <iomanip.h>
#include <conio.h>
char GetChoice(char *msg)
{
cout << msg;
char choice = getch();
cout << endl;
return choice;
} // GET CHOICE
long GetNum(char *msg)
{
long num;
cout << "get a " << msg << " number : ";
cin >> num;
if(num < 0)
num = -num;
return num;
} // GET NUM
char *DecimalToBinary(long num)
{
static char str[120];
static int i = 0;
if(!num)
{
i = 0;
return NULL;
}
DecimalToBinary(num/2);
str[i] = '0' + num%2;
str[++i] = 0;
return str;
} // DECIMAL TO BINARY
int BinaryToDecimal(long num)
{
int result = 0,exp = 1;
while(num)
{
int x = num % 10;
if(x > 1)
return -1;
result = result + x*exp;
exp *= 2;
num /= 10;
}
return result;
} // BINARY TO DECIMAL
void main()
{
for(;GetChoice("another one ? - ESC for exit --->")!= 27;)
{
long num_in;
char *num_out;
int binum;
switch(GetChoice("d for decimal, b for binary : "))
{
case 'd' : cout << "decimal to binary : " << endl << endl;
num_in = GetNum("decimal");
num_out = DecimalToBinary(num_in);
cout << "binary number is : " << num_out << endl;
break;
case 'b' : cout << "binary to decimal : " << endl << endl;
num_in = GetNum("binary");
binum = BinaryToDecimal(num_in);
if(binum < 0)
cout << "illegal input" << endl;
else
cout << "decimal number is : " << binum << endl;
break;
} // SWITCH
} // FOR
cout << endl << endl << "end of program - good bye ! ! !" << endl;
} // MAIN
