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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/**
@file numbersystem.cpp
@author Margaret Fink
@date 2011- 04-14
Description: Menu-driven application that performs conversions among
numbers of different bases.
Course: CS1253 Section: 2
Logon ID: cs125377
Programming Project #: 5
Instructor: Duncan
*/
# include <iostream>
# include <cmath>
using namespace std;
/*
* This function display the menu shown in this
* project description
*/
void menu()
{
cout<<" NUMBER SYSTEM APPLICATION "<<endl;
cout<<"===================================="<<endl;
cout<<"[1] Base-10 -> Nonbase-10"<<endl;
cout<<"[2] Nonbase-10 -> Base-10"<<endl;
cout<<"[3] Nonbase-10 -> Nonbase-10"<<endl;
cout<<"[0] Exit"<<endl;
cout<<endl;
}
/*
* This function returns the value of the digit
* of a non base 10 number.
* @param digit a character 0-9 or A-Z
* @ return 1 for the character ’1’, 2 for the
* character ’2’, ..., 10 for the character ’A’,
* 11 for the character ’B’, 12 for the character
* ’C’, etc.
*/
char valueToDigit(int value)
{
if(0 <= value <= 9)
value = value + 48;
else
value = value + 55;
return value;
}
/*
* This function converts a base-10 number to
* a string equivalent to a non-base 10 number.
* @param base10Num a non-negative integer
* @param base a number greater than 1 representing
* the base to which the base10Num is to be converted.
* @return a string representing the base b
* equivalent of the base 10 number.
*/
string fromBase10(long base10Num, long base)
{
string ans = " ";
int remain;
int Q = base10Num;
while(Q != 0)
{
remain = (Q % base);
Q = Q/base;
ans = valueToDigit(remain);
}
return ans;
}
/*
* This function converts a non base 10 number to
* a base 10 number.
* @param nonBase10Num a string representation of
* a non-base 10 number; nonBase10Num
* is assumed to be a valid number in the indicated base.
* @param base a number greater than 1 representing
* the base of the nonBase10Num.
* @return the base 10 equivalent on the non-base 10
* number, nonBase10Num.
*/
long toBase10(string nonBase10Num , long base)
{
int i;
int j;
int base10;
for(i = nonBase10Num.length() - 1; i >= 0; i--)
{
digitToValue(nonBase10Num[i]);
base10 = nonBase10Num[i] * pow(base,j);
j++;
}
return base10;
}
int main()
{
int option;
do
{
menu();
cout<<endl;
cout<<"Select an option.";
cin>>option;
switch(option)
{
case 1: cout<<"Enter a non-negative integer:";
int integer;
cin>> integer;
cout<<"To which base should it be converted?";
int baseConvrt;
cin>>baseConvrt;
fromBase10(integer,baseConvrt);
cout<<integer<<" = "<<fromBase10<<" base "<<baseConvrt<<endl;
break;
case 2: cout<<"Enter a non base-10 number:";
int nonBase;
cin>> nonBase;
cout<<"To whish base does this number belong?";
int baseNum;
cin>> baseNum;
toBase10(nonBase,baseNum);
cout<<nonBase<<" = "<<toBase10<<" base "<<baseNum<<endl;
break;
case 3: cout<<"Enter a non base-10 number:";
cin>> nonBase;
cout<<"To which base does this number belong?";
int base;
cin>> base;
cout<<"To which base should it be converted?";
cin>> baseConvrt;
toBase10(nonBase,base);
fromBase10(toBase10,baseConvrt);
cout<<nonBase<<" = "<<fromBase10<<" base "<<baseConvrt;
break;
case 0:
break;
default:
cout<<"Invalid option... please try again."<<endl;
}
}while(option != 0);
return 0;
|