in Uncategorized

二进制数转化为十进制数的C程序代码。

GCC下测试通过。
==========================================================================
#include<stdlib.h>
#include<stdio.h>
#include<math.h>

main(int argc,char* argv[])
{
        int flag=0,divisor=1,count=0,i,power,Dec=0;
        long int InputBin;
        int Bin[20];
        if (argc != 2)
        {
                printf("Usage:btd number (Binrary number).n");
                return 1;
        }
        InputBin=atol(argv[1]);
        do
        {

                divisor=divisor*10;
                flag=InputBin/divisor;
                count++;
        }while(flag);
        count–;
        for (i=0;i<=count;i++)
        {

                divisor=divisor/10;
                if (0==InputBin/divisor ||1==InputBin/divisor)
                {
                        Bin[i]=InputBin/divisor;
                        InputBin=InputBin%divisor;
                }
                else
                {
                        printf("Usage:btd number (Binrary number).n");
                        return 1;
                }
        }
        power=count;
        for (i=0;i<=count;i++)
        {
                Dec=Dec+Bin[i]*pow(2,power);
                power–;
        }
        printf("The decimanl number is:%dn",Dec);
        return 0;
}
==================================================================================

Write a Comment

Comment