conversion
tengo que hacer tres funsiones pero nose como se hacen. que consta de la siguiete manera. tengo que hacer 3 funsiones para que pase un numero decimal a binario, octal y hexadecimal. sin usar %x ni %o. chau espero que me ayuden. gracias
[a] : numero decimal a binario, sin usar n%2 ni n/2
// program k6_5.CPP - page 104
// bitwise operations : Bynary Form Print.
//written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
unsigned long GetNum()
{
unsigned long x;
cout << setw(15) << "num = ";
cin >> x;
return x;
} // GET NUM
void ShowA(unsigned long a)
{
cout << setw(12) << a << " = ";
for(int i = 31;i >= 0;i--)
{
cout << ((a & (1L << i)) != 0);
if(i % 4 == 0)
cout << " ";
}
cout << endl;
} // SHOW A
void ShowB(unsigned long a)
{
int first = 0;
cout << setw(12) << a << " = ";
for(int i = 31;i >= 0;i--)
{
if(!first)
{
if(((a & (1L << i)) != 0))
first = 1;
else
cout << " ";
}
if(first)
cout << ((a & (1L << i)) != 0);
if(i % 4 == 0)
cout << " ";
}
cout << endl;
} // SHOW B
void main()
{
clrscr();
unsigned long n;
cout << "bitwise operations : Bynary Form Print" << endl << endl;
while(n = GetNum())
{
ShowA(n);
ShowB(n);
cout << endl;
}
cout << endl << "end of program - good bye ! ! !" << endl;
getch();
} // MAIN
[b] : transformacion de decimal a hexadecimal
// program k6_6.CPP - page 104
// bitwise operations : HexaDecimal Form Print.
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
unsigned long GetNum()
{
unsigned long x;
cout << setw(15) << "num = ";
cin >> x;
return x;
} // GET NUM
void Show(unsigned long a)
{
const char *symb[] = {"0","1","2","3","4","5","6","7",
"8","9","a","b","c","d","e","f"};
cout << setw(12) << a << " = ";
for(int i = 0;i < 8;i++)
{
cout << symb[(a & 0xf0000000) >> 28];
a <<= 4;
}
cout << endl << endl;
} // SHOW
void main()
{
clrscr();
unsigned long n;
cout << "bitwise operations : HexaDecimal Form Print" << endl << endl;
while(n = GetNum())
Show(n);
cout << endl << "end of program - good bye ! ! !" << endl;
getch();
} // MAIN
[c] : la solucion de decimal a octal, la dejo para ti. Si tienes problemas, comunicate.
Chau, pibe ! ! !
// program k6_5.CPP - page 104
// bitwise operations : Bynary Form Print.
//written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
unsigned long GetNum()
{
unsigned long x;
cout << setw(15) << "num = ";
cin >> x;
return x;
} // GET NUM
void ShowA(unsigned long a)
{
cout << setw(12) << a << " = ";
for(int i = 31;i >= 0;i--)
{
cout << ((a & (1L << i)) != 0);
if(i % 4 == 0)
cout << " ";
}
cout << endl;
} // SHOW A
void ShowB(unsigned long a)
{
int first = 0;
cout << setw(12) << a << " = ";
for(int i = 31;i >= 0;i--)
{
if(!first)
{
if(((a & (1L << i)) != 0))
first = 1;
else
cout << " ";
}
if(first)
cout << ((a & (1L << i)) != 0);
if(i % 4 == 0)
cout << " ";
}
cout << endl;
} // SHOW B
void main()
{
clrscr();
unsigned long n;
cout << "bitwise operations : Bynary Form Print" << endl << endl;
while(n = GetNum())
{
ShowA(n);
ShowB(n);
cout << endl;
}
cout << endl << "end of program - good bye ! ! !" << endl;
getch();
} // MAIN
[b] : transformacion de decimal a hexadecimal
// program k6_6.CPP - page 104
// bitwise operations : HexaDecimal Form Print.
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
unsigned long GetNum()
{
unsigned long x;
cout << setw(15) << "num = ";
cin >> x;
return x;
} // GET NUM
void Show(unsigned long a)
{
const char *symb[] = {"0","1","2","3","4","5","6","7",
"8","9","a","b","c","d","e","f"};
cout << setw(12) << a << " = ";
for(int i = 0;i < 8;i++)
{
cout << symb[(a & 0xf0000000) >> 28];
a <<= 4;
}
cout << endl << endl;
} // SHOW
void main()
{
clrscr();
unsigned long n;
cout << "bitwise operations : HexaDecimal Form Print" << endl << endl;
while(n = GetNum())
Show(n);
cout << endl << "end of program - good bye ! ! !" << endl;
getch();
} // MAIN
[c] : la solucion de decimal a octal, la dejo para ti. Si tienes problemas, comunicate.
Chau, pibe ! ! !
