code.club

 找回密碼
 立即註冊
搜索
查看: 10740|回復: 6
打印 上一主題 下一主題

將十進位正整數轉成二,八及十六進位數的程式

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2015-7-1 17:56:29 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最後由 enter 於 2015-8-7 16:15 編輯

#include <stdio.h>
#include <stdlib.h>

int main(void){
        int arr[24];
        char str[25]; //十六進位中會出現字母,所以要另以字串陣列代之,而且最後一位要是'\0',所以多一元素。
        int j,d,x;
        char ch;
        
        do
        {
        
        printf("Enter a number:\n");
        scanf("%d",&d);
        x=d; //因為如果使用 d 在第一輪做完之後會成為0,故要另行取個變數 x ,將 d 的值給它,而且每輪做完後都要重新 x=d 一次。
               
        for(j=0;j<24;j++)
        {
               
                arr[23-j]=x%2; //arr[]的最後一元素號碼是24-1=23
               
                x=x/2; //x除以2之後的商數要繼續除以2,這是本演算法的重點。
               
        }
        printf("The Binary for %d is: ",d);
        
        x=d;
        for(j=0;j<24;j++)
        printf("%d",arr[j]);
                printf("\n");
                //system("pause");
               
                              
        for(j=0;j<24;j++){
            arr[23-j]=x%8; //餘數即為該元素的值。
            x=x/8;
            }         
        printf("The Octal for %d is: ",d);  
                    
        for(j=0;j<24;j++)
        printf("%d",arr[j]);
                printf("\n");
                //system("pause");
        
              x=d;
for(j=0;j<25;j++)
        str[j]='\0'; //每個元素都先給它設成'\0',主要是最後一個要是 '\0' 才能結束字串。

for(j=0;j<24;j++){ // 從倒數第二個元素開始。
        if(x%16==0)
                str[23-j]='0';
        else if(x%16==1)
                str[23-j]='1';
        else if(x%16==2)
                str[23-j]='2';
        else if(x%16==1)
                str[23-j]='1';
        else if(x%16==3)
                str[23-j]='3';
        else if(x%16==4)
                str[23-j]='4';
        else if(x%16==5)
                str[23-j]='5';
        else if(x%16==6)
                str[23-j]='6';
        else if(x%16==7)
                str[23-j]='7';
        else if(x%16==8)
                str[23-j]='8';
        else if(x%16==9)
                str[23-j]='9';
        else if(x%16==10)
                str[23-j]='A';
        else if(x%16==11)
                str[23-j]='B';
        else if(x%16==12)
                str[23-j]='C';
        else if(x%16==13)
                str[23-j]='D';
        else if(x%16==14)
                str[23-j]='E';
        else str[23-j]='F';
               
        x=x/16;
}

        printf("The Hexadecimal for %d is: %s\n",d,str);   
               
                printf("Do you want to continue? y/n\n");
                fflush(stdin); //清掉記憶體中的所有資料,以免下個動作被自動執行為「否」。
                ch=getche();
                printf("\n");
                }while(ch=='y');
        
        
        return 0;
}
回復

使用道具 舉報

沙發
 樓主| 發表於 2015-7-1 20:04:50 | 只看該作者
本帖最後由 enter 於 2015-7-1 20:08 編輯

改進版,去掉前面的0不被印出。


#include <stdio.h>
#include <stdlib.h>

