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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
// Numbers conversion. 1 to 32767 into binary, octal and hexa codes.
//Copied from Professor Richmond, edited by me.
//Milton Neto. 9/12/14. CIT 120.
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main()
{
do
{
const int DIVISOR = 2;
const int Divisor = 8;
const int SUB = 20;
int binary[SUB] = {0}, dividend = 0, quotient = -1, remainder = 0, x = 0;
int octal[SUB] = {0}, Dividend = 0, Quotient = -1, Remainder = 0, X = 0;
system ("cls");
cout << "Please enter a number between 1 and 32767: ";
cin >> dividend, Dividend;
while (dividend, Dividend <=0 || dividend, Dividend > 32767)
{
//Loop does not end until user enters correct number
cout << "Invalid entry; please reenter:";
cin >> dividend, Dividend;
}
while (quotient !=0)
{
//When quotient is 0 you have extracted the binary number
quotient = dividend / DIVISOR;
remainder = dividend % DIVISOR;
binary[x++] = remainder;
dividend = quotient;
}
while (Quotient !=0)
{
//When quotient is 0 you have extracted the octal number
Quotient = Dividend / Divisor;
Remainder = Dividend % Divisor;
octal[X++] = Remainder;
Dividend = Quotient;
}
cout << "The binary value is: ";
for (int y = x -1; y >= 0; y--) //Prints array backwards
cout << binary[y] << endl;
cout << "The octal value is: ";
for (int w = X -1; w >= 0; w--)
cout << octal[w] << endl;
cout << "Go again (Y/N)?";
}while (toupper(_getch()) != 'N'); //Get response without hitting ENTER key
cout << "\n";
return 0;
}
|