C ++ output format

Source: Internet
Author: User
Tags setf

 

1. Use a controller to control the output format

Note:These controllers are defined in the header file iomanip, so the program should contain the header file iomanip. The following example shows how to use them,

Example 2 use a controller to control the output format,
# Include <iostream>
# Include <iomanip>// Do not forget to include this header file
Using namespace STD;
Int main ()
{Int;
Cout <"input :";
Cin>;
Cout <"Dec:" <dec <A <Endl;// Output an integer in the preceding hexadecimal format
Cout <"HEX:" // Output the integer a in hexadecimal format
Cout <"Oct:" <setbase (8) <A <Endl;// Output the integer a in octal format
Char * PT = "China ";
// PT points to the string "China"
Cout <SETW (10) <PT <Endl;// Specify the field width as 10 and the output string
Cout <setfill ('*') <SETW (10) <PT <Endl;// Specify the field width of 10. The output string is blank and filled "*"
Double Pi = 22.0/7.0;
// Calculate the PI value
Cout <setiosflags (IOs: Scientific) <setprecision (8 );// Output in exponential form, with 8 decimal places
Cout <"Pi =" <pI <Endl;// Output pI value
Cout <"Pi =" <setprecision (4) <pI <Endl ;//Change to 4 decimal places
Cout <"Pi =" <setiosflags (IOs: fixed) <pI <Endl;// Output in decimal form
Return 0 ;}

The running result is as follows::
InputA: 34 (input a value)
Dec: 34 (in decimal format)
HEX: 22 (hexadecimal)
Oct: 42 (octal form)
China (the domain width is 10)
* ***** China (the domain width is 10, and the blank space is filled)
Pi = 3.14285714e + 00 (exponential output, 8 decimal places)
Pi = 3.1429e + 00) (small in exponential form, 4 decimal places)
Pi = 3.143 (output in decimal form, with the margin still 4)

2. Use the member functions of the stream object to control the output format.

In addition to using the control operator to control the output format, you can also control the output format by calling the member functions used to control the output format in the stream object cout. Common member functions used to control the output format are shown in table 4.

The parameters in the stream member functions SETF and the control character setiosflags indicate the format status, which is specified by the format flag. The format flag is defined as an enumeration value in IOS. Therefore, when referencing these format tags, you must add the class name iOS and the domain operator ":" in front. See table 5.

Example 3 use the flow control member function to output data.
# Include <iostream>
Using namespace STD;
Int main ()
{Int A = 21;
Cout. SETF (IOs: showbase );// Set the base symbol for output
Cout <"Dec:" <A <Endl;
// Output A in decimal format by default
Cout. unsetf (IOs: Dec );
// Terminate the decimal format setting
Cout. SETF (IOs: Hex );
// Set the status of the hexadecimal output.
Cout <"HEX:" <A <Endl;
// Output A in hexadecimal format
Cout. unsetf (IOs: Hex );
// Terminate hexadecimal format settings
Cout. SETF (IOs: Oct );
// Set the status output in octal Mode
Cout <"Oct:" <A <Endl;// Output A in octal format
Cout. unsetf (IOs: Oct );
// Terminate the eight-digit output format setting
Char * PT = "China ";// PT points to the string "China"
Cout. Width (10 );// Specify the domain width to 10
Cout <PT <Endl;// Output string
Cout. Width (10 );// Specify the domain width to 10
Cout. Fill ('*');// Fill the blank '*'
Cout <PT <Endl;// Output string
Double Pi = 22.0/7.0;
// Calculate the PI value
Cout. SETF (IOs: Scientific );// Specify the scientific notation output
Cout <"Pi = ";// Output "Pi ="
Cout. Width (14 );// Specify the domain width to 14
Cout <pI <Endl;// Output "pI value
Cout. unsetf (IOs: Scientific );// Terminate the scientific notation
Cout. SETF (IOs: fixed );// Specify the output as a fixed point
Cout. Width (12 );// Specify the domain width to 12
Cout. SETF (IOs: showpos );// "+" Is displayed when a positive number is output.
Cout. SETF (IOs: Internal );
// The number character appears on the left.
Cout. Precision (6 );
// Retain 6 decimal places
Cout <pI <Endl;// Output pi. Pay attention to the position of the digit "+ ".
Return 0 ;}

