十七周作业1
来源:互联网 发布:张家港法院拍卖淘宝网 编辑:程序博客网 时间:2024/04/29 21:01
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
class Student
{
public:
Student();
Student(string name, double cpp, double math, double English);
double all_score();
double ave_score();
void read_score(ifstream &in);
void write_score(ofstream &out);
void display();
private:
string name;
double score_cpp;
double score_math;
double score_English;
double score_all;
double score_average;
};
Student::Student()
{
this->name = "0000";
this->score_cpp = 0;
this->score_math = 0;
this->score_English = 0;
}
Student::Student(string name, double cpp, double math, double English)
{
this->name = name;
this->score_cpp = cpp;
this->score_math = math;
this->score_English = English;
}
void Student::display()
{
this->all_score();
this->ave_score();
cout << setiosflags(ios::left) << setw(12) << this->name << setw(8) << this->score_cpp << setw(8) << this->score_math << setw(8) << this->score_English << setw(8) << this->score_all <<setw(8)<<this->score_average<< endl;
}
double Student::all_score()
{
this->score_all = this->score_cpp + this->score_math + this->score_English;
return this->score_all;
}
double Student::ave_score()
{
this->score_average = (this->score_cpp + this->score_math + this->score_English) / 3;
return this->score_average;
}
void Student::read_score(ifstream &in)
{
in >> this->name >> this->score_cpp >> this->score_math >> this->score_English;
}
void Student::write_score(ofstream &out)
{
out << this->name << '\t' << this->score_cpp << '\t' << this->score_math << '\t' << this->score_English << endl;
}
void readfile(Student * s, int num)
{
ifstream infile("score.dat",ios::in);
if(!infile)
{
cerr << "open error!" << endl;
exit(1);
}
for(int i = 0; i < num; ++i)
{
s[i].read_score(infile);
}
infile.close();
}
void Readfile(Student * s, int num)
{
ifstream infile("binary_score.dat",ios::in|ios::binary);
if(!infile)
{
cerr << "open error!" << endl;
abort();
}
for(int i = 0; i < num; ++i)
{
infile.read((char *) & s[i], sizeof(s[i]));
}
infile.close();
}
void writefile(Student * s, int num)
{
ofstream outfile("binary_score.dat",ios::out|ios::binary);
if(!outfile)
{
cerr << "open error!" << endl;
abort();
}
for(int i = 0; i < num; ++i)
{
outfile.write((char *) & s[i], sizeof(s[i]));
}
outfile.close();
}
void Writefile(Student * s, int num)
{
ofstream outfile("binary_score2.dat",ios::out);
if(!outfile)
{
cerr << "open error!" << endl;
abort();
}
for(int i = 0; i < num; ++i)
{
s[i].write_score(outfile);
}
outfile.close();
}
int main()
{
Student stu[100], stu1[101], my_score("陈文洁", 100, 100, 100);
readfile(stu, 100);
writefile(stu, 100);
Readfile(stu1, 100);
stu1[100] = my_score;
cout << setiosflags(ios::left) << setw(12) << "姓名" << setw(8) << "C++" << setw(8) << "高数" << setw(8) << "英语" << setw(8) <<"总分" << setw(8) << "平均分" << endl;
for(int i = 0; i < 101; ++i)
{
stu1[i].display();
}
Writefile(stu1, 101);
system("pause");
return 0;
}
- 十七周作业1
- 作业 二十七 三国志
- 十七周任务1
- 十七周任务1
- 十七周实验报告1
- 十七周实验
- 十七周实验
- 十七周实验
- 十七周任务
- 十七周任务二
- 十七周 任务一
- 十七周任务一
- 十七周项目---排序
- 十七周 委派任务
- 十七周 都要学C
- 十七周 软件比拼
- 《第四十七周》
- hiho第二十七周
- 网络知识积累
- 宇宙真实形状
- ppt转换成word的几种方法
- 关于把设计时代码从运行时代码中分离出来的问题
- Linux下常用系统工具及知识点
- 十七周作业1
- eclipse代码提示配置及一些的快捷键。
- WWDC之iOS6新功能完全解析
- Linux中的几个问题
- 基于比较的排序总结-结合qsort&&std::sort分析
- String里“==”与等于“equals”
- 共享内存操作类(C#源码)
- dao
- 深入认识javascript中的eval函数