编写一个函数,实现数制转换。在主函数中输入一个十进制数,输出相应的十六进制数。要求用数组实现,画出流

一定要用数组实现
2024-11-12 11:49:46
推荐回答(1个)
回答1:

//C语言实现:

#include
void main()
{char b[]="0123456789ABCDEF";
long int n,x;
int i,a[40];
printf("\n Please input n=");
scanf("%ld",&n);
x=n;i=0;
while(x>0)
{a[i]=x%16;
x=x/16;
i++;
}
while(i>0)
{printf("%c",b[a[i-1]]);
i--;
}
printf("\n");
}

对慧谈于大数转换可以用如下程序:

#include "Stdio.h"
#include "Conio.h"
char result[100];
int pos;
void change(int *num);
int main(void)
{
char str[100];
int num[100];
int bin[300]=;
int quot[100],residue=0;
int i,j,k=0,suffix=0,index=0;
puts("Input a decimal number:");
gets(str);
for(i=0;i j=i-1;
while(j>=0)
{
num[j]%2?(bin[k++]=1):(bin[k++]=0);
while(index<=j)
{
residue+=num[index];
if(residue>=2)
quot[suffix++]=residue/2;
else if(index)
quot[suffix++]=0;
residue=(num[index]%2)*10;
index++;
}
for(i=0;i j=suffix-1;
residue=suffix=index=0;
}
for(i=0;i for(i=pos-1;i>=0;printf("%c",result[i]),i--);
getch();
return 0;
}
void change(int *num)
{
if(num[3]==0&&num[2]==0&&num[1]==0&&num[0]==0)
sprintf(result+pos,"%c",'碧碧冲0');
if(num[3]==0&&num[2]==0&&num[1]==0&&num[0]==1)
sprintf(result+pos,"%c",'1');
if(num[3]==0&&num[2]==0&&num[1]==1&&num[0]==0)
sprintf(result+pos,"%c",'2');
if(num[3]==0&&num[2]==0&&num[1]==1&&num[0]==1)
sprintf(result+pos,"%c",'悔歼3');
if(num[3]==0&&num[2]==1&&num[1]==0&&num[0]==0)
sprintf(result+pos,"%c",'4');
if(num[3]==0&&num[2]==1&&num[1]==0&&num[0]==1)
sprintf(result+pos,"%c",'5');
if(num[3]==0&&num[2]==1&&num[1]==1&&num[0]==0)
sprintf(result+pos,"%c",'6');
if(num[3]==0&&num[2]==1&&num[1]==1&&num[0]==1)
sprintf(result+pos,"%c",'7');
if(num[3]==1&&num[2]==0&&num[1]==0&&num[0]==0)
sprintf(result+pos,"%c",'8');
if(num[3]==1&&num[2]==0&&num[1]==0&&num[0]==1)
sprintf(result+pos,"%c",'9');
if(num[3]==1&&num[2]==0&&num[1]==1&&num[0]==0)
sprintf(result+pos,"%c",'A');
if(num[3]==1&&num[2]==0&&num[1]==1&&num[0]==1)
sprintf(result+pos,"%c",'B');
if(num[3]==1&&num[2]==1&&num[1]==0&&num[0]==0)
sprintf(result+pos,"%c",'C');
if(num[3]==1&&num[2]==1&&num[1]==0&&num[0]==1)
sprintf(result+pos,"%c",'D');
if(num[3]==1&&num[2]==1&&num[1]==1&&num[0]==0)
sprintf(result+pos,"%c",'E');
if(num[3]==1&&num[2]==1&&num[1]==1&&num[0]==1)
sprintf(result+pos,"%c",'F');
pos++;
}