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

来源:互联网 发布:高校大数据平台 编辑:程序博客网 时间:2024/05/28 18:42

二进、八进和十六进制 表输出(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 } 

原创粉丝点击