格式化输出1.7输出/输入流对象:cout/cin:例如:cout<<"I like C++"其中,<<是流插入操作符int x;cin >> X;其中,>>是流提取操作符
11 1.7 格式化输出 • cout/cin:输出/输入流对象; 例如: cout << " I like C++" ; 其中,<< 是流插入操作符 int x; cin >> x; 其中,>> 是流提取操作符
【例1.9】程序举例:#include<iostream>using namespace std:int mainOnum3=837.intnuml=2897.num2=5,num4=34,num5=7.num6=1623.num7=390,num8=3456,num9=12;//显示第一行数cout<<numl<<”";//numl后面空了3个空格cout<<num2<<"cout<<num3<<endl;显示第二行数cout<<num4<<"cout<<num5<<"cout<<num6<<endl;1/显示第三行数cout<<num7<<"cout<<num8<<"";cout<<num9<<endl ;return O;
【例1.9】程序举例: #include <iostream> using namespace std; int main( ) { int num1 = 2897, num2 = 5, num3 = 837, num4 = 34, num5 = 7, num6 = 1623, num7 = 390, num8 = 3456, num9 = 12 ; // 显示第一行数 cout << num1 << " " ; // num1后面空了3个空格 cout << num2 << " " ; cout << num3 << endl ; // 显示第二行数 cout << num4 << " " ; cout << num5 << " " ; cout << num6 << endl ; // 显示第三行数 cout << num7 << " " ; cout << num8 <<" " ; cout << num9 << endl ; return 0; }
格式化输出1.71.用setw操作符为输出数据项指定宽度(仅对一项有效):回顾:C语言中的格式intintValue=3928;化输出方法是什么?float floatValue=91.5;char cStringValuell="Confucius & Mo-tse";cout<<"("<< setw(5)<< intValue<<")"<<endl ;cout<<"("<<setw(8)<<floatValue<<")"<<endl;cout<<"("<<setw(20)<<cStringValue<<")"<<endl
13 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 ; 回顾:C语言中的格式 化输出方法是什么?
格式化输出1.72.用setprecision操作符输出数的有效位数(对多项有效)。float quotient,number1=132.364f,number2=26.91f:quotient= numberl /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(l)<<quotient<<endl;14
14 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 ;