c++的输入和输出
实验目的和要求
- 熟悉流及流类库的作用。
- 熟悉掌握流类库中常用的类及其成员函数的使用方法。
- 掌握重载“<<”和“>>”的方法。
- 掌握控制输出宽度的函数width和setw。
实验内容:
- 编程对10个浮点数排序,排序后用浮点格式显示。
代码
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
float a[10];
int i,j;
cout<<"Please input 10 float numbers: "<<endl;
for(i=0;i<10;i++)
{
cin>>a[i];
}
for(i=0;i<10;i++)
{
for(j=0;j<9-i;j++)
{
if(a[j]>a[j+1])
{
float t;
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
cout<<"The sorted numbers are: "<<endl;
for(i=0;i<10;i++)
{
cout<<a[i];
cout<<endl;
}
cout<<"\n"<<endl;
system("pause");
return 0;
}