int main(void){
        int arr[24];
        char str[25];
        int j,d,x,k;
        char ch;
        
        do
        {
        
        printf("Enter a number:\n");
        scanf("%d",&d);
        x=d;
               
        for(j=0;j<24;j++)
        {
               
                arr[23-j]=x%2; //arr[]的最後一元素號碼是24-1=23
               
                x=x/2; //x除以2之後的商數要繼續除以2,這是本演算法的重點。
               
        }
        printf("The Binary for %d is: ",d);
        
        x=d;
        k=0;
        for(j=0;j<24;j++)
        {
        if(arr[j]!=0) k=1;
        if(k==1)
         printf("%d",arr[j]);
           }
                printf("\n");
                //system("pause");
               
                              
        for(j=0;j<24;j++){
            arr[23-j]=x%8;
            x=x/8;
            }         
        printf("The Octal for %d is: ",d);  
                    
        k=0;
        for(j=0;j<24;j++)
        {
        if(arr[j]!=0) k=1;
        if(k==1)
         printf("%d",arr[j]);
           }
                printf("\n");
                //system("pause");
        
              x=d;
for(j=0;j<25;j++)
        str[j]='\0';
for(j=0;j<24;j++){
        if(x%16==0)
                str[23-j]='0';
        else if(x%16==1)
                str[23-j]='1';
        else if(x%16==2)
                str[23-j]='2';
        else if(x%16==1)
                str[23-j]='1';
        else if(x%16==3)
                str[23-j]='3';
        else if(x%16==4)
                str[23-j]='4';
        else if(x%16==5)
                str[23-j]='5';
        else if(x%16==6)
                str[23-j]='6';
        else if(x%16==7)
                str[23-j]='7';
        else if(x%16==8)
                str[23-j]='8';
        else if(x%16==9)
                str[23-j]='9';
        else if(x%16==10)
                str[23-j]='A';
        else if(x%16==11)
                str[23-j]='B';
        else if(x%16==12)
                str[23-j]='C';
        else if(x%16==13)
                str[23-j]='D';
        else if(x%16==14)
                str[23-j]='E';
        else str[23-j]='F';
               
        x=x/16;
}
printf("The Hexadecimal for %d is: ",d);
    k=0;
    for(j=0;j<25;j++)
         {
         if(str[j]!='0') k=1;
         if(k==1)
        printf("%c",str[j]);   
            }
                printf("\nDo you want to continue? Enter 'y' to continue, others to quit.\n");
                fflush(stdin); //清掉記憶體中的所有資料,以免下個動作被自動執行為「否」。
                ch=getche();
                printf("\n");
                }while(ch=='y');
        
        
        return 0;
}
回復 支持 反對

使用道具 舉報

板凳
 樓主| 發表於 2015-7-23 23:23:52 | 只看該作者
更簡化版,少了九個 if :

#include <stdio.h>
#include <stdlib.h>

int main(void){
        int arr[24];
        char str[25];
        int j,d,x,k;
        char ch;
        
        do
        {
        
        printf("Enter a number:\n");
        scanf("%d",&d);
        x=d;
               
        for(j=0;j<24;j++)
        {
               
                arr[23-j]=x%2; //arr[]的最後一元素號碼是24-1=23
               
                x=x/2; //x除以2之後的商數要繼續除以2,這是本演算法的重點。
               
        }
        printf("The Binary for %d is: ",d);
        
        x=d;
        k=0;
        for(j=0;j<24;j++)
        {
        if(arr[j]!=0) k=1;
        if(k==1)
         printf("%d",arr[j]);
           }
                printf("\n");
                //system("pause");
               
                              
        for(j=0;j<24;j++){
            arr[23-j]=x%8;
            x=x/8;
            }         
        printf("The Octal for %d is: ",d);  
                    
        k=0;
        for(j=0;j<24;j++)
        {
        if(arr[j]!=0) k=1;
        if(k==1)
         printf("%d",arr[j]);
           }
                printf("\n");
                //system("pause");
        
              x=d;
for(j=0;j<25;j++)
        str[j]='\0';
for(j=0;j<24;j++){
        if(x%16<=9)
        str[23-j]=48+x%16; //'0'的ASCII值是48,所以用48+餘數所對應的字符就是該餘數的值。
        else if(x%16==10)
                str[23-j]='A';
        else if(x%16==11)
                str[23-j]='B';
        else if(x%16==12)
                str[23-j]='C';
        else if(x%16==13)
                str[23-j]='D';
        else if(x%16==14)
                str[23-j]='E';
        else str[23-j]='F';
               
        x=x/16;
}
printf("The Hexadecimal for %d is: ",d);
    k=0;
    for(j=0;j<25;j++)
         {
         if(str[j]!='0') k=1;
         if(k==1)
        printf("%c",str[j]);   
            }
                printf("\nDo you want to continue? Enter 'y' to continue, others to quit.\n");
                fflush(stdin); //清掉記憶體中的所有資料,以免下個動作被自動執行為「否」。
                ch=getche();
                printf("\n");
                }while(ch=='y');
        
        
        return 0;
}
回復 支持 反對

使用道具 舉報

地板
 樓主| 發表於 2015-8-7 16:22:57 | 只看該作者
本帖最後由 enter 於 2015-8-7 16:24 編輯

