十七周任务一学生文件处理,保存为二进制文件
2012-06-11 19:24
417 查看
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称: 学生文件处理,保存为二进制文件 * 作 者: 石丽君 * 完成日期: 2012 年 6 月 11 日 * 版 本 号: * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: * 程序输出: * 程序头部的注释结束 */ #include<iostream> #include<fstream> #include<string> using namespace std; class Student { private: string name; int C; int Math; int English; int All_score; double Average; public: Student(); Student(int c,int math,int english,int all_score,double average):C(c),Math(math),English(english),All_score(all_score),Average(average){} friend void arrange_all_score(Student stu[]); //排列总成绩 friend void get_frome_file(Student stu[]); //从文件中读入成绩 friend void save_to_file(Student stu[]); //保存到文件中 friend void get_heigh_allscore(Student stu[]); //得到最高分 friend void get_height_Cscore(Student stu[]); //得到最高c++成绩 friend void get_height_Mathscore(Student stu[]); //得到最高高数成绩 friend void get_height_Englishscore(Student stu[]);//得到最高英语成绩 friend void getagain_fome_file(Student stu[]); //再次从文件中读出 }; Student::Student() { C=0; Math=0; English=0; All_score=0; Average=0; } void arrange_all_score(Student stu[]) { int i,j; Student t; for( j=0;j<100;j++) { for( i=0;i<100-j;i++) if(stu[i].All_score<stu[i+1].All_score) { t=stu[i+1]; stu[i+1]=stu[i]; stu[i]=t; } } } void get_heigh_allscore(Student stu[]) { Student s; int i=0; s.All_score=stu[i].All_score; for(i=0;i<101;i++) { if(stu[i].All_score>s.All_score) { s.All_score =stu[i].All_score ; //s.name =stu[i].name ; } } cout<<"总分成绩最高为:"<<s.All_score<<'\t'<<"成员:"; for(i=0;i<101;i++) if(s.All_score ==stu[i].All_score ) cout<<stu[i].name<<" "; } void get_height_Cscore(Student stu[]) { Student s; int i=0; s.C=stu[i].C; for(i=0;i<101;i++) { if(stu[i].C>s.C) { s.C =stu[i].C; s.name =stu[i].name ; } } cout<<"C++成绩最高为:"<<s.C<<'\t'<<"成员:"; for(i=0;i<101;i++) if(s.C ==stu[i].C ) cout<<stu[i].name<<" "; } void get_height_Mathscore(Student stu[]) { Student s; int i=0; s.Math=stu[i].Math; for(i=0;i<101;i++) { if(stu[i].Math>s.Math) { s.Math =stu[i].Math; s.name =stu[i].name ; } } cout<<"高数成绩最高为:"<<s.Math<<'\t'<<"成员:"; for(i=0;i<101;i++) if(s.Math==stu[i].Math ) cout<<stu[i].name<<" "; } void get_height_Englishscore(Student stu[]) { Student s; int i=0; s.English=stu[i].English; for(i=0;i<101;i++) { if(stu[i].English>s.English) { s.English =stu[i].English; s.name =stu[i].name ; } } cout<<"英语成绩最高为:"<<s.English<<'\t'<<"成员:"; for(i=0;i<101;i++) if(s.English==stu[i].English ) cout<<stu[i].name<<" "; } void get_frome_file(Student stu[]) { ifstream infile("score.dat",ios::in); if(!infile) { cerr<<"open score.dat error!"<<endl; exit(1); } for(int i=0;i<100;i++) { infile>>stu[i].name>>stu[i].C>>stu[i].Math>>stu[i].English; stu[i].Average=(stu[i].C+stu[i].Math+stu[i].English)/3; stu[i].All_score=stu[i].C+stu[i].Math+stu[i].English; } stu[100].name="石丽君"; stu[100].C=100; stu[100].Math=100; stu[100].English=100; stu[100].Average =100; stu[100].All_score =300; infile.close(); } void save_to_file(Student stu[]) { ofstream outfile("binary_score.dat",ios::binary); if (!outfile) { cerr<<"open error!"<<endl; } for(int i=0;i<101;i++) { outfile.write((char*)&stu[i],sizeof(stu[i])); } outfile.close(); } void getagain_fome_file(Student stu[]) { ifstream infile("binary_score.dat",ios::binary); if(!infile) { cerr<<"open score.dat error!"<<endl; exit(1); } cout<<"姓名"<<'\t'<<"C++"<<'\t'<<"高数"<<'\t'<<"英语"<<'\t'<<"平均分"<<'\t'<<"总分"<<endl; for(int i=0;i<101;i++) { infile.read((char*)&stu[i],sizeof(stu[i])); cout<<stu[i].name<<'\t'<<stu[i].C<<'\t'<<stu[i].Math<<'\t'<<stu[i].English<<'\t'<<stu[i].Average<<'\t'<<stu[i].All_score<<endl; } infile.close(); } int main() { Student stu[101]; get_frome_file(stu); //读入原始文件 arrange_all_score( stu); get_heigh_allscore(stu); cout<<endl; get_height_Cscore( stu); cout<<endl; get_height_Mathscore( stu); cout<<endl; get_height_Englishscore( stu); cout<<endl; cout<<endl; save_to_file(stu); //保存到文件中 cout<<endl; getagain_fome_file(stu); system("pause"); return 0; }
感言:还是老师厉害!!!!!!
相关文章推荐
- 《第十七周实验报告任务1——学生成绩处理:保存为二进制文件》
- 第十六周任务二(学生分数文件处理)
- 第十七周实验--任务1--学生成绩处理:保存为二进制文件
- 十六周任务二 学生成绩排序文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩,将成绩排序后保存到ordered_score.dat文件中
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序, 成绩, 年龄
- C++ 十七周任务-有相同的数????
- c语言结构体保存并输出学生信息
- 第16周-任务2-学生成绩排序
- Mac下定时任务编辑 crontab -e 总是不能保存或者修改,
- 第60篇一对多之学生端私有白板图片保存及学生传数组过去不再单张传周三
- 第十六周项目三 用文件保存学生的名单 ( 不用函数重载 历经千辛万苦终于出来了)
- 飛飛(七十五)文件score.dat中保存的是若干名学生的姓名和C++课、高数和英语成绩
- 用文件保存学生信息
- 十七周任务
- 学生管理系统----文件和数据库(oracle)两种保存数据方式
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 第15周 项目二 用文件保存的学生名单
- Windows 7 的计划任务程序库保存在哪里?
- 第十四周项目2:用文件保存的学生名单
- Python备份文件、文件版本的学生管理系统如何实现(将数据保存在txt文件中)