C ++ formatted input and output
1, the format defined in the class ios control flag
ios class defines a data member: format control word flag, long x_flags
X_flags state value of each bit is defined symbolic constant enumeration: common few are listed below
enum{
skipws // skip to enter a space (skip whitespace)
left // output left-justified adjustment
right justified right // output adjustment
dec // converted to decimal (in / out) (decimalism)
oct // converted to octal (in / out) (octonary number system)
hex // converted to hexadecimal (in / out) (hexadecimal)
// display showbase all output base values, octal front plus is 0, Hex is applied in front 0x: example: dec: 255, cot: 0377, hex: 0xff
showpoint decimal point and the extra zero as // print, even if not needed
uppercase // uppercase hexadecimal output
showpos // positive integer display front plus "+" (show positive sign)
scientific // scientific notation shown float
fixed // float, and wears scientific notation corresponding to the counting method using a normal display
};
2, the operator
Operator: two categories
Without parameters: the definition in the header file iostream.h
Parameters: the definition in the header file iomanip.h
(1) in iostream.h operator (for example the variable representing num)
oct // cout << oct << num; octal
cin>>oct<<num;
dec // cout << dec << num; Decimal
cin>>dec>>num;
hex // cout << hex << num; hexadecimal
ws // cin >> ws; // skip any leading whitespace characters (whitespace)
endl // cout << endl inserts a line break and refresh the stream
ends // cout << ends, the output of the null character '\ 0'
flush // cout << flush to empty flow
(2)iomanip.h
setprecision (int) // set the numerical precision (rounded) cout << setprecision (5) << num;
setw (int) // set the field width cout << setw (4) << num ;
setifosflags (long p) // Enable designated as p flag cout << setifosflags (ios :: left | ios :: dec) // Left decimal output
resettifosflags (long p) // Cancel designated as a symbol of p
other
precision () Returns the current floating-point precision value
Precision (val) set to the new val precision floating-point values, and return to the original value
setf (flags) add formatting flag flags, flags of all return the original state
flags (long p) // Enable designated as a sign of p
Example:
flags()
int inum=255; cout<<"十进制方式"<<inum<<"\t"; cout.flags(ios::oct|ios::showbase); cout<<"八进制方式"<<inum<<"\t"; cout.flags(ios::hex|ios::showbase); cout<<"十六进制方式"<<inum<<endl;
precison()
void fn (float interest,float amount) { cout<<"RMB amount="; cout.precision(2); cout<<amount<<endl; cout<<"\nthe interest="; cout.precision(4); cout<<interest<<endl; } int main () { float f1=29.41560067; float f2=12.567188; fn(f1,f2); return 0; }