Danh mục

Algorithms and Data Structures in C part 4

Số trang: 5      Loại file: pdf      Dung lượng: 131.17 KB      Lượt xem: 10      Lượt tải: 0    
Hoai.2512

Hỗ trợ phí lưu trữ khi tải xuống: miễn phí Tải xuống file đầy đủ (5 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

To test out the derivation for calculating the 2’s complement of a number derived in Section 1.1.3 a program to calculate the negative of a number is shown in Code List 1.7.
Nội dung trích xuất từ tài liệu:
Algorithms and Data Structures in C part 4 1 0 0 1 1 0 1 1 1 0 1 0To test out the derivation for calculating the 2’s complement of a number derived in Section1.1.3 a program to calculate the negative of a number is shown in Code List 1.7. The output ofthe program is shown in Code List 1.8. Problem 1.11 investigates the output of the program.Code List 1.7 Testing the Binary Operators in C++Code List 1.8 Output of Program in Code List 1.7A program demonstrating one of the most important uses of the OR operator, |, is shown in CodeList 1.9. The output of the program is shown in Code List 1.10. Figure 1.1 demonstrates thevalue of x for the program. The eight attributes are packed into one character. The character fieldcan hold 256 = 28 combinations handling all combinations of each attribute taking on the valueON or OFF. This is the most common use of the OR operators. For a more detailed exampleconsider the file operation command for opening a file. The file definitions are defined in by BORLAND C++ as shown in Table 1.7.Figure 1.1 Packing Attributes into One CharacterCode List 1.9 Bit OperatorsCode List 1.10 Output of Program in Code List 1.9 Table1.7FieldsforFileOperations inC++Source enumopen_mode{ in=0x01,//openforreading out=0x02,//openforwriting ate=0x04,//seektoeofuponoriginalopen app=0x08,//appendmode:alladditionsateof trunc=0x10,//truncatefileifalreadyexists nocreate=0x20,//openfailsiffiledoesn’texist noreplace=0x40,//openfailsiffilealreadyexists binary=0x80//binary(nottext)file };A program illustrating another use is shown in Code List 1.11. If the program executes correctlythe output file, test.dat, is created with the string, “This is a test”, placed in it. The file, test.dat, isopened for writing with ios::out and for truncation with ios::trunc. The two modes are presentedtogether to the ofstream constructor with the use of the or function.Code List 1.11 Simple File I/O Previous TableofContents Next Copyright © CRC Press LLC Algorithms and Data Structures in C++ by Alan Parker CRC Press, CRC Press LLC ISBN: 0849371716 Pub Date: 08/01/93 Previous TableofContents Next 1.2.3ExamplesThis section presents examples of IEEE 32-bit and 64-bit floating point representations.Converting 100.5 to IEEE 32-bit notation is demonstrated in Example 1.1.Determining the value of an IEEE 64-bit number is shown in Example 1.2. In many cases forproblems as in Example 1.1 the difficulty lies in the actual conversion from decimal to binary.The next section presents a simple methodology for such a conversion.1.2.4ConversionfromDecimaltoBinaryThis section presents a simple methodology to convert a decimal number, A, to its correspondingbinary representation. For the sake of simplicity, it is assumed the number satisfiesin which case we are seeking the ak such thatExample 1.1 IEEE 32-Bit FormatThe simple procedure is illustrated in Code List 1.12. The C Code performing the decimal tobinary conversion is shown in Code List 1.13. The output of the program is shown in Code List1.14. This program illustrates the use of the default value. When a variable is declared as z is bydata z, z is assigned 0.0 and precision is assigned 32. This can be seen as in the program z.prec()is never called and the output results in 32 bits of precision. The paper conversion for 0.4 isillustrated in Example 1.3.1.3 Character Formats—ASCIITo represent keyboard characters, a standard has been adopted to ensure compatibility acrossmany different machines. The most widely used standard is the ASCII (American Standard Codefor Information Interchange) character set. This set has a one byte format and is shown in Table1.8. It allows for 256 distinct characters and specifies the first 128. The lower ASCII charactersare control characters which were derived from their common use in earlier machines.Althoughthe ASCII standard is widel ...

Tài liệu được xem nhiều: