从文件中读出学生的成绩,输出最高、最低,以及平均成绩
#include <iostream>
#include <fstream>
#include<cstdlib> //调用exit(1)需要包含cstdlib
using namespace std;
int main()
{
double max=60,min=50,a,s=0.0;
int n=0;
ifstream infile("english.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(infile>>a)
{
if(a>max) max=a;
else if(a<min) min=a;
s+=a;
n++;
}
infile.close();
cout << "最高分为" <<max<< endl;
cout << "最低分为" <<min<< endl;
cout << "平均分为" <<s/n<< endl;
return 0;
}
统计各分数段的人数(优秀:≥90,良好:≥80,中等:≥70,及格:≥60,不及格:<60),并将统计结果保存到数据文件中
#include <iostream>
#include <fstream>
#include<cstdlib> //调用exit(1)需要包含cstdlib
using namespace std;
int main()
{
double max=60,min=50,a,s=0.0;
int n=0,A=0,B=0,C=0,D=0,E=0;
ifstream infile("english.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(infile>>a)
{
if(a>=90) A++;
else if(a>=80) B++;
else if(a>=70) C++;
else if(a>=60) D++;
else E++;
if(a>max) max=a;
else if(a<min) min=a;
s+=a;
n++;
}
infile.close();
ofstream outfile("统计结果.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile<<"共有学生"<<n<<"人"<<endl<<endl;
outfile << "最高分:" <<max<< endl<<endl;
outfile << "最低分:" <<min<< endl<<endl;
outfile << "平均分:" <<s/n<< endl<<endl;
outfile << "优秀人数:" <<A<< endl<<endl;
outfile << "良好人数:" <<B<< endl<<endl;
outfile << "中等人数:" <<C<< endl<<endl;
outfile << "及格人数:" <<D<< endl<<endl;
outfile << "不及格人数:" <<E<< endl<<endl;
outfile.close();
return 0;
}