鱼C论坛

 找回密码
 立即注册
查看: 2184|回复: 4

怎么输入一个二进制数?

[复制链接]
发表于 2019-11-23 22:48:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
2.从键盘输入-个二进制非负整数,屏幕上打印输出对应的十进制、八进制和十六进制数,要
求输出的十六进制数中的英文字母为大写字母。
示例输入、输出如下:。
Input a binary number:
100110101101
The number is 2477 in decimal.
The number is 4655 in octa1.
The number is 9AD in hexadecima
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-11-23 22:57:28 | 显示全部楼层
不知你需要用什么语言,我用python写一个。
  1. temp = input('Input a binary number:\n')
  2. num = int(temp, base = 2)
  3. print('The number is %d in decimal.'%num)
  4. print('The number is %o in octa1.'%num)
  5. print('The number is %X in hexadecima.'%num)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-23 23:48:39 | 显示全部楼层
本帖最后由 jackz007 于 2019-11-24 01:01 编辑
  1. #include <stdio.h>

  2. main(void)
  3. {
  4.         int c , d , k , m                                                                  ;
  5.         printf("Input a binary number:\n")                                                 ;
  6.         for(d = 0 ; (c = getchar()) != '\n';) if(c == '0' || c == '1') d = d * 2 + c - '0' ;
  7.         printf("The number is %d in decimal.\n" , d)                                       ;
  8.         printf("The number is %o in octal.\n" , d)                                         ;
  9.         printf("The number is %X in hexadecimal.\n" , d)                                   ;
  10. }
复制代码

        编译、运行实况:
  1. C:\Bin>g++ -o x x.c

  2. C:\Bin>x
  3. Input a binary number:
  4. 100110101101
  5. The number is 2477 in decimal.
  6. The number is 4655 in octal.
  7. The number is 9AD in hexadecimal.

  8. C:\Bin>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-25 23:38:34 From FishC Mobile | 显示全部楼层
冬雪雪冬 发表于 2019-11-23 22:57
不知你需要用什么语言,我用python写一个。

谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-25 23:39:40 From FishC Mobile | 显示全部楼层
jackz007 发表于 2019-11-23 23:48
编译、运行实况:

谢谢(*°°)=3
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 20:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表