The running status is as follows:
Dec: 21 (in decimal format)
HEX: oxl5 (hexadecimal format, starting with 0x)
Oct: 025 (octal format, starting with "O)
China (the domain width is 10)
* ***** China (the domain width is 10, and the blank space is filled)
Pi = ** 3.142857e + 00 (exponential output, 14 domain width, 6 decimal places by default)
* *** 3.142857 (decimal number input, precision is 6, leftmost output digit "+ ")

Note:

1. The member function width (N) and the control character SETW (n) are valid only for the first output item. If you want to output data according to the specified width N of the same field, you must call width (n) once before each output item ).

2. The output format states in Table 5 are divided into five groups, each of which can only be selected (for example, Dec, Hex, and OCT can only be selected, they are mutually exclusive). If you want to change the output format status to another state in the same group after you set the output format status using the member functions serf and the control operator setiosflags, call the member function unsetf (corresponding to the member function serf) or resetiosflags (corresponding to the control character sefiosflags) to terminate the original state. Then set other statuses.
Similarly, the call to the unsetf function of the program's last 8th rows is also indispensable. The reader may wish to give it a try on the computer.

3. When you use the serf function to set the format status, it can contain two or more format flags. Because these format flags are defined as enumeration values in the Los class, each format flag is represented by a binary character. Therefore, you can use the bitwise OR operator "I" to combine multiple format marks.

4. You can see that the control of the output format can be used either the control operator (for example, 2) or the related member functions of the cout stream (for example, 3 ), the two functions are the same. The Controller is defined in the header file mmamp. Therefore, when using the controller, it must contain the iomanip header file. The member functions of the cout stream are defined in the header file iostream. Therefore, you only need to include the header file iostream and do not need to include iomanip. Many programmers feel that it is convenient and simple to use controllers. Multiple controllers can be used continuously in a cout output statement.

5. concerning the control of the input mountain format, some details will be encountered during use, and it is impossible to cover all of them here. When you encounter problems, please refer to the special manual or test the machine to solve the problem.

6. Output characters using the stream member function put

In a program, the cout and insert operator <is used to implement the output. The cout stream has a buffer in the memory. Sometimes users have special output requirements, such as outputting only one character. In addition to the member functions used for format Control described above, the ostream class also provides the member function put for outputting a single character.For example:
Cout. Put ('A ');
The result of calling this function is a character a displayed on the screen. The parameter of the put function can be an ascii code (or an integer expression) of characters or characters ).Example: cout. Put (65 + 32 );
It also displays Character A, because 97 is the ASCII code of Character.

You can call the put function continuously in a statement. For example
Cout. Put (71), put (79). Put (79). Put (68). Put ('\ n ');
Good is displayed on the screen.

Example 4 contains a string "Basic", which must be output in reverse order.
The procedure is as follows:

# Include <iostream>
Using namespace STD;
Int main ()
{Char * A = "Basic ";// The character guide points to 'B'
For (INT I = 4; I> = 0; I --)
Cout. Put (* (a + I ));// Output from the last character
Cout. Put ('\ n ');
Return 0 ;}

Output on the screen during running:
Cisab

For example 4, you can also use the putchar function. The procedure is as follows:

# Include <iostream>// You can also use # include <stdio. h> instead of the next line.
Usmg namespace STD;
Int main ()
{Char * A = "Basic ";
For (INT I = 4; I> = 0; I --)
Putchar (* (a + I ));
Putchar ('\ n ');}

The running result is the same as that before. The member function put can be called not only by cout stream objects, but also by other stream objects in the ostream class.

Source: http://hi.baidu.com/hankcs/blog/item/9d40d70f6283e5c67bcbe184.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.