enter 發表於 2015-6-30 02:28:01

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

本帖最後由 enter 於 2015-6-30 17:14 編輯

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

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

enter 發表於 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;
      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=0; //arr[]的最後一元素號碼是24-1=23
                else arr=1;
                }
                x=x/2; //x除以2之後的商數要繼續除以2,這是本演算法的重點。
               
      }
      for(j=0;j<24;j++)
      printf("%d",arr);
                printf("\n");
                system("pause");
        }
                if(ch=='2')
                {
                /*
                for(j=0;j<24;j++)
                {
                        {
                        if(x%8==0)
                                        arr=0;
                        else if(x%8==1)
                                        arr=1;
                        else if(x%8==2)
                                        arr=2;
                        else if(x%8==3)
                                        arr=3;
                        else if(x%8==4)
                                        arr=4;
                        else if(x%8==5)
                                        arr=5;
                        else if(x%8==6)
                                        arr=6;
                        else arr=7;       
                        }
                        x=x/8;

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

enter 發表於 2015-7-1 02:01:42

本帖最後由 enter 於 2015-7-1 02:06 編輯

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

for(j=0;j<24;j++)
arr=x%2;
x=x/2;

以及:

for(j=0;j<24;j++)
arr=x%8;
x=x/8;

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

enter 發表於 2015-7-1 17:25:24

本帖最後由 enter 於 2015-7-1 17:29 編輯

轉十六進位的程式:

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

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


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

        printf("%s",arr);
               
        return 0;
}
頁: [1]
查看完整版本: 將一般數字轉成二進位的格式印出來