第十三周项目六——体验文件操作(4-实战)

来源:互联网 发布:js修改input type属性 编辑:程序博客网 时间:2024/06/05 22:55

问题及代码:

/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作    者:陈旭 * 完成日期:2014年 11 月 24 日 * 版 本 号:v1.0 * * 问题描述:读入若干名学生的英语成绩,编程求出这次考试的平均成绩,并统计输出优秀人数和不及格人数 * 输入描述:学生英语成绩 * 程序输出:平均成绩,优秀人士和不及格人数 */#include<fstream>  //处理文件要包括头文件fstream#include<iostream>#include<cstdlib>    //调用exit(1)需要包含cstdlibusing namespace std;int main(){    int i=0,max=-1,a[100000],j;    ifstream infile("english.dat",ios::in);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>a[i])    {        if(a[i]>=max)            max=a[i];        i++;    }    infile.close();    cout <<"这次考试的最高成绩为:"<<max<<endl;    cout <<"取得最高成绩的学生学号分别为:"<<endl;    for(j=0; j<=i; j++)    {        if(a[j]==max)            cout <<j<<endl;    }    return 0;}


 

运行结果:

 

知识点总结:

    通过这个程序的编写,让我们把数组和文件操作结合使用,锻炼我们的思维,能力要求。

学习心得:

    一开始就是因为while语句中i++的位置不对导致一直输不出结果,看来真的是细节决定成败呀。

 

0 0
原创粉丝点击