当前位置: 代码迷 >> C语言 >> [转载]进制转换C语言实现
  详细解决方案

[转载]进制转换C语言实现

热度:126   发布时间:2007-04-09 02:32:05.0
[转载]进制转换C语言实现

#i nclude<stdio.h>

#define N 50
void binarysys();
void octalsys();
void hex();

void main()
{
printf(" &进制转换器& \n");
int select;
do
{
printf("==========================================================================\n");
printf("0、退出\t1、十进制~二进制\t2、十进制~八进制\t3、十进制~十六进制\n");
printf("请选择功能:");
scanf("%d",&select);
if(select==0)
{
break;
}
switch(select)
{
case 1: binarysys();break;
case 2: octalsys();break;
case 3: hex();break;
default : printf("请输入0~3其中的数!\n");
}
}while(1);
}

void binarysys()
{
long int number,i=0;
int arr[N];
printf("请输入一个数:");
scanf("%d",&number);
while(number>=0)
{
if(number<2)
{
arr[i]=number;
break;
}
arr[i]=number%2;
number/=2;
i++;
}
printf("这个数转为二进制后为:");
for(;i>=0;i--)
{
printf("%d",arr[i]);
}
printf("\n");

}

void octalsys()
{
long int number,i=0;
int arr[N];
printf("请输入一个数:");
scanf("%ld",&number);
while(number>=0)
{
if(number<8)
{
arr[i]=number;
break;
}
arr[i]=number%8;
number/=8;
i++;
}
printf("这个数转为八进制后为:");
for(;i>=0;i--)
{
printf("%d",arr[i]);
}
printf("\n");
}

void hex()
{
long int number,i=0;
int arr[N];
printf("请输入一个数:");
scanf("%ld",&number);
while(number>=0)
{
if(number<16)
{
arr[i]=number;
break;
}
arr[i]=number%16;
number/=16;
i++;
}
printf("这个数转为十六进制后为:");
for(;i>=0;i--)
{
switch(arr[i])
{
case 1:printf("%d",arr[i]);break;
case 2:printf("%d",arr[i]);break;
case 3:printf("%d",arr[i]);break;
case 4:printf("%d",arr[i]);break;
case 5:printf("%d",arr[i]);break;
case 6:printf("%d",arr[i]);break;
case 7:printf("%d",arr[i]);break;
case 8:printf("%d",arr[i]);break;
case 9:printf("%d",arr[i]);break;
case 10:
arr[i]='A';
printf("%c",arr[i]);break;
case 11:
arr[i]='B';
printf("%c",arr[i]);break;
case 12:
arr[i]='C';
printf("%c",arr[i]);break;
case 13:
arr[i]='D';
printf("%c",arr[i]);break;
case 14:
arr[i]='E';
printf("%c",arr[i]);break;
case 15:
arr[i]='F';
printf("%c",arr[i]);break;
case 16:
arr[i]='G';
printf("%c",arr[i]);break;
}
}
printf("\n");
}

搜索更多相关的解决方案: C语言  进制  

----------------解决方案--------------------------------------------------------
  相关解决方案