進制轉換:今天學進制轉換的方法,其實很簡單,今天也忙,隨便做了一下,不懂請留言,大牛繞路走
#include<stdio.h>
void decimalismToBinary(int num);
void decimalismToOther()
{
int num = 0;
printf("please input a decimalism number: ");
scanf("%d",&num);//以10進制輸入,當然可以使其他進制(scanf("%o",&num))
printf("the hex number is: %x \n",num);//16進制
printf("the octal number is: %o \n",num);//8進制
printf("the nosigned number is: %u \n",num);//無符號整數
printf("**********加入確定的數*******\n");
//加入確定的數 www.2cto.com
printf("the exponent number is: %e \n",1000.0);
printf("the float number is: %-10.1f\n",100.0f);//“-”為左對齊輸出,默認右對齊
printf("********* input a number to binary ***************\n");
scanf("%d",&num);
decimalismToBinary(num);
}
//10進制轉2進制
void decimalismToBinary(int num)
{
int binary[32] = {0};//存儲2進制數
for(int i=0;i<32;i++)
{
binary[i] = num % 2;
num = num / 2;
}
//輸出
printf(" the binary of the number is: ");
for(int j=31;j>=0;j--)
{
printf("%d",binary[j]);
if(j%4==0)//4位空一格
{
printf(" ");
}
}
printf("\n");
}