发布网友 发布时间:2022-04-20 10:27
共1个回答
热心网友 时间:2023-08-18 17:54
#include "stdafx.h"
#include "stdlib.h"
int _tmain(int argc, _TCHAR* argv[])
{
int decimal;
int hex;
int num[20];
int count = 0;
int i;
char ch;
//ch = 'a';
//printf("%d", ch);
for (i = 0; i < 20; i++)
{
num[i] = -1;
}
printf("please input the decimal number:\n");
scanf_s("%d",&decimal);
printf("\n");
printf("please input the system you are going to transform(2 to 16):\n");
scanf_s("%d",&hex);
printf("\n");
printf("\n");
while (decimal >= hex)
{
num[count] = decimal - int(decimal / hex) *hex;
if (num[count] == 10)
num[count] = 'a';
if (num[count] == 11)
num[count] = 'b';
if (num[count] == 12)
num[count] = 'c';
if (num[count] == 13)
num[count] = 'c';
if (num[count] == 14)
num[count] = 'd';
if (num[count] == 15)
num[count] = 'e';
decimal = int(decimal / hex);
count = count++;
}
num[count] = decimal;
count = 0;
for (i = 0; i < 20; i++)
{
if (num[i] >= 0)
count = count++;
else
break;
}
printf("corresponding hex number is :\n");
for (i = count-1; i >= 0; i--)
{
if (num[i] >= 'a' && num[i] <= 'e')
printf("%c", num[i]);
else
printf("%d", num[i]);
}
printf("\n");
system("pause");
}