C++文件操作初体验

文件中english.dat中给出的是2012级10000名新生入学英语分级考试全校同学的成绩                                                                           练习1:从文件中读出学生的成绩,输出最高、最低,以及平均成绩(平均成绩为小数)。
练习2:从文件中读出学生的成绩,统计90分以上学生的人数并输出。
练习3:从文件中读出学生的成绩,将超过90分的成绩输出/写入到文件good.dat中。
练习4:编程序,求出这次考试的平均成绩,并统计各分数段的人数(优秀:≥90,良好:≥80,中等:≥70,及格:≥60,不及格:<60)。
/* 
 * Copyright (c) 2013, 烟台大学计算机学院 
* All rights reserved. 
* 作    者:高古尊 
* 完成日期:2013 年 12 月1  日 
* 版 本 号:v1.0 
* 
* 输入描述: 
* 问题描述: 
* 程序输出: 
* 问题分析: 
* 算法设计: 
*/  
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    int i;
    double a[10000],max=0,min=100,sum=0;
    ifstream infile("english.dat",ios::in);
    if(!infile)
        cerr<<"open error!"<<endl;
    for(i=0; i<100; i++)
    {
        infile>>a[i];
        sum+=a[i];
        max=(max>a[i])?max:a[i];
        min=(min<a[i])?min:a[i];
    }
    infile.close();
    cout<<"平均成绩:"<<sum/100<<endl;
    cout<<"最高分:"<<max<<endl;
    cout<<"最低分:"<<min<<endl;
    ofstream outfile("good.dat",ios::out);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
    }

    int n=0,l=0,e=0;
    for(i=0; i<100; i++)
    {
        if(a[i]>=90)
        {
            n++;
            outfile<<a[i]<<endl;
        }
        else
        {
            if(a[i]<60)
                l++;
            else
                e++;
        }
    }
    outfile.close();
    cout<<"优秀人数:"<<n<<endl;
    cout<<"良好人数:"<<e<<endl;
    cout<<"不及格人数:"<<l<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值