code.club

 找回密碼
 立即註冊
搜索
查看: 6190|回復: 3

將一般數字轉成二進位的格式印出來

[複製鏈接]
發表於 2015-6-30 02:28:01 | 顯示全部樓層 |閱讀模式
本帖最後由 enter 於 2015-6-30 17:14 編輯

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

int main(vojd){
        int arr[15];
        int j,x;
                char ch;
        do{
        printf("Enter a number:\n");
        scanf("%d",&x);
        
        for(j=0;j<15;j++)
        {
                {
                if(x%2==0)
                arr[14-j]=0; //arr[]的最後一元素號碼是15-1=14
                else arr[14-j]=1;
                }
                x=x/2; //x除以2之後的商數要繼續除以2,這是本演算法的重點。
               
        }
        for(j=0;j<15;j++)
        printf("%d",arr[j]);
                printf("\n");
                system("pause");
               
                printf("Do you want to continue? y/n\n");
                fflush(stdin); //清掉記憶體中的所有資料,以免下個動作被自動執行為「否」。
                ch=getche();
                }while(ch=='y');
        
        
        return 0;
}
回復

使用道具 舉報

 樓主| 發表於 2015-7-1 01:04:22 | 顯示全部樓層
本帖最後由 enter 於 2015-7-1 02:05 編輯

增加選項,可以將十進位的轉為二進位或八進位格式。並且將陣列位元擴至24 bytes。


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

int main(vojd){
        int arr[24];
        int j,x;
                char ch;
        do{
        printf("Enter a number:\n");
        scanf("%d",&x);
        fflush(stdin);
        do{
                        printf("Want to convert to 1.binary or 2.octal?\nPlease choose 1 or 2\n");
                        ch=getchar();
                }while(ch !='1' && ch!='2');
               
                if(ch=='1'){
               
        for(j=0;j<24;j++)
        {
                {
                if(x%2==0)
                arr[23-j]=0; //arr[]的最後一元素號碼是24-1=23
                else arr[23-j]=1;
                }
                x=x/2; //x除以2之後的商數要繼續除以2,這是本演算法的重點。
               
        }
        for(j=0;j<24;j++)
        printf("%d",arr[j]);
                printf("\n");
                system("pause");
        }
                if(ch=='2')
                {
                /*
                for(j=0;j<24;j++)
                {
                        {
                        if(x%8==0)
                                        arr[23-j]=0;
                        else if(x%8==1)
                                        arr[23-j]=1;
                        else if(x%8==2)
                                        arr[23-j]=2;
                        else if(x%8==3)
                                        arr[23-j]=3;
                        else if(x%8==4)
                                        arr[23-j]=4;
                        else if(x%8==5)
                                        arr[23-j]=5;
                        else if(x%8==6)
                                        arr[23-j]=6;
                        else arr[23-j]=7;       
                        }
                          x=x/8;

                        */

                       
                       
                       for(j=0;j<24;j++)
                         arr[23-j]=x%8;
                        x=x/8;

                }               
                for(j=0;j<24;j++)
        printf("%d",arr[j]);
                printf("\n");
                system("pause");
        }
               
               
               
                printf("Do you want to continue? y/n\n");
                fflush(stdin); //清掉記憶體中的所有資料,以免下個動作被自動執行為「否」。
                ch=getche();
                printf("\n");
                }while(ch=='y');
        
        
        return 0;
}
回復 支持 反對

使用道具 舉報

 樓主| 發表於 2015-7-1 02:01:42 | 顯示全部樓層
本帖最後由 enter 於 2015-7-1 02:06 編輯

進化版,將中間的 if else 全部簡化成為:

for(j=0;j<24;j++)
  arr[23-j]=x%2;
x=x/2;

以及:

for(j=0;j<24;j++)
arr[23-j]=x%8;
x=x/8;

因為所得的餘數就是那個位置的數字,不必一個一個去列出來。
除非是十六進位的才要去轉換。
回復 支持 反對

使用道具 舉報

 樓主| 發表於 2015-7-1 17:25:24 | 顯示全部樓層
本帖最後由 enter 於 2015-7-1 17:29 編輯

轉十六進位的程式:

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

int main(void){
char arr[25]; //因裡面會有字母 A 到 F 出現,所以要用字串。
int x,j;


printf("enter a number:\n");
scanf("%d",&x);
for(j=0;j<25;j++)
        arr[j]='\0';
for(j=0;j<24;j++){
        if(x%16==0)
                arr[23-j]='0';
        else if(x%16==1)
                arr[23-j]='1';
        else if(x%16==2)
                arr[23-j]='2';
        else if(x%16==1)
                arr[23-j]='1';
        else if(x%16==3)
                arr[23-j]='3';
        else if(x%16==4)
                arr[23-j]='4';
        else if(x%16==5)
                arr[23-j]='5';
        else if(x%16==6)
                arr[23-j]='6';
        else if(x%16==7)
                arr[23-j]='7';
        else if(x%16==8)
                arr[23-j]='8';
        else if(x%16==9)
                arr[23-j]='9';
        else if(x%16==10)
                arr[23-j]='A';
        else if(x%16==11)
                arr[23-j]='B';
        else if(x%16==12)
                arr[23-j]='C';
        else if(x%16==13)
                arr[23-j]='D';
        else if(x%16==14)
                arr[23-j]='E';
        else arr[23-j]='F';
               
        x=x/16;
}

        printf("%s",arr);
               
        return 0;
}
回復 支持 反對

使用道具 舉報

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

本版積分規則

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

GMT+8, 2024-3-29 16:49 , Processed in 0.108980 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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