|  | 
 
 发表于 2019-11-23 23:48:39
|
显示全部楼层 
| 本帖最后由 jackz007 于 2019-11-24 01:01 编辑 
 复制代码#include <stdio.h>
main(void)
{
        int c , d , k , m                                                                  ;
        printf("Input a binary number:\n")                                                 ;
        for(d = 0 ; (c = getchar()) != '\n';) if(c == '0' || c == '1') d = d * 2 + c - '0' ;
        printf("The number is %d in decimal.\n" , d)                                       ;
        printf("The number is %o in octal.\n" , d)                                         ;
        printf("The number is %X in hexadecimal.\n" , d)                                   ;
}
编译、运行实况:
 
 复制代码C:\Bin>g++ -o x x.c
C:\Bin>x
Input a binary number:
100110101101
The number is 2477 in decimal.
The number is 4655 in octal.
The number is 9AD in hexadecimal.
C:\Bin>
 | 
 |