请问C语言能否表示8进制和16进制的小数?如果能,怎么表示?

2024-12-05 02:57:10
推荐回答(2个)
回答(1):

2,8,10,16相互转换的程序
#include

two_eight()
{
char *p,s[20];
long n;
int i;
p=s;
printf(" 请输入一个二进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*2+*p-'0';
p++;
}
i=0;
while(n)
{
s[i]=n%8;
n=n/8;
i++;
}
printf(" 2 进制转化为 8 进制:");
for(i--;i>=0;i--)
printf("%d",s[i]);
printf("\n");
}
two_ten()
{
char *p,s[20];
long n;
p=s;
printf(" 请输入一个二进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*2+*p-'0';
p++;
}
printf(" 2 进制转化为 10 进制:%d\n",n);
}
two_sixten()
{
char *p,s[20],str[20];
long n;
int i,mark;
p=s;
printf(" 请输入一个二进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*2+*p-'0';
p++;
}
i=0;
while(n)
{
s[i]=n;
n=n/16;
mark=s[i];
switch(mark)
{
case 0: str[i]='0'; break;
case 1: str[i]='1'; break;
case 2: str[i]='2'; break;
case 3: str[i]='3'; break;
case 4: str[i]='4'; break;
case 5: str[i]='5'; break;
case 6: str[i]='6'; break;
case 7: str[i]='7'; break;
case 8: str[i]='8'; break;
case 9: str[i]='9'; break;
case 10: str[i]='A'; break;
case 11: str[i]='B'; break;
case 12: str[i]='C'; break;
case 13: str[i]='D'; break;
case 14: str[i]='E'; break;
case 15: str[i]='F'; break;
}
i++;
}
printf(" 2 进制转化为 16 进制:");
for(i--;i>=0;i--)
printf("%c",str[i]);
printf("\n");
}

eight_two()
{
char *p,s[20];
long n;
int i;
p=s;
printf(" 请输入一个八进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*8+*p-'0';
p++;
}
i=0;
while(n)
{
s[i]=n%2;
n=n/2;
i++;
}
printf(" 8 进制转化为 2 进制:");
for(i--;i>=0;i--)
printf("%d",s[i]);
printf("\n");
}

eight_ten()
{
char *p,s[20];
long n;
p=s;
printf(" 请输入一个八进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*8+*p-'0';
p++;
}
printf(" 8 进制转化为 10 进制:%d\n",n);
}
eight_sixten()
{
char *p,s[20],str[20];
long n;
int i,mark;
p=s;
printf(" 请输入一个八进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*8+*p-'0';
p++;
}
i=0;
while(n)
{
s[i]=n;
n=n/16;
mark=s[i];
switch(mark)
{
case 0: str[i]='0'; break;
case 1: str[i]='1'; break;
case 2: str[i]='2'; break;
case 3: str[i]='3'; break;
case 4: str[i]='4'; break;
case 5: str[i]='5'; break;
case 6: str[i]='6'; break;
case 7: str[i]='7'; break;
case 8: str[i]='8'; break;
case 9: str[i]='9'; break;
case 10: str[i]='A'; break;
case 11: str[i]='B'; break;
case 12: str[i]='C'; break;
case 13: str[i]='D'; break;
case 14: str[i]='E'; break;
case 15: str[i]='F'; break;
}
i++;
}
printf(" 8 进制转化为 16 进制:");
for(i--;i>=0;i--)
printf("%c",str[i]);
printf("\n");
}

ten_two()
{
int s[20],n,i;
printf(" 请输入一个10进制数: ");
scanf("%d",&n);
i=0;
while(n)
{
s[i]=n%2;
n=n/2;
i++;
}
printf(" 10 进制转化为 2 进制::");
for(i--;i>=0;i--)
printf("%d",s[i]);
printf("\n");
}
ten_eight()
{
int s[20],n,i;
printf(" 请输入一个10进制数: ");
scanf("%d",&n);
i=0;
while(n)
{
s[i]=n%8;
n=n/8;
i++;
}
printf(" 10 进制转化为 8 进制:");
for(i--;i>=0;i--)
printf("%d",s[i]);
printf("\n");
}
ten_sixten()
{
int s[20],n,i,mark;
char str[20];
printf(" 请输入一个10进制数: ");
scanf("%d",&n);
i=0;
while(n)
{
s[i]=n;
n=n/16;
mark=s[i];
switch(mark)
{
case 0: str[i]='0'; break;
case 1: str[i]='1'; break;
case 2: str[i]='2'; break;
case 3: str[i]='3'; break;
case 4: str[i]='4'; break;
case 5: str[i]='5'; break;
case 6: str[i]='6'; break;
case 7: str[i]='7'; break;
case 8: str[i]='8'; break;
case 9: str[i]='9'; break;
case 10: str[i]='A'; break;
case 11: str[i]='B'; break;
case 12: str[i]='C'; break;
case 13: str[i]='D'; break;
case 14: str[i]='E'; break;
case 15: str[i]='F'; break;
}
i++;
}
printf(" 10 进制转化为 16 进制:");
for(i--;i>=0;i--)
printf("%c",str[i]);
printf("\n");
}

