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

来源:互联网 发布:java web三大框架 书籍 编辑:程序博客网 时间:2024/05/16 19:06

问题及代码:

/** Copyright (c) 2014, 烟台大学计算机学院* All rights reserved.* 文件名称:Project4.cpp* 作    者:chenqin.* 完成日期:2014年11月25日* 版 本 号:v1.0** 问题描述:编程求出这次考试的最高成绩,以及得最高成绩的学生的学号(设学号即是相应数组元素的下标)* 程序输入:* 程序输出:输出考试的最高成绩以及该学生的学号*/#include <fstream>   //操作文件必写#include<iostream>#include<cstdlib>   //调用exit(1)需要包含cstdlibusing namespace std;int main( ){    int grade[10000],max=-1;    int i,count=0;    ifstream infile("english.dat",ios::in);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>grade[count])    {        if(grade[count]>max)            max=grade[count];        count++;    }    infile.close();    cout<<"最高分为:"<<max<<endl;    cout<<"得最高分的同学的学号为:"<<endl;    for(i=0; i<count; i++)        if(grade[i]==max)            cout<<i<<endl;    return 0;}

运行结果:

知识点总结:要注意只有正确读入了文件中的数据,输出的内容才是所想要的数据

学习心得:现学现用,发现文件操作真的很不错

0 0
原创粉丝点击