(一)输入输出
1、读取
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int a;
int i[10];
memset(i,0,sizeof(i));
while(scanf("%d",&a) != EOF){
i[0]++;
}
cout<<"(1) "<<i[0]<<endl;
while(cin>>a){
i[1]++;
}
cout<<"(2) "<<i[1]<<endl;
while(scanf("%d",&a)!=-1){
i[3]++;
}
cout<<"(3) "<<i[3]<<endl;
return 0;
}
结果:
2、输出
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// left right fixed scientific setprecision setfill 具有后效性
// setw(10) 不具有后效性
// fixed和setprecision的作用还在。只要你设置了,会一直在(除非你把fixed清除,setprecision()修改)
//%md m位右对齐输出,高位补空格
//%0md m位右对齐输出,高位补0
double n;
cin>>n;
cout<<n<<endl; // 默认以6精度,所以输出为 12.3457
// 可以这样认为,默认省去了setprecision(6)这句代码
cout << setprecision(4) << n << endl; // 改成4精度,所以输出为12.35
cout << "-------" << endl;
cout << fixed << setprecision(4) << n << endl;
// 加了fixed意味着是固定点方式显示,所以这里精度指的是小数位,输出为12.3457
cout << n << endl;
// fixed和setprecision的作用还在,依然显示12.3457
cout.unsetf( ios::fixed );
// 去掉了fixed,所以精度恢复成整个数值的有效位数,显示为12.35 setprecision(4)的作用仍存在
cout << n << endl;
// 恢复成原来的样子,输出为12.3457
cout << "-------" << endl;
cout << setprecision(6) << n << endl;
cout << scientific << n << endl; // scientific与 fixed 类似 一个是科学计数,一个是小数形式
cout.unsetf( ios::scientific);
cout << n << endl;
cout << "-------" << endl;
// setfill必须写在开头
cout << setfill('*')<< right << setw(10) << n << endl;
cout << setfill('#')<< setw(10) << n << endl;
// setw(10) 不具有后效性 所以需要在每个n前加上
cout << setfill('*')<< left << n << endl;
cout << setfill('#')<< setw(10) << n << endl;
cout<<"-------"<<endl;
int m;
cin>>m;
printf("%6d\n",m); //%md m位右对齐输出,高位补空格
printf("%06d\n",m);//%0md m位右对齐输出,高位补0
return 0;
}
输出结果