sixten_two()
{
int s[20],n=0,i,j;
char str[20];
printf(" 请输入一个16进制数: ");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{
switch(str[i])
{
case '0': s[i]=0; break;
case '1': s[i]=1; break;
case '2': s[i]=2; break;
case '3': s[i]=3; break;
case '4': s[i]=4; break;
case '5': s[i]=5; break;
case '6': s[i]=6; break;
case '7': s[i]=7; break;
case '8': s[i]=8; break;
case '9': s[i]=9; break;
case 'A': s[i]=10; break;
case 'B': s[i]=11; break;
case 'C': s[i]=12; break;
case 'D': s[i]=13; break;
case 'E': s[i]=14; break;
case 'F': s[i]=15; break;
case 'a': s[i]=10; break;
case 'b': s[i]=11; break;
case 'c': s[i]=12; break;
case 'd': s[i]=13; break;
case 'e': s[i]=14; break;
case 'f': s[i]=15; break;
}
i++;
}
for(j=0;j {
n=n*16+s[j];
}
i=0;
while(n)
{
s[i]=n%2;
n=n/2;
i++;
}
printf(" 16 进制转化为 2 进制::");
for(i--;i>=0;i--)
printf("%d",s[i]);
printf("\n");
}

sixten_eight()
{
int s[20],n=0,i,j;
char str[20];
printf(" 请输入一个16进制数: ");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{
switch(str[i])
{
case '0': s[i]=0; break;
case '1': s[i]=1; break;
case '2': s[i]=2; break;
case '3': s[i]=3; break;
case '4': s[i]=4; break;
case '5': s[i]=5; break;
case '6': s[i]=6; break;
case '7': s[i]=7; break;
case '8': s[i]=8; break;
case '9': s[i]=9; break;
case 'A': s[i]=10; break;
case 'B': s[i]=11; break;
case 'C': s[i]=12; break;
case 'D': s[i]=13; break;
case 'E': s[i]=14; break;
case 'F': s[i]=15; break;
case 'a': s[i]=10; break;
case 'b': s[i]=11; break;
case 'c': s[i]=12; break;
case 'd': s[i]=13; break;
case 'e': s[i]=14; break;
case 'f': s[i]=15; break;
}
i++;
}
for(j=0;j {
n=n*16+s[j];
}
i=0;
while(n)
{
s[i]=n%8;
n=n/8;
i++;
}
printf(" 16 进制转化为 8 进制:");
for(i--;i>=0;i--)
printf("%d",s[i]);
printf("\n");
}

sixten_ten()
{
int s[20],n=0,i,j;
char str[20];
printf(" 请输入一个16进制数: ");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{
switch(str[i])
{
case '0': s[i]=0; break;
case '1': s[i]=1; break;
case '2': s[i]=2; break;
case '3': s[i]=3; break;
case '4': s[i]=4; break;
case '5': s[i]=5; break;
case '6': s[i]=6; break;
case '7': s[i]=7; break;
case '8': s[i]=8; break;
case '9': s[i]=9; break;
case 'A': s[i]=10; break;
case 'B': s[i]=11; break;
case 'C': s[i]=12; break;
case 'D': s[i]=13; break;
case 'E': s[i]=14; break;
case 'F': s[i]=15; break;
case 'a': s[i]=10; break;
case 'b': s[i]=11; break;
case 'c': s[i]=12; break;
case 'd': s[i]=13; break;
case 'e': s[i]=14; break;
case 'f': s[i]=15; break;
}
i++;
}
for(j=0;j {
n=n*16+s[j];
}
printf(" 16 进制转化为 10 进制:%d\n",n);
}

void main()
{
int select,i; char str[20], n ;
printf("\n");
printf("\t**************************欢迎使用******************************\n");
printf("\t*----------------万能进制转换器v1.0 版-------------------------*\n");
printf("\t*——使用方法: *\n");
printf("\t* 请选择输入下列功能前的数字序号后按enter键进入。 *\n");
printf("\t* ~~~~仅用于学习交流。 *\n");
printf("\t*\t\t\t ------祝你成功 *\n");
printf("\t****************************************************************\n");
printf("\n");
printf("\n");
printf("\t 1 : 2 进制转化为 8 进制。");
printf(" 2 : 2 进制转化为 10 进制。\n");
printf("\t 3 : 2 进制转化为 16 进制。");
printf(" 4 : 8 进制转化为 2 进制。\n");
printf("\t 5 : 8 进制转化为 10 进制。");
printf(" 6 : 8 进制转化为 16 进制。\n");
printf("\t 7 :10 进制转化为 2 进制。");
printf(" 8 :10 进制转化为 8 进制。\n");
printf("\t 9 :10 进制转化为 16 进制。");
printf(" 10 :16 进制转化为 2 进制。\n");
printf("\t 11 :16 进制转化为 8 进制。");
printf(" 12 :16 进制转化为 10 进制。\n");
printf("\n");
printf("按任意键 回车 后继续,按 n 键 回车 后退出程序。");
n=getchar();
printf("\n");
while(n!='n')
{
printf("请选择您要转换的类型!!!");
scanf("%d",&select);
for(;select<1||select>12;)
{
printf("输入错误,请重新输入!!\n");
scanf("%d",&select);
}
switch(select)
{
case 1: two_eight(); break;
case 2: two_ten(); break;
case 3: two_sixten(); break;
case 4: eight_two(); break;
case 5: eight_ten(); break;
case 6: eight_sixten(); break;
case 7: ten_two(); break;
case 8: ten_eight(); break;
case 9: ten_sixten(); break;
case 10: sixten_two(); break;
case 11: sixten_eight(); break;
case 12: sixten_ten(); break;
}
printf("按任意键回车继续,按 n 键退出程序。");
getchar();
n=getchar();
printf("\n");

}
}

回答(2):

可以,
八进制数以0开头,后面数字为0到7,如010
十六进制以0x开头,后面数字为0到9和a、b、c、d、e、f,如0x10 和0x1a
在c语言中可以用来声明变量,如
int a=010,b=0x10,c=0x1a