从文件读取成绩

从文件中读出学生的成绩,输出最高、最低,以及平均成绩

#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;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值