怎么输入一个二进制数?
2.从键盘输入-个二进制非负整数,屏幕上打印输出对应的十进制、八进制和十六进制数,要求输出的十六进制数中的英文字母为大写字母。
示例输入、输出如下:。
Input a binary number:
100110101101
The number is 2477 in decimal.
The number is 4655 in octa1.
The number is 9AD in hexadecima 不知你需要用什么语言,我用python写一个。
temp = input('Input a binary number:\n')
num = int(temp, base = 2)
print('The number is %d in decimal.'%num)
print('The number is %o in octa1.'%num)
print('The number is %X in hexadecima.'%num) 本帖最后由 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> 冬雪雪冬 发表于 2019-11-23 22:57
不知你需要用什么语言,我用python写一个。
谢谢 jackz007 发表于 2019-11-23 23:48
编译、运行实况:
谢谢(*°°)=3
页:
[1]