十进制转换为二进制、八进制、十六进制。

来源:互联网 发布:聚划算软件 编辑:程序博客网 时间:2024/04/27 20:55

原文  十进制转换为二进制、八进制、十六进制。

分析:

            

代码:

[cpp] view plaincopyprint?
  1. #include "stdio.h"  
  2.   
  3. /* 函数声明 */  
  4. void GetBibary(long int x);  
  5. void GetOctal(long int x);  
  6. void GetHexadecimal(long int x);  
  7. /* 主函数 */  
  8. int main(void)  
  9. {  
  10.    long int x;  
  11.    printf("我会帮您把大于等于零的十进制数转换成二进制、八进制、十六进制数。\n");  
  12.    printf("请输入您想要转换的十进制数:");  
  13.    scanf("%ld",&x);  
  14.    if(x<0)  
  15.      {  
  16.         printf("请输入大于等于零的十进制数!\n");  
  17.      }  
  18.    else  
  19.      {  
  20.         GetBibary(x);  
  21.         GetOctal(x);  
  22.         GetHexadecimal(x);  
  23.      }  
  24.    return 0;  
  25. }  
  26. /* 求二进制的函数 */  
  27. void GetBibary(long int x)  
  28. {  
  29.   long int X,quotient,remainder;  
  30.   long int i=0,j,binary[32];  
  31.   X=x;  
  32.   if(x>=0&&x<2)  
  33.     {  
  34.       printf("%ld的二进制数是%ld.\n",x,x);  
  35.     }  
  36.   else  
  37.     {  
  38.       do  
  39.        {  
  40.          quotient=x/2;  
  41.          remainder=x%2;  
  42.          binary[i]=remainder;  
  43.          x=quotient;  
  44.          i++;  
  45.        }while(x>=2);  
  46.       binary[i]=quotient;  
  47.       printf("%ld的二进制数为:",X);  
  48.       for(j=i;j>=0;j--)  
  49.          {  
  50.            printf("%ld",binary[j]);  
  51.          }  
  52.       printf("\n");  
  53.     }  
  54. }  
  55. /* 求八进制的函数 */  
  56. void GetOctal(long int x)  
  57. {  
  58.   long int X,quotient,remainder;  
  59.   long int i=0,j,octal[16];  
  60.   X=x;  
  61.   if(x>=0&&x<8)  
  62.     {  
  63.       printf("%ld的八进制数是%ld.\n",X,x);  
  64.     }  
  65.   else  
  66.     {  
  67.       do  
  68.        {  
  69.          quotient=x/8;  
  70.          remainder=x%8;  
  71.          octal[i]=remainder;  
  72.          x=quotient;  
  73.          i++;  
  74.        }while(x>=8);  
  75.       octal[i]=quotient;  
  76.       printf("%ld的八进制数为:",X);  
  77.       for(j=i;j>=0;j--)  
  78.          {  
  79.            printf("%ld",octal[j]);  
  80.          }  
  81.       printf("\n");  
  82.     }  
  83.   
  84. }  
  85. /* 求十六进制的函数 */  
  86. void GetHexadecimal(long int x)  
  87. {  
  88.   long int X,quotient,remainder;  
  89.   long int i=0,j,hexadecimal[8];  
  90.   char change[6]={'F','E','D','C','B','A'};  
  91.   X=x;  
  92.   if(x>=0&&x<10)  
  93.     {  
  94.       printf("%ld的十六进制数是%ld.\n",X,x);  
  95.     }  
  96.   else if(x>=10&&x<16)  
  97.          {  
  98.             x=16-(x+1);  
  99.             printf("%ld的十六进制数是%c.\n",X,change[x]);  
  100.          }  
  101.   else  
  102.     {  
  103.       do  
  104.        {  
  105.          quotient=x/16;  
  106.          remainder=x%16;  
  107.          hexadecimal[i]=remainder;  
  108.          x=quotient;  
  109.          i++;  
  110.        }while(x>=16);  
  111.       hexadecimal[i]=quotient;  
  112.       printf("%ld的十六进制数为:",X);  
  113.       for(j=i;j>=0;j--)  
  114.          {  
  115.            if(hexadecimal[j]>=0&&hexadecimal[j]<10)  
  116.              {  
  117.                printf("%ld",hexadecimal[j]);  
  118.              }  
  119.            else  
  120.              {  
  121.                hexadecimal[j]=16-(hexadecimal[j]+1);  
  122.                printf("%c",change[(hexadecimal[j])]);  
  123.              }  
  124.          }  
  125.       printf("\n");  
  126.     }  
  127.   
  128. }  

编译结果:


点石成金  写于  2012/08/10/01:27


原创粉丝点击