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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
using namespace std;
class decimalHexOctalBinary
{
public:
int decimalUnsignedInt(int unconverted, int baseNumber)
{
int firstDigit = 0;
int secondDigit = 0;
int thirdDigit = 0;
int fourthDigit = 0;
int converted = 0;
while(unconverted > 0)
{
fourthDigit++;
unconverted--;
if (fourthDigit >= baseNumber)
{
for(int j = 0; j < unconverted-1; j++)
{
thirdDigit++;
if(thirdDigit > baseNumber)
{
for(int k =0; j < baseNumber; k++)
{
secondDigit++;
if (secondDigit > baseNumber)
{
for(int l=0; l<baseNumber; l++)
{
firstDigit++;
break;
}
}
break;
}//for secondDigit++
}//thirdDigit
break;
}//fourthDigit
}//if
break;
}//while
cout << "The number" << unconverted << " converted to" << baseNumber << " base, is";
displayConversion(firstDigit, secondDigit, thirdDigit, fourthDigit);
}
void displayConversion(int firstDigit, int secondDigit, int thirdDigit, int fourthDigit)
{
if (firstDigit == 0)
{
if(secondDigit==0)
{
if(thirdDigit == 0)
{
cout << fourthDigit << endl;
break;
}
cout << thirdDigit << fourthDigit << endl;
break;
}
cout << secondDigit << thirdDigit << fourthDigit << endl;
}
else
cout << firstDigit << secondDigit << thirdDigit << fourthDigit;
}// displayConverstion
int hexadecimalUnsignedInt(int unconverted, int baseNumber)
{
int newBaseNumber = baseNumber+6;
decimalUnsignedInt(unconverted, newBaseNumber);
return 0;
}
int octalUnsignedInt(int unconverted, int baseNumber)
{
int newBaseNumber = baseNumber-2;
decimalUnsignedInt(unconverted, newBaseNumber);
return 0;
}
int binaryUnsignedInt(int unconverted, int baseNumber)
{
int newBaseNumber = baseNumber-8;
decimalUnsignedInt(unconverted, newBaseNumber);
return 0;
}
}; // end class
int main()
{
int unconverted;
int baseNumber = 10;
char answer = 'y';
cout << "The original base number is: Base " << baseNumber << endl;
cout << "The bases this program will cover are: " << baseNumber-8 << ", "
<< baseNumber-2 << ", " << baseNumber << ", and " << baseNumber+6 << "." << endl;
while(answer == 'y')
{
cout << "Would you like change the base? y/n" << endl;
cin >> answer;
cout << "What base for the original number base?" << endl;
cin >> baseNumber;
if(baseNumber < 0)
cout << "That number is invalid, please only input positive integers." << endl;
}
cout << "Please enter a value(in base 10) to convert to other bases." << endl;
cin >> unconverted;
decimalHexOctalBinary::decimalUnsignedInt(unconverted, baseNumber);
decimalHexOctalBinary::hexadecimalUnsignedInt(unconverted, baseNumber);
decimalHexOctalBinary::octalUnsignedInt(unconverted, baseNumber);
decimalHexOctalBinary::binaryUnsignedInt(unconverted, baseNumber);
return 0;
}
|