17格式化输出 1.用setw操作符为输出数据项指定宽度(仅对一项有效) int intvalue=3928 float floatvalue=91.5 char cstring Value[="Confucius mo-tse; cout <<"C<< setw(5)<< intValue <<")<<endl cout <<C<< setw(8)<<floatvalue < )<<endl cout <<"C<< setw(20)<< cString Value < )<<endl i 见1-10.cpp
12 1.7 格式化输出 1. 用setw操作符为输出数据项指定宽度(仅对一项有效): int intValue = 3928 ; float floatValue = 91.5 ; char cStringValue[ ] = " Confucius & Mo-tse" ; cout << "(" << setw(5) << intValue << ")" << endl ; cout << "(" << setw(8) << floatValue << ")" << endl ; cout << "(" << setw(20) << cStringValue << ")" << endl ; 见1-10.cpp
17格式化输出 2.用 I setprecision操作符输出数的有效位数对多项有效) float quotient, number=132.364f, number2=2691f quotient =number/number2 cout < quotient < endl cout < setprecision(5)<< quotient < endl cout < setprecision(4)<< quotient < end cout < setprecision 3)<<quotient < endl cout < quotient < endl cout < setprecision (2)<< quotient < end cout < setprecision (1)<< quotient < end 见1-1cp
13 1.7 格式化输出 2. 用setprecision操作符输出数的有效位数(对多项有效) 。 float quotient , number1 = 132.364f , number2 = 26.91f ; quotient = number1 / number2 ; cout << quotient << endl ; cout << setprecision (5) << quotient << endl ; cout << setprecision (4) << quotient << endl ; cout << setprecision (3) << quotient << endl ; cout << quotient << endl ; cout << setprecision (2) << quotient << endl ; cout << setprecision (1) << quotient << endl ; 见1-11.cpp
1.7.2采用函数成员实现格式化输出 float davl day2. total cout<<"输入第1天和第2天的销售量:"; cin >> dayl > day2 total=dayl day2 【例113】使用| cout. precision((2 函数成员实现格 cout. setf(ios: fixed ios: showpoint) cout<<"第1天 式化输出。 cout width ( 8); cout<< dayl < end cout<<"第2天 cout width(8) 见1-13.cpp cout < day2 < endl; cout<<"总和:"<setw(8)< total<<endl
14 1.7.2 采用函数成员实现格式化输出 float day1, day2, total ; cout << "输入第1天和第2天的销售量:" ; cin >> day1 >> day2 ; total = day1 + day2; cout.precision ( 2 ); cout.setf(ios::fixed | ios::showpoint); cout << "第 1 天:" ; cout.width(8); cout << day1 << endl ; cout << "第 2 天:" ; cout.width(8); cout << day2 << endl ; cout << "总和:"<< setw(8) << total << endl ; 【例1-13】使用 函数成员实现格 式化输出。 见1-13.cpp