实训C++语言设计——二进、八进和十六进制 表输出

二进、八进和十六进制 表输出(1-256) prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the

range 1
through 256
2 // The oct, hex, and dec identifiers are stream manipulators
3 // like endl that are defined in Chapter 11. The manipulator
4 // oct causes integers to be output in octal, the manipulator
5 // hex causes integers to be output in hexadecimal, and the manipulator
6 // dec causes integers to be output in decimal.
7 #include <iostream>
89
using std::cout;
10 using std::endl;
11 using std::oct;
12 using std::hex;
13 using std::dec;
14
15 int main()
16 {
17 cout << "Decimal/t/tBinary/t/t/tOctal/tHexadecimal/n";
18
19 for ( int loop = 1; loop <= 256; ++loop ) {
20 cout << dec << loop << "/t/t";
21
22 // Output binary number
23 int number = loop;
24 cout << ( number == 256 ? '1' : '0' );
25 int factor = 256;
26
27 do {
28 cout << ( number < factor && number >= ( factor / 2 ) ? '1' : '0' );
29 factor /= 2;
30 number %= factor;
31 } while ( factor > 2 );
32
33 // Output octal and hexadecimal numbers
34 cout << '/t' << oct << loop << '/t' << hex << loop << endl;
35 }
36
37 return 0;
38 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值