更新版,將陣列arr[]和str[]全改成32bits的大小,變成arr[32] str[32]以合實際狀況。因為一旦有十住位負數,會用到第一個arr[0],但目前本程式只能適用於正數的轉換而已。

#include <stdio.h>
#include <stdlib.h>

int main(void){
        int arr[32];
        char str[32];
        int j,d,x,k;
        char ch;
        
        do
        {
        
        printf("Enter a number:\n");
        scanf("%d",&d);
        x=d;
               
        for(j=0;j<32;j++)
        {
               
                arr[31-j]=x%2; //arr[]的最後一元素號碼是24-1=23
               
                x=x/2; //x除以2之後的商數要繼續除以2,這是本演算法的重點。
               
        }
        printf("The Binary for %d is: ",d);
        
        x=d;
        k=0;
        for(j=0;j<32;j++)
        {
        if(arr[j]!=0) k=1;
        if(k==1)
         printf("%d",arr[j]);
           }
                printf("\n");
                //system("pause");
               
                              
        for(j=0;j<32;j++){
            arr[31-j]=x%8;
            x=x/8;
            }         
        printf("The Octal for %d is: ",d);  
                    
        k=0;
        for(j=0;j<32;j++)
        {
        if(arr[j]!=0) k=1;
        if(k==1)
         printf("%d",arr[j]);
           }
                printf("\n");
                //system("pause");
        
              x=d;
for(j=0;j<32;j++)
        str[j]='\0';
for(j=0;j<31;j++){
        if(x%16==0)
                str[30-j]='0';
        else if(x%16==1)
                str[30-j]='1';
        else if(x%16==2)
                str[30-j]='2';
        else if(x%16==1)
                str[30-j]='1';
        else if(x%16==3)
                str[30-j]='3';
        else if(x%16==4)
                str[30-j]='4';
        else if(x%16==5)
                str[30-j]='5';
        else if(x%16==6)
                str[30-j]='6';
        else if(x%16==7)
                str[30-j]='7';
        else if(x%16==8)
                str[30-j]='8';
        else if(x%16==9)
                str[30-j]='9';
        else if(x%16==10)
                str[30-j]='A';
        else if(x%16==11)
                str[30-j]='B';
        else if(x%16==12)
                str[30-j]='C';
        else if(x%16==13)
                str[30-j]='D';
        else if(x%16==14)
                str[30-j]='E';
        else str[30-j]='F';
               
        x=x/16;
}
printf("The Hexadecimal for %d is: ",d);
    k=0;
    for(j=0;j<32;j++)
         {
         if(str[j]!='0') k=1;
         if(k==1)
        printf("%c",str[j]);   
            }
                printf("\nDo you want to continue? Enter 'y' to continue, others to quit.\n");
                fflush(stdin); //清掉記憶體中的所有資料,以免下個動作被自動執行為「否」。
                ch=getche();
                printf("\n");
                }while(ch=='y');
        
        
        return 0;
}
回復 支持 反對

使用道具 舉報

5#
 樓主| 發表於 2015-8-7 17:52:17 | 只看該作者
本帖最後由 enter 於 2015-8-7 17:59 編輯

用遞迴函數來把十進位正整數轉為二和八進位,有更簡單的方法。如果直接將參數設定為 long long int,則還可以輸入十幾位數以上的整數。而且更可以直輸入負數。目前發現最強大而且最簡單的寫法。

#include <stdio.h>
#include <stdlib.h>

int bi(int);
int oct(int);

int main(void){
       
        int n;
       
        printf("Please enter a number:\n");
        scanf("%d",&n);
       
        bi(n);
        printf("\n");
        oct(n);
       
        return 0;
}

int bi(int  x){
        if(x!=0){
        bi(x/2);
        printf("%d",x%2);
}
}

int oct(int y){
        if(y!=0){
                oct(y/8);
                printf("%d",y%8);
        }
}
回復 支持 反對

使用道具 舉報

6#
 樓主| 發表於 2015-8-7 21:58:10 | 只看該作者
本帖最後由 enter 於 2015-8-7 22:09 編輯

新發現,其實最大重點竟是輸入的數值的型態,要長達 long long int 型態,則原始的寫法也可以解出很長的數字和負數。但原理仍沒想通。

#include <stdio.h>
#include <stdlib.h>

