Write C language program to carry on the base conversion problem example
- 2020-04-02 03:14:15
- OfStack
The title
Title description:
Converts the base M number X to the base N number output.
Input:
The first line of input contains two integers: M and N(2)
<
= M, N
<
= 36).
The next line is a number X, X is a base M number, and now you have to convert the base M number X to the base N number output.
Output:
Output the number in base N of X.
Sample input:
10 of 16
F
Sample output:
15
Tip:
The input is uppercase, the output is lowercase, and there is big data.
Train of thought
Large integer multiplication converts to a decimal array
The large integer division converts to the specified base number
AC code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 2000
char str[LEN], another[LEN];
int ten[LEN];
int switchToTen();
void switchToAnother(int k, int n);
int main()
{
int m, n, k;
while (scanf("%d %d", &m, &n) != EOF) {
scanf("%s", str);
k = switchToTen(m);
switchToAnother(k, n);
}
return 0;
}
int switchToTen(int m)
{
int i, j, len, k, c;
//Initialize the
len = strlen(str);
k = 1;
memset(ten, 0, sizeof(ten));
//Convert to base 10
for (i = 0; i < len; i ++) {
for (j = 0; j < k; j ++) {
ten[j] *= m;
}
if (str[i] >= '0' && str[i] <= '9') {
ten[0] += str[i] - '0';
}else if (str[i] >= 'A' && str[i] <= 'Z') {
ten[0] += str[i] - 'A' + 10;
}else if (str[i] >= 'a' && str[i] <= 'z') {
ten[0] += str[i] - 'a' + 10;
}
for (j = c = 0; j < k; j ++) {
ten[j] += c;
if (ten[j] >= 10) {
c = ten[j] / 10;
ten[j] %= 10;
}else {
c = 0;
}
}
while (c) {
ten[k ++] = c % 10;
c /= 10;
}
}
//Flip the array
int temp;
for (i = 0, j = k - 1; i < j; i ++, j --) {
temp = ten[i];
ten[i] = ten[j];
ten[j] = temp;
}
return k;
}
void switchToAnother(int k, int n)
{
int sum, i, r, t, d;
sum = 1;
r = 0;
memset(another, 0, sizeof(another));
while (sum) {
sum = 0;
for (i = 0; i < k; i ++) {
d = ten[i] / n;
sum += d;
if (i == k - 1) {
t = ten[i] % n;
if (t >= 0 && t <= 9) {
another[r] = t + '0';
}else {
another[r] = t - 10 + 'a';
}
r ++;
}else {
ten[i + 1] += ten[i] % n * 10;
}
ten[i] = d;
}
}
//Print is output
for (i = r - 1; i >= 0; i --) {
printf("%c", another[i]);
}
printf("n");
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Problem: 1080
User: wangzhengyi
Language: C
Result: Accepted
Time: 170 ms
Memory: 920 KB
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
Attached below is a binary, octal, decimal, hexadecimal conversion of the C language program (a bit long, well, see function n in one on the XD) ~
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void D_B(int);
void D_O(int);
void D_X(int);
void B_D(int);
void B_O(int);
void B_X(int);
void O_B(int);
void O_D(int);
void O_X(int);
void X_B(char r[],int k);
void X_D(char r[],int k);
void X_O(char r[],int k);
void main()
{
int i,j,k=0;
int q;
char r[10];
printf("+=============================================================+n");
printf("| huan To meet make with Into the system turn in cheng sequence |n");
printf("+=============================================================+n");
printf(" note : this version this only do is The whole The number the Into the system turn in ! ! ");
do
{
q=0;
//fflush(stdin);
printf("n Please select the base to be converted :n0 , exit n1 , binary n2 , octal, n3 , decimal n4 , hexadecimal n");
scanf("%d",&i);
switch (i)
{
case 1: printf("n Please select the converted base :n0 , exit n1 , binary n2 , octal, n3 , decimal n4 , hexadecimal n");
scanf("%d",&j);
switch(j)
{
case 1: printf("n No conversion between bases! n");
q=1;
break;
case 2: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
B_O(k);
q=1;
break;
case 3: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
B_D(k);
q=1;
break;
case 4: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
B_X(k);
q=1;
break;
case 0:
printf(" Thanks for using!! ");
}
break;
case 2: printf("n Please select the converted base :n0 , exit n1 , binary n2 , octal, n3 , decimal n4 , hexadecimal n");
scanf("%d",&j);
switch(j)
{
case 2: printf("n No conversion between bases! n");
q=1;
break;
case 1: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
O_B(k);
q=1;
break;
case 3: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
O_D(k);
q=1;
break;
case 4: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
O_X(k);
q=1;
break;
case 0:
printf(" Thanks for using!! ");
}
break;
case 3: printf("n Please select the converted base :n0 , exit n1 , binary n2 , octal, n3 , decimal n4 , hexadecimal n");
scanf("%d",&j);
switch(j)
{
case 3: printf("n No conversion between bases! n");
q=1;
break;
case 1: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
D_B(k);
q=1;
break;
case 2: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
D_O(k);
q=1;
break;
case 4: printf("n Please enter the number you want to convert: ");
scanf("%d",&k);
D_X(k);
q=1;
break;
case 0:
printf(" Thanks for using!! ");
}
break;
case 4: printf("n Please select the converted base :n0 , exit n1 , binary n2 , octal, n3 , decimal n4 , hexadecimal n");
scanf("%d",&j);
switch(j)
{
case 4: printf("n No conversion between bases! n");
q=1;
break;
case 1: printf("n Please enter the number you want to convert :");
fflush(stdin);
gets(r);
for(k=0;;k++)
{
if(r[k]=='0')
break;
}
X_B(r,k);
q=1;
break;
case 2: printf("n Please enter the number you want to convert :");
fflush(stdin);
gets(r);
for(k=0;;k++)
{
if(r[k]=='0')
break;
}
X_O(r,k);
q=1;
break;
case 3: printf("n Please enter the number you want to convert :");
fflush(stdin);
gets(r);
for(k=0;;k++)
{
if(r[k]=='0')
break;
}
X_D(r,k);
q=1;
break;
case 0:
printf(" Thanks for using!! ");
}
break;
case 0: printf("n Thanks for using! n");
}
}while(q==1);
}
/////// following: binary conversion to decimal, octal, hexadecimal.
void B_D(int a)
{
int i,s=0;
int result=0;
for(i=1;a!=0;i*=2)
{
if(a%10>1)
{
s=1;
break;
}
else
{
result+=(a%10)*i;
a=a/10;
}
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
printf("n The converted number is: %dn",result);
}
void B_O(int a)
{
int i,j,k,s=0;
int p[30];
int result=0;
for(i=1;a!=0;i*=2)
{
if(a%10>1)
{
s=1;
break;
}
else
{
result+=(a%10)*i;
a=a/10;
}
}
for(j=0;result!=0;j++)
{
p[j]=result%8;
result=result/8;
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
{
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%d",p[k]);
}
printf("n");
}
}
void B_X(int a)
{
int i,j,k,s=0;
char p[30];
int result=0;
for(i=1;a!=0;i*=2)
{
if(a%10>1)
{
s=1;
break;
}
else
{
result+=(a%10)*i;
a=a/10;
}
}
for(j=0;result!=0;j++)
{
p[j]=result%16;
result=result/16;
if (p[j]>10)
{
switch(p[j])
{
case 10: p[j]='A';
break;
case 11: p[j]='B';
break;
case 12: p[j]='C';
break;
case 13: p[j]='D';
break;
case 14: p[j]='E';
break;
case 15: p[j]='F';
break;
}
}
else p[j]+=48;
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
{
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%c",p[k]);
}
printf("n");
}
}
/////// following: octal conversion to binary, decimal, hexadecimal.
void O_B(int a)
{
int i,j,k,s=0;
int result=0;
int p[30];
for(i=1;a!=0;i*=8)
{if(a%10>7)
{
s=1;
break;
}
else
{
result+=(a%10)*i;
a=a/10;
}
}
for(j=0;result!=0;j++)
{
p[j]=result%2;
result=result/2;
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
{
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%d",p[k]);
}
printf("n");
}
}
void O_D(int a)
{
int i,s=0;
int result=0;
for(i=1;a!=0;i*=8)
{
if(a%10>7)
{
s=1;
break;
}
else
{
result+=(a%10)*i;
a=a/10;
}
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
{
printf("n The converted number is: %dn",result);
}
}
void O_X(int a)
{
int i,j,k,s=0;
char p[30];
int result=0;
for(i=1;a!=0;i*=8)
{
if(a%10>7)
{
s=1;
break;
}
else
{
result+=(a%10)*i;
a=a/10;
}
}
for(j=0;result!=0;j++)
{
p[j]=result%16;
result=result/16;
if(p[j]<10)
p[j]+=48;
else
{
switch(p[j])
{
case 10: p[j]='A';
break;
case 11: p[j]='B';
break;
case 12: p[j]='C';
break;
case 13: p[j]='D';
break;
case 14: p[j]='E';
break;
case 15: p[j]='F';
break;
}
}
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
{
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%c",p[k]);
}
printf("n");
}
}
/////// following: hexadecimal conversion to decimal, binary, octal.
void X_D(char a[],int k)
{
int i,j,s=0;
int result=0;
int b[50];
for(i=0;i<k;i++)
{
if(a[i]<='9'&&a[i]>='1')
{ b[i]=a[i]-48;
}
else
{
switch(a[i])
{
case 'A': b[i]=10;
break;
case 'B': b[i]=11;
break;
case 'C': b[i]=12;
break;
case 'D': b[i]=13;
break;
case 'E': b[i]=14;
break;
case 'F': b[i]=15;
break;
case 'a': b[i]=10;
break;
case 'b': b[i]=11;
break;
case 'c': b[i]=12;
break;
case 'd': b[i]=13;
break;
case 'e': b[i]=14;
break;
case 'f': b[i]=15;
break;
default: s=1;
}
}
}
for(i=1,j=k-1;j>=0;j--,i*=16)
{
result+=b[j]*i;
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
{
printf("n The converted number is: %d",result);
}
}
void X_B(char a[],int k)
{
int i,j,s=0;
int result=0;
int b[50];
int p[30];
for(i=0;i<k;i++)
{
if(a[i]<='9'&&a[i]>='1')
b[i]=a[i]-48;
else
{
switch(a[i])
{
case 'A': b[i]=10;
break;
case 'B': b[i]=11;
break;
case 'C': b[i]=12;
break;
case 'D': b[i]=13;
break;
case 'E': b[i]=14;
break;
case 'F': b[i]=15;
break;
case 'a': b[i]=10;
break;
case 'b': b[i]=11;
break;
case 'c': b[i]=12;
break;
case 'd': b[i]=13;
break;
case 'e': b[i]=14;
break;
case 'f': b[i]=15;
break;
default: s=1;
}
}
}
for(j=k-1,i=1;j>=0;j--,i*=16)
{
result+=b[j]*i;
}
for(j=0;result!=0;j++)
{
p[j]=result%2;
result=result/2;
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
{
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%d",p[k]);
}
printf("n");
}
}
void X_O(char a[],int k)
{
int i,j,s=0;
int result=0;
int b[30];
int p[30];
for(i=0;i<k;i++)
{
if(a[i]<='9'&&a[i]>='1')
b[i]=a[i]-48;
else
{
switch(a[i])
{
case 'A': b[i]=10;
break;
case 'B': b[i]=11;
break;
case 'C': b[i]=12;
break;
case 'D': b[i]=13;
break;
case 'E': b[i]=14;
break;
case 'F': b[i]=15;
break;
case 'a': b[i]=10;
break;
case 'b': b[i]=11;
break;
case 'c': b[i]=12;
break;
case 'd': b[i]=13;
break;
case 'e': b[i]=14;
break;
case 'f': b[i]=15;
break;
default: s=1;
}
}
}
for(j=k-1,i=1;j>=0;j--,i*=16)
{
result+=b[j]*i;
}
for(j=0;result!=0;j++)
{
p[j]=result%8;
result=result/8;
}
if(s==1)
printf(" Your input is incorrect! Please re-enter n");
else
{
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%d",p[k]);
}
printf("n");
}
}
//////Conversion from decimal to binary, octal, hexadecimal.
void D_B(int a)
{
int j,k;
int p[30];
for(j=0;a!=0;j++)
{
p[j]=a%2;
a=a/2;
}
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%d",p[k]);
}
printf("n");
}
void D_O(int a)
{
int j,k;
int p[30];
for(j=0;a!=0;j++)
{
p[j]=a%8;
a=a/8;
}
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%d",p[k]);
}
printf("n");
}
void D_X(int a)
{
int j,k;
int p[30];
for(j=0;a!=0;j++)
{
p[j]=a%16;
a=a/16;
if(p[j]<10)
p[j]+=48;
else
{
switch(p[j])
{
case 10: p[j]='A';
break;
case 11: p[j]='B';
break;
case 12: p[j]='C';
break;
case 13: p[j]='D';
break;
case 14: p[j]='E';
break;
case 15: p[j]='F';
break;
}
}
}
printf("n The converted number is: ");
for(k=j-1;k>=0;k--)
{
printf("%c",p[k]);
}
printf("n");
}