Write a program that allows numbers to be entered in decimal (base 10), hexadeci
ID: 3642763 • Letter: W
Question
Write a program that allows numbers to be entered in decimal (base 10), hexadecimal (base 16), or binary (base 2), and then prints out the number again in all three bases.
To distinguish between different bases we'll say that hexadecimal numbers should always be preceded by a "$" and binary numbers by a "%". Other numbers are assumed to be decimal.
When the program is run it should repeatedly allow a number to be entered and then print it out in all three bases until "q" is typed in. Here's an example of how your program should operate:
Enter #: 45
45 $2D %101101
Enter #: %101
5 $5 %101
Enter #: $31
49 $31 %110001
Enter #: q
Approach
A good way to structure this program is to read in each "number" as a string of characters, look at the first character to determine what kind of number it is (hex, binary, decimal), and then call an appropriate function to convert the string into an actual integer number. Have another set of functions that can take an integer and return a hex, binary, or decimal number, and use those to print the number out in each of the bases.
You'll need to write these three functions to convert strings into integers:
int dectoint( string st ); // e.g. "14" ? 14
int hextoint( string st ); // e.g. "$14" ? 20
int bintoint( string st ); // e.g. "%10" ? 2
and these three functions to convert integers into strings:
string inttodec( int n ); // e.g. 14 ? "14"
string inttohex( int n ); // e.g. 20 ? "$14"
string inttobin( int n ); // e.g. 2 ? "%10"
Some tips:
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
//PROTOTYPES
int dectoint(string);
int hextoint(string);
int bintoint(string);
string inttodec(int);
string inttohex(int);
string inttobin(int);
int main()
{
//LOCAL DECLARATIONS
string st = " ";
int n = 0;
while (st[0] != 'q')
{
// get number
cout << "Enter #: ";
getline(cin, st);
while (st.empty()) // make sure st is not empty
{
cout << "Enter #: ";
getline(cin, st);
}
// process number
if (st[0] != 'q')
{
if (st[0] == '$')
n = hextoint(st);
else if (st[0] == '%')
n = bintoint(st);
else
n = dectoint(st);
cout << inttodec(n) << " "
<< "$" << inttohex(n) << " "
<< "%" << inttobin(n) << endl;
}
cout << endl;
}
return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
int dectoint(string st)
{
int result = 0;
for (int i = 0; i < st.length(); i++)
{
result *= 10;
result += st[i] - '0';
}
return result;
}
//---------------------------------------------------------
int hextoint(string st)
{
int result = 0;
for (int i = 1; i < st.length(); i++)
{
result *= 16;
if (st[i] <= '9')
result += st[i] - '0';
else
result += st[i] % 32 + 9;
}
return result;
}
//---------------------------------------------------------
int bintoint(string st)
{
int result = 0;
for (int i = 1; i < st.length(); i++)
{
result *= 2;
result += st[i] - '0';
}
return result;
}
//---------------------------------------------------------
string inttodec(int n)
{
string result = "";
while (n > 0)
{
result.insert(result.begin(), '0' + n % 10);
n /= 10;
}
return result;
}
//---------------------------------------------------------
string inttohex(int n)
{
string result = "";
while (n > 0)
{
int chr2ins = n % 16;
if (chr2ins < 10)
result.insert(result.begin(), '0' + chr2ins);
else
result.insert(result.begin(), 'A' + chr2ins - 10);
n /= 16;
}
return result;
}
//---------------------------------------------------------
string inttobin(int n)
{
string result = "";
while (n > 0)
{
result.insert(result.begin(), '0' + n % 2);
n /= 2;
}
return result;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.