您的位置:首页 > 编程语言 > C语言/C++

用文件保存的学生名单

2016-12-21 19:54 344 查看
这次的试题的来源是贺利坚老师博客中的题目,传送门:http://blog.csdn.net/sxhelijian/article/details/51620224

【项目2-用文件保存的学生名单】参考解答 

  文件score.dat中保存的是若干名学生的姓名和C++课、高数和英语成绩。 

  (1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分数据成员。
//定义学生类
class Student
{
public:
//声明必要的成员函数
private:
string name;
double cpp;
double math;
double english;
double total;
static int stu_num;  //学生人数,处理为类的静态成员合适
static double total_sum; //学生总分和
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14

(2)用对象数组进行存储学生的成绩,读入成绩并计算总分;将总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中。
int main( )
{
Student stud[200],t; //stud[200]为保存数据的对象数组
string sname;
double total_avg;
int i=0;
//从文件score.dat中读入数据,保存到对象数组中

//总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11

讨论: 

  学生人数和总分的另外一种解决方法是用全局变量。但这两种信息与学生有关,是学生类的“属性”,成为学生类的数据成员合适;这两种信息由学生整体决定,用作静态数据成员合适。如不理解这样设计的理由,复习课程前面的相关内容。

我是在其基础之上增加了内容,并且没有将总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中;

好了,说一下我的思路,我是利用了向量vector这一容器来实现对信息的保存,因为我的程序设计到需要按名次的输出,所以利用algorithm头文件中的sort()函数来对vector容易中的类进行排序,排序的是以总分的高低来进行排序的。

其次,添加,删除,查询学生的信息都是利用了vector来进行操作的:添加操作,直接用vector的成员函数push_back()来实现, 但是在添加完元素后记得使用sort()来对vector进行排序,以免使得后面的操作失误

删除操作,直接对vector利用iterator迭代器进行遍历,根据学生的姓名对每一个迭代器判断,查找到后,利用vector的成员函数erase()删除该迭代器,最后说一句,此处不需要用sort()函数来进行排序的操作,因为我的vector本身是有序的,删除一个元素后不影响我的vector的次序;

查询操作,也是利用了迭代器。

好了贴一下代码:

首先是student类:

#pragma once
#include <string>
using namespace std;
class Student {
public:

Student(string _sname,double _dcpp,double _dmath,double _denglish):name(_sname),cpp(_dcpp),math(_dmath),english(_denglish),total(_dcpp+_dmath+_denglish){}
Student():Student("", 0, 0, 0){}
Student(string _sname):Student(_sname,0,0,0){}
void setName(string _sname);
void setCpp(double _dcpp);
void setMath(double _dmath);
void setEnglish(double _denglish);
string getName();
double getCpp();
double getMath();
double getEnglish();
double getTotal();
static int getStu_Num();
static double getTotal_Sum();
static void resetTotal_Sum();
static void addTotal_Sum(double d);
static void resetStu_num();
static void addStu_num();
private:
string name;
double cpp;
double math;
double english;
double total;
static int stu_num;
static double total_sum;
};
static void resetStu_num(); static void resetTotal_Sum();两个函数的主要作用就是将stu_num与total_sum两个静态成员变量置零

下面是实现student类的源代码:

#pragma once
#include"Student.h"
#include<iostream>
void Student::setName(string _sname)
{
this->name = _sname;
}

void Student::setCpp(double _dcpp)
{
this->cpp = _dcpp;
this->total = this->cpp + this->math + this->english;
}

void Student::setMath(double _dmath)
{
this->math = _dmath;
this->total = this->cpp + this->math + this->english;
}

void Student::setEnglish(double _denglish)
{
this->english = _denglish;
this->total = this->cpp + this->math + this->english;
}

int Student::getStu_Num()
{
return Student::stu_num;
}

double Student::getTotal_Sum()
{
return Student::total_sum;
}

string Student::getName()
{
return this->name;
}
double Student::getCpp()
{
return this->cpp;
}

double Student::getMath()
{
return this->math;
}

double Student::getEnglish()
{
return this->english;
}

double Student::getTotal()
{
return this->total;
}

void Student::resetTotal_Sum()
{
Student::total_sum = 0;
}
void Student::addTotal_Sum(double d)
{
Student::total_sum += d;
}

void Student::resetStu_num()
{
Student::stu_num = 0;
}
void Student::addStu_num()
{
Student::stu_num++;
}

最后的操作代码:

#include"students.h"
#include"Student.h"
#include<fstream>
#include<vector>
#include<algorithm>
int Student::stu_num = 50;
double Student::total_sum = 0;
void Init();
void addStu();
void DelStu();
void OutFile();
void InFile();
void OutputAll();
bool compare(Student a,Student b);
void Inquire();
void greaterAverage();
void pass();
void notPass();
vector<Student> stu;
int main()
{
// double total_avg;
int i = 0;
Init();
return 0;
}
void Init()
{
bool b;
while (true)
{
system("cls");
cout << "1.添加学生信息" << endl;
cout << "2.删除学生信息" << endl;
cout << "3.查询学生信息" << endl;
cout << "4.输出学生成绩排序" << endl;
cout << "5.总分高于平均总分且没挂科的同学的信息" << endl;
cout << "6.没挂科的同学的信息" << endl;
cout << "7.挂了科的同学的信息" << endl;
cout << "8.退出" << endl;
int select;
b = true;
cin >> select;
InFile();
system("cls");
switch (select)
{
case 1:
addStu();
break;
case 2:
DelStu();
break;
case 3:
Inquire();
break;
case 4:
OutputAll();
break;
case 5:
greaterAverage();
break;
case 6:
pass();
break;
case 7:
notPass();
break;
case 8:
cout << "感谢使用!" << endl;
exit(1);
default:
cout << "输入的选项错误,请重新输入!" << endl;
}
}
}
void OutFile() //将容器stu的学生信息输出到文件中
{
fstream outfile;
outfile.open("score.dat", ofstream::out);
if (!outfile)
{
cout << "文件创建失败!" << endl;
exit(1);
}
vector<Student>::iterator it = stu.begin();
while (it != stu.end())
{
outfile << it->getName() << " " << it->getCpp() << " " << it->getMath() << " " << it->getEnglish() << endl;
it++;
}
}
void InFile() //得到文件的学生信息
{
string name,tname = " ";
double cpp;
double math;
double english;
fstream infile;
infile.open("score.dat", ifstream::in);
if (!infile)
{
cout << "score.dat文件未打开成功!" << endl;
exit(1);
}
stu.clear();

while (!infile.eof())
{
string s;
//getline(infile,s);
//cout << s;
infile >> name >> cpp >> math >> english ;
if (name == tname)
break;
//cout << name << " " << cpp << " " << math << " " << english << endl;
stu.push_back(Student(name,cpp,math,english));
getline(infile, s);
tname = name;
}
sort(stu.begin(),stu.end(),compare);
}

void addStu() //增加学生信息
{
string name;
double cpp;
double math;
double english;
while (true) {
cout << "请输入学生信息(中间以空格隔开:name cpp math english,退出按q):" << endl;
cin >> name;
if (name == "q")
break;
cin >> cpp >> math >> english;
vector<Student>::iterator it = stu.begin();
while (it != stu.end())
{
while (it->getName() == name)
{
cout << "输入的学生姓名信息错误,请重新输入:" << endl;
cin >> name;
}
it++;
}
while (cpp > 100 || cpp < 0) {
cout << "输入的学生C++成绩错误,请重新输入:" << endl;
cin >> cpp;
}
while (math > 100 || math < 0) {
cout << "输入的学生数学成绩错误,请重新输入:" << endl;
cin >> math;
}
while (english > 100 || english < 0) {
cout << "输入的学生英语成绩错误,请重新输入:" << endl;
cin >> english;
}

stu.push_back(Student(name,cpp,math,english));
}
sort(stu.begin(), stu.end(), compare);
OutFile();
}

void DelStu() //删除学生信息
{
bool b = true;
string name;
while (b)
{
cout << "请输入删除的学生的名字(输入q退出)" << endl;
cin >> name;
if (name == "q")
b = false;
vector<Student>::iterator it = stu.begin();
while (it != stu.end())
{
if (it->getName() == name)
{
stu.erase(it);
cout << "删除成功!!!\n按任意键继续..." << endl;
getchar(); getchar();
break;
}
it++;
}
}
OutFile();
}

void Inquire() //查询
{
unsigned int i;
string name;
while (true)
{
cout << "请输入需要被查询学生的姓名(输入q退出):" << endl;
cin >> name;
if (name=="q")
break;
vector<Student>::iterator it = stu.begin();
i = 1;
while (it != stu.end())
{
if (it->getName() == name)
break;
i++;
it++;
}
if (i <= stu.size()) {
cout << "您查询的结果如下:" << endl;
cout << "第 " << i << " 名 : " << it->getName() << " " << it->getCpp() << " " << it->getMath() << " " << it->getEnglish() << " " << it->getTotal() << endl;
}
else {
cout << "未查询到此人!" << endl;
}
cout << "按任意键继续..." << endl;
getchar(); getchar();
}
}

void OutputAll() //输出所有学生信息
{
int i = 1;
vector<Student>::iterator it = stu.begin();
while (it != stu.cend())
{
cout << "第" << i++ << "名 : " << it->getName() << " " << it->getCpp() << " " << it->getMath() << " " << it->getEnglish() << " " << it->getTotal() << endl;
it++;
}
cout << "按任意键继续..." << endl;
getchar(); getchar();
}

void greaterAverage()
{
int i = 1;
double average;
vector<Student>::iterator it1, it2;
it1 = it2 = stu.begin();
Student::resetTotal_Sum();
Student::resetStu_num();
while (it1 != stu.end())
{
Student::addTotal_Sum(it1->getTotal());
it1++;
Student::addStu_num();
}
//cout << Student::getTotal_Sum() << " " << Student::getStu_Num() << endl;
it1 = stu.begin();
average = Student::getTotal_Sum() / Student::getStu_Num();
//cout << average << endl;
while (it1 != stu.end())
{
if (it1->getTotal()<average)
break;
it1++;
}

while (it2 != it1)
{
cout << "第 " << i++ << " 名 : " << it2->getName() << " " << it2->getCpp() << " " << it2->getMath() << " " << it2->getEnglish() << " " << it2->getTotal() << endl;
it2++;
}
cout << "请按任意键继续..." << endl;
getchar();
getchar();
}

void pass()
{
int i = 1;
vector<Student>::iterator it1;
it1 = stu.begin();
while (it1 != stu.end())
{
if (it1->getCpp() >= 60 && it1->getEnglish() >= 60 && it1->getMath() >= 60)
cout << "第 " << i << " 名 : " << it1->getName() << " " << it1->getCpp() << " " << it1->getMath() << " " << it1->getEnglish() << " " << it1->getTotal() << endl;
it1++;
i++;
}
cout << "请按任意键继续..." << endl;
getchar(); getchar();
}

void notPass()
{
int i = 1;
vector<Student>::iterator it1 = stu.begin();
while (it1 != stu.end())
{
if (it1->getCpp() < 60 || it1->getEnglish() < 60 || it1->getMath() < 60)
break;
it1++;
i++;
}
while (it1 != stu.end())
{
cout << "第 " << i++ << " 名 : " << it1->getName() << " " << it1->getCpp() << " " << it1->getMath() << " " << it1->getEnglish() << " " << it1->getTotal() << endl;
it1++;
}
cout << "请按任意键继续..." << endl;
getchar();
getchar();
}

bool compare(Student a, Student b)
{
return a.getTotal() > b.getTotal();
}代码稍微写的有点乱哈,凑活着看

本次的对文件的读写部分,在从文件中读取信息时,会遇见一个问题,那就是最后一行的信息会被重复读取两次
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++