Skip to content

Commit

Permalink
add base conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Nov 4, 2024
1 parent e5dad3f commit ed5551f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions conversions/进制转换.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char f[]="0123456789ABCDEF";
void A(int n,int m)
{
int i=0;
char a[1000];
while(n)
{
a[i]=f[n%m];
n=n/m;
i++;
}
while(i--)
printf("%c",a[i]);
}

void B(int n,int m)
{
if(n<m)
{
if(n<10)
printf("%d",n);
else
printf("%c",n-10+'A');
}
else
{
B(n/m,m);
if(n%m<10)
printf("%d",n%m);
else
printf("%d",n%m-10+'A');
}
}

int main()
{
int i,j;
printf("输入数字:\n");
scanf("%d",&i);
printf("输入要转换的进制:\n");
scanf("%d",&j);
printf("输出转换后的数字:");
B(i,j);
printf("\n");
A(i,j);
system("pause");
return 0;
}

0 comments on commit ed5551f

Please sign in to comment.