Dabeen Park/C /Changebasenumber
#include <iostream>
- include <cstring>
using namespace std;
int main()
{
int k, j, again;
int value1, value_con;
int value2 = 0;
char num[100];
char ext[100];
cout << "----------------------------------------------" << endl;
cout << "EP1 base-k converter by 20141222 Dabin Park" << endl;
cout << "----------------------------------------------" << endl;
while(1)
{
for(int m=0;m<100;m++)
{
num[m] = 0;
ext[m] = 0;
}
cout << "Provide k, number, and j" << endl;
cin >> k;
cin.ignore(2, ' ');
cin.getline(num, 100, ' ');
cin >> j;
cout << "k:" << k << endl << "j:" << j << endl << "num:" << num << endl;
int p = 0;
bool check = true;
if(k>16 || j>16)
{
cout << "*The system cannot handle a base > 16" << endl;
continue;
}
else if(k<2 || j<2)
{
cout << "*The system cannot handle a base < 2" << endl;
continue;
}
else
{
for(int i=0;i<strlen(num);i++)
{
if (num[i] < 48 && num[0]!=45)
check = false;
if(k<=10)
{
if((num[i]-48)>=k)
check = false;
}
else
if ((num[i] > 57 && num[i] < 97) || num[i] >= k + 87)
check = false;
}
}
if (!check)
{
cout << "*Your input is not in the given base" << endl;
continue;
}
if(num[0]==45)
{
for(int v=1;v<=strlen(num);v++)
{
if(num[v]>=48 && num[v]<=57)
value1 = num[v] - 48;
else
value1 = num[v] - 87;
for(int b = 0; b < strlen(num)-v-1;b++)
value1 *= k;
value2 += value1;
}
}
else
{
for(int v=0;v<strlen(num);v++)
{
if(num[v]>=48 && num[v]<=57)
value1 = num[v] - 48;
else
value1 = num[v] - 87;
for(int b = 0; b < strlen(num)-v-1;b++)
value1 *= k;
value2 += value1;
}
}
do
{
value_con = value2 % j;
if(value_con <=9)
value_con = value_con + 48;
else
value_con = value_con + 87;
ext[p] = value_con;
p++;
value2 = value2 / j;
}while(value2!=0);
cout << "Your output is ";
if(num[0]==45)
{
cout << "-";
for(int i=strlen(ext)-1;i>=0;i--)
cout << ext[i];
}
else
{
for(int i=strlen(ext)-1;i>=0;i--)
cout << ext[i];
}
cout << " in base-" << j << endl;
cout << "Again? (-1 to quit or 1 to test again):" << endl;
cin >> again;
if(again==-1)
{
cout << "Bye" << endl;
break;
}
}
}