int main(void){
        int arr[32]; //採一個整數占有 4 byte 共32個bits(0/1)的意思,但arr[32]本身的長度可是占了128byte。
        char str[32];
        //int j,d,x,k;
        char ch;
        int j,k;
        long long x,d; //輸入的數值採 long long (int)
        do
        {
        
        printf("Enter a number:\n");
        scanf("%d",&d);
        x=d;
               
        for(j=0;j<32;j++)
        {
               
                arr[31-j]=x%2; //arr[]的最後一元素號碼是32-1=31
               
                x=x/2; //x除以2之後的商數要繼續除以2,這是本演算法的重點。
               
        }
        printf("The Binary for %d is: ",d);
        
        x=d;
        k=0;
        for(j=0;j<32;j++)
        {
        if(arr[j]!=0) k=1;
        if(k==1)
         printf("%d",arr[j]);
           }
                printf("\n");
                //system("pause");
               
                              
        for(j=0;j<32;j++){
            arr[31-j]=x%8;
            x=x/8;
            }         
        printf("The Octal for %d is: ",d);  
                    
        k=0;
        for(j=0;j<32;j++)
        {
        if(arr[j]!=0) k=1;
        if(k==1)
         printf("%d",arr[j]);
           }
                printf("\n");
                //system("pause");
        
              x=d;
for(j=0;j<32;j++)
        str[j]='\0';
for(j=0;j<31;j++){
        if(x%16==0)
                str[30-j]='0';
        else if(x%16==1)
                str[30-j]='1';
        else if(x%16==2)
                str[30-j]='2';
        else if(x%16==1)
                str[30-j]='1';
        else if(x%16==3)
                str[30-j]='3';
        else if(x%16==4)
                str[30-j]='4';
        else if(x%16==5)
                str[30-j]='5';
        else if(x%16==6)
                str[30-j]='6';
        else if(x%16==7)
                str[30-j]='7';
        else if(x%16==8)
                str[30-j]='8';
        else if(x%16==9)
                str[30-j]='9';
        else if(x%16==10)
                str[30-j]='A';
        else if(x%16==11)
                str[30-j]='B';
        else if(x%16==12)
                str[30-j]='C';
        else if(x%16==13)
                str[30-j]='D';
        else if(x%16==14)
                str[30-j]='E';
        else str[30-j]='F';
               
        x=x/16;
}
printf("The Hexadecimal for %d is: ",d);
    k=0;
    for(j=0;j<32;j++)
         {
         if(str[j]!='0') k=1;
         if(k==1)
        printf("%c",str[j]);   
            }
                printf("\nDo you want to continue? Enter 'y' to continue, others to quit.\n");
                fflush(stdin); //清掉記憶體中的所有資料,以免下個動作被自動執行為「否」。
                ch=getche();
                printf("\n");
                }while(ch=='y');
        
        
        return 0;
}
回復 支持 反對

使用道具 舉報

7#
 樓主| 發表於 2015-8-9 00:59:09 | 只看該作者
本帖最後由 enter 於 2015-8-9 17:21 編輯

更省記憶體的十進位變二進位和八進位的寫法,本法只占用33 byte,而以前的要占用128 byte。

#include <stdio.h>
#include <stdlib.h>

int main(void){
        long long x;
        int i,j;
        char str[33];
       
        printf("enter a number:\n");
        scanf("%d",&x);
        str[32]='\0';
       
        for(i=31;i>=0;i--)
        {
                if((x%2)==0) str='0';
                else str='1';
                x=x/2;
        }
        j=0;
        for(i=0;i<=32;i++){
         if(str=='1') j=1;
          if(j==1) printf("%c",str);
}

          return 0;
}




#include <stdio.h>
#include <stdlib.h>

int main(void)
{
long long x;
  int y,i;
  char str[33];
  
  printf("enter a number:\n");
  scanf("%d",&x);
  
  
  str[32]='\0';
  
  for(i=31;i>=0;i--)
  {
    if(x%8==7) str='7';
    else if(x%8==1) str='1';
    else if(x%8==2) str='2';
    else if(x%8==3) str='3';
    else if(x%8==4) str='4';
    else if(x%8==5) str='5';
    else if(x%8==6) str='6';
    else str='0';
    x=x/8;
    }
    y=0;
    for(i=0;i<33;i++)
    {
      if(str!='0') y=1;
      if(y==1) printf("%c",str);
      }
      
      return 0;
      }
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-4-24 21:48 , Processed in 0.084402 second(s), 16 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表