C++设计一个程序,输入一个十进制数,输出相应的二,八,十六进制数,用函数实现

2024-12-01 19:21:17
推荐回答(2个)
回答1:

希望对你有帮助
#include
#include
void showMenu()
{
char *str[5]={"请选择:","1:十进制改十六进制","2:十进制改二进制","3:十进制改八进制","4:退出"};
int i;
for(i=0;i<5;i++)
printf("%s\n",str[i]);
}
f1()
{
int a[1000], i, n;
printf("请输入要转换的十进制数");
scanf( "%d", &n );
for ( i=0; n>0; i++ )
{
a[i] = n%16;
n /= 16;
}
for ( i--;i>=0; i-- )
printf( "%X ", a[i] );
system("pause");
}
f2()
{
int a[1000], i, n;
printf("请输入要转换的十进制数");
scanf( "%d", &n );
for ( i=0; n>0; i++ )
{
a[i] = n%2;
n /= 2;
}
for ( i--;i>=0; i-- )
printf( "%X ", a[i] );
system("pause");
}
f3()
{
int a[1000], i, n;
printf("请输入要转换的十进制数");
scanf( "%d", &n );
for ( i=0; n>0; i++ )
{
a[i] = n%8;
n /= 8;
}
for ( i--;i>=0; i-- )
printf( "%X ", a[i] );
system("pause");
}
void main()
{
char ch;

system("cls");
showMenu();
for(;;)
{
ch=getch();
switch(ch)
{
case'1':f1();
break;
case'2':f2();
break;
case'3':f3();
break;
case'4':
return;
default:
break;
}
}
}

回答2:

很简单的问题,给你个源代码
#include

void main()
{
int a=0;

printf("please input your number:\n");
scanf("%x",&a);
printf("the result is %d",a);
}