將十進位正整數轉成二,八及十六進位數的程式
本帖最後由 enter 於 2015-8-7 16:15 編輯#include <stdio.h>
#include <stdlib.h>
int main(void){
int arr;
char str; //十六進位中會出現字母,所以要另以字串陣列代之,而且最後一位要是'\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=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);
printf("\n");
//system("pause");
for(j=0;j<24;j++){
arr=x%8; //餘數即為該元素的值。
x=x/8;
}
printf("The Octal for %d is: ",d);
for(j=0;j<24;j++)
printf("%d",arr);
printf("\n");
//system("pause");
x=d;
for(j=0;j<25;j++)
str='\0'; //每個元素都先給它設成'\0',主要是最後一個要是 '\0' 才能結束字串。
for(j=0;j<24;j++){ // 從倒數第二個元素開始。
if(x%16==0)
str='0';
else if(x%16==1)
str='1';
else if(x%16==2)
str='2';
else if(x%16==1)
str='1';
else if(x%16==3)
str='3';
else if(x%16==4)
str='4';
else if(x%16==5)
str='5';
else if(x%16==6)
str='6';
else if(x%16==7)
str='7';
else if(x%16==8)
str='8';
else if(x%16==9)
str='9';
else if(x%16==10)
str='A';
else if(x%16==11)
str='B';
else if(x%16==12)
str='C';
else if(x%16==13)
str='D';
else if(x%16==14)
str='E';
else str='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;
} 本帖最後由 enter 於 2015-7-1 20:08 編輯
改進版,去掉前面的0不被印出。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int arr;
char str;
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=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!=0) k=1;
if(k==1)
printf("%d",arr);
}
printf("\n");
//system("pause");
for(j=0;j<24;j++){
arr=x%8;
x=x/8;
}
printf("The Octal for %d is: ",d);
k=0;
for(j=0;j<24;j++)
{
if(arr!=0) k=1;
if(k==1)
printf("%d",arr);
}
printf("\n");
//system("pause");
x=d;
for(j=0;j<25;j++)
str='\0';
for(j=0;j<24;j++){
if(x%16==0)
str='0';
else if(x%16==1)
str='1';
else if(x%16==2)
str='2';
else if(x%16==1)
str='1';
else if(x%16==3)
str='3';
else if(x%16==4)
str='4';
else if(x%16==5)
str='5';
else if(x%16==6)
str='6';
else if(x%16==7)
str='7';
else if(x%16==8)
str='8';
else if(x%16==9)
str='9';
else if(x%16==10)
str='A';
else if(x%16==11)
str='B';
else if(x%16==12)
str='C';
else if(x%16==13)
str='D';
else if(x%16==14)
str='E';
else str='F';
x=x/16;
}
printf("The Hexadecimal for %d is: ",d);
k=0;
for(j=0;j<25;j++)
{
if(str!='0') k=1;
if(k==1)
printf("%c",str);
}
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;
}
更簡化版,少了九個 if :
#include <stdio.h>
#include <stdlib.h>
int main(void){
int arr;
char str;
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=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!=0) k=1;
if(k==1)
printf("%d",arr);
}
printf("\n");
//system("pause");
for(j=0;j<24;j++){
arr=x%8;
x=x/8;
}
printf("The Octal for %d is: ",d);
k=0;
for(j=0;j<24;j++)
{
if(arr!=0) k=1;
if(k==1)
printf("%d",arr);
}
printf("\n");
//system("pause");
x=d;
for(j=0;j<25;j++)
str='\0';
for(j=0;j<24;j++){
if(x%16<=9)
str=48+x%16; //'0'的ASCII值是48,所以用48+餘數所對應的字符就是該餘數的值。
else if(x%16==10)
str='A';
else if(x%16==11)
str='B';
else if(x%16==12)
str='C';
else if(x%16==13)
str='D';
else if(x%16==14)
str='E';
else str='F';
x=x/16;
}
printf("The Hexadecimal for %d is: ",d);
k=0;
for(j=0;j<25;j++)
{
if(str!='0') k=1;
if(k==1)
printf("%c",str);
}
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;
} 本帖最後由 enter 於 2015-8-7 16:24 編輯
更新版,將陣列arr[]和str[]全改成32bits的大小,變成arr str以合實際狀況。因為一旦有十住位負數,會用到第一個arr,但目前本程式只能適用於正數的轉換而已。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int arr;
char str;
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=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!=0) k=1;
if(k==1)
printf("%d",arr);
}
printf("\n");
//system("pause");
for(j=0;j<32;j++){
arr=x%8;
x=x/8;
}
printf("The Octal for %d is: ",d);
k=0;
for(j=0;j<32;j++)
{
if(arr!=0) k=1;
if(k==1)
printf("%d",arr);
}
printf("\n");
//system("pause");
x=d;
for(j=0;j<32;j++)
str='\0';
for(j=0;j<31;j++){
if(x%16==0)
str='0';
else if(x%16==1)
str='1';
else if(x%16==2)
str='2';
else if(x%16==1)
str='1';
else if(x%16==3)
str='3';
else if(x%16==4)
str='4';
else if(x%16==5)
str='5';
else if(x%16==6)
str='6';
else if(x%16==7)
str='7';
else if(x%16==8)
str='8';
else if(x%16==9)
str='9';
else if(x%16==10)
str='A';
else if(x%16==11)
str='B';
else if(x%16==12)
str='C';
else if(x%16==13)
str='D';
else if(x%16==14)
str='E';
else str='F';
x=x/16;
}
printf("The Hexadecimal for %d is: ",d);
k=0;
for(j=0;j<32;j++)
{
if(str!='0') k=1;
if(k==1)
printf("%c",str);
}
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;
} 本帖最後由 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(intx){
if(x!=0){
bi(x/2);
printf("%d",x%2);
}
}
int oct(int y){
if(y!=0){
oct(y/8);
printf("%d",y%8);
}
} 本帖最後由 enter 於 2015-8-7 22:09 編輯
新發現,其實最大重點竟是輸入的數值的型態,要長達 long long int 型態,則原始的寫法也可以解出很長的數字和負數。但原理仍沒想通。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int arr; //採一個整數占有 4 byte 共32個bits(0/1)的意思,但arr本身的長度可是占了128byte。
char str;
//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=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!=0) k=1;
if(k==1)
printf("%d",arr);
}
printf("\n");
//system("pause");
for(j=0;j<32;j++){
arr=x%8;
x=x/8;
}
printf("The Octal for %d is: ",d);
k=0;
for(j=0;j<32;j++)
{
if(arr!=0) k=1;
if(k==1)
printf("%d",arr);
}
printf("\n");
//system("pause");
x=d;
for(j=0;j<32;j++)
str='\0';
for(j=0;j<31;j++){
if(x%16==0)
str='0';
else if(x%16==1)
str='1';
else if(x%16==2)
str='2';
else if(x%16==1)
str='1';
else if(x%16==3)
str='3';
else if(x%16==4)
str='4';
else if(x%16==5)
str='5';
else if(x%16==6)
str='6';
else if(x%16==7)
str='7';
else if(x%16==8)
str='8';
else if(x%16==9)
str='9';
else if(x%16==10)
str='A';
else if(x%16==11)
str='B';
else if(x%16==12)
str='C';
else if(x%16==13)
str='D';
else if(x%16==14)
str='E';
else str='F';
x=x/16;
}
printf("The Hexadecimal for %d is: ",d);
k=0;
for(j=0;j<32;j++)
{
if(str!='0') k=1;
if(k==1)
printf("%c",str);
}
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;
} 本帖最後由 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;
printf("enter a number:\n");
scanf("%d",&x);
str='\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;
printf("enter a number:\n");
scanf("%d",&x);
str='\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;
}
頁:
[1]