第15周项目1-用二进制文件处理学生成绩
2015-06-21 16:46
309 查看
【项目1-用二进制文件处理学生成绩】
(1)定义学生类,其中包含学号、姓名、C++课、高数和英语成绩及总分数据成员,成员函数根据需要确定。
(2)读入学生的成绩,并求出总分,用对象数组进行存储。ASCII文件score.dat中保存的是100名学生的学号、姓名和C++课、高数和英语成绩。
(3)将所有数据保存到一个二进制文件binary_score.dat中,最后通过键盘输入你的信息,并写入到文件中(咱不谦虚,三科全100分,期末求好运)。
(4)为验证输出文件正确,再将binary_score.dat中的记录逐一读出到学生对象中并输出查看。
(5)用BinaryViewer命令查看二进制文件文件
运行结果:
【项目1-用二进制文件处理学生成绩】
(1)定义学生类,其中包含学号、姓名、C++课、高数和英语成绩及总分数据成员,成员函数根据需要确定。
(2)读入学生的成绩,并求出总分,用对象数组进行存储。ASCII文件score.dat中保存的是100名学生的学号、姓名和C++课、高数和英语成绩。
(3)将所有数据保存到一个二进制文件binary_score.dat中,最后通过键盘输入你的信息,并写入到文件中(咱不谦虚,三科全100分,期末求好运)。
(4)为验证输出文件正确,再将binary_score.dat中的记录逐一读出到学生对象中并输出查看。
(5)用BinaryViewer命令查看二进制文件文件
#include <iostream> #include <fstream> #include <cstring> #include <cstdlib> using namespace std; class Student { private: int num; string name; double cpp; double math; double english; double total; public: Student() {}; Student(int nu,string na,double c,double m,double e):num(nu),name(na),cpp(c),math(m),english(e) { total=c+m+e; } void setMation(int nu,string na,double c,double m,double e); int get_num() { return num; } string get_name() { return name; } double get_cpp() { return cpp; } double get_math() { return math; } double get_english() { return english; } double get_total() { return total; } void set_num(int n) { num=n; } void set_name(string na) { name=na; } void set_cpp(double c) { cpp=c; } void set_math(double m) { math=m; } void set_english(double e) { english=e; } void set_total(double t) { total=t; } friend ostream& operator<<(ostream &out,Student &s); }; void Student::setMation(int nu,string na,double c,double m,double e) { num=nu; name=na; cpp=c; math=m; english=e; total=c+m+e; } ostream& operator<<(ostream &out,Student &s) { out<<s.num<<" "<<s.name<<" "<<s.cpp<<" "<<s.math<<" "<<s.english<<" "<<s.total<<endl; return out; } int main() { Student stud[100]; int i,n; string sname; double scpp,smath,senglish; ifstream infile("score.dat",ios::in); if(!infile) { cerr<<"open error!"<<endl; exit(1); } for(i=0; i<100; i++) { infile>>n>>sname>>scpp>>smath>>senglish; stud[i].setMation(n,sname,scpp,smath,senglish); } infile.close(); ofstream outfile("binary_score.dat",ios::out|ios::binary); if(!outfile) { cerr<<"open error!"<<endl; exit(1); } for(i=0; i<100; i++) { outfile.write((char*)&stud[i],sizeof(stud[i])); } cout<<"请输入自己的信息:"; cin>>n>>sname>>scpp>>smath>>senglish; Student me(n,sname,scpp,smath,senglish); outfile.write((char*)&me,sizeof(me)); outfile.close(); Student s; ifstream infile2("binary_score.dat",ios::in|ios::binary); if(!infile2) { cerr<<"open error!"<<endl; exit(1); } while(true) { infile2.read((char*)&s,sizeof(s)); if(infile2.eof()) break; cout<<s; } infile2.close(); return 0; }
运行结果:
相关文章推荐
- mac 递归删除文件路径 find . -name ._*|xargs rm -rf
- lvs之ip-tun(ip隧道)技术的学习与实践
- Surrounded Regions
- Python的装饰器模式与面向切面编程详解
- ubuntu开启root用户登录
- ubuntu开启root用户登录
- 体系结构复习3——数据级并行
- Android解析xml文件获取数据练习
- 学生信管数据库
- 使用Spring MVC 4构建Restful服务 搭建Spring MVC 4开发环境八步走
- 人,绩效,职业道德,一个程序员的生命周期,读后感。
- iOS多线程的初步研究(八)-- dispatch队列
- leetcode | Valid Palindrome
- POJ2352:Stars(树状数组)
- iOS评分功能、APP中打开其他应用程序
- iOS评分功能、APP中打开其他应用程序
- JSON之解析通过TouchJSON\SBJSON\JSONKit\NSJSONSeriliz...
- 大约PCA算法学习总结
- java读写数据
- CentOS 6.4下Squid代理服务器的安装与配置【转】