實訓C++語言設計——二進、八進和十六進位 表輸出

來源:互聯網
上載者:User

二進、八進和十六進位 表輸出(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 } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.