求C语言程序代码?

发布网友 发布时间: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");
}

 

声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com