具体题目要求:编写一个人学生和教师数据输入和显示程序。其中,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名设计成一个类Person;然后设计类Person的派生类:学生类Student和教师类Teacher;编写一个主函数,通过定义student、teacher的对象,完成相应的功能。并通过文件知识对数据进行保存。
运用文件保存时分别会有以下三种情况:
1.文件不存在。
2.文件存在但内容为空
3.文件存在且有内容
第一二种情况都好说,直接打开文件对数据进行保存。而第三种情况就需要考虑之前内容是否要保存呢?
因此需要编写一个函数来判断文件之前是否存在,存在的话是否有内容。具体代码如下:
void exit()
{
int flag;//设置一个是否删除标识位
char ch;
ifstream ifs;
ifs.open(TEXT, ios::in);//我这里的TEXT是一个宏定义,具体实现看下面
if (ifs.is_open())//如果文件存在,则打开文件
{
ifs >> ch;//先让读取文件一个字符,若文件位空,则读取到文件的结尾标识符EOF
if (!ifs.eof())//文件是否已经有记录
{
cout << "文件已经有数据是否要删除之前的数据" << endl
<< "******* 1.不删除 0.删除*******" << endl
<< "请选择->:" << endl;
while (1)
{
cin >> flag;
if (flag == 0)
{
ifs.close();
delate();//删除之前文件数据的函数,具体代码见后文
break;
}
else if (flag == 1)
{
ifs.close();
break;//如果不删除直接跳出这个函数
}
else
{
cout << "输入错误,请重新输入:" << endl;
cin >> flag;
}
}
}
}
}
OK!现在要写出一个函数来对之前文件内容删除,代码如下:
void delate()
{
ofstream ofs(TEXT, ios::trunc);//删除文件内容就直接把原有文件删除重新创建一个,这不就相当于删除嘛
ofs.close();
}
现在还有一个小坑:就是你每次把数据保存在文件中是不能用ios::out,而要用ios::app,用追加的方式进行写文件,不然每次写文件时就会把上次写的内容给覆盖掉!
程序完整代码如下:
#include "iostream"
#include <string>
#include <fstream>
using namespace std;
#define TEXT "text.txt"
int m, n;
class Person
{
public:
Person() :m_num(" "), m_name(" ") {};
protected:
string m_num;
string m_name;
};
class Student:public Person
{
public:
Student() :Person(), m_class(" "), m_sorce(0) {};
~Student()
{
}
void Init(string num,string name,string clas,int sorce)
{
m_num = num;
m_name = name;
m_class = clas;
m_sorce = sorce;
}
void display()
{
cout << "编号为:" << m_num << "姓名为:" << m_name << "班级为:" << m_class << "成绩为:" << m_sorce << endl;
}
void save()
{
ofstream ofs(TEXT, ios::app);
ofs << "学生编号为:" << m_num
<< " 姓名为:" << m_name
<< " 班级为:" << m_class
<< " 成绩为:" << m_sorce << endl;
ofs.close();
}
protected:
string m_class;
int m_sorce;
};
void delate()
{
ofstream ofs(TEXT, ios::trunc);
ofs.close();
}
class Teacher :public Person
{
public:
Teacher() :Person(), m_title(" "), m_department(" ") {};
~Teacher()
{
}
void Init(string num, string name, string title, string department)
{
m_num = num;
m_name = name;
m_title = title;
m_department = department;
}
void save()
{
ofstream ofs(TEXT, ios::app);
ofs << "老师编号为:" << m_num
<< " 姓名为:" << m_name
<< " 职称为:" << m_title
<< " 部门为:" << m_department << endl;
ofs.close();
}
void display()
{
cout << "编号为:" << m_num << "姓名为:" << m_name << "职称为:" << m_title << "部门为:" << m_department << endl;
}
protected:
string m_title;
string m_department;
};
void exit()
{
int flag=2;
char ch;
ifstream ifs;
ifs.open(TEXT, ios::in);
if (ifs.is_open())//如果文件存在,则打开文件
{
ifs >> ch;
if (!ifs.eof())//文件是否已经有记录
{
cout << "文件已经有数据是否要删除之前的数据" << endl
<< "******* 1.不删除 0.删除*******" << endl
<< "请选择->:" << endl;
while (1)
{
cin >> flag;
if (flag == 0)
{
delate();
break;
}
else if (flag == 1)
break;
else
{
cout << "输入错误,请重新输入:" << endl;
cin >> flag;
}
}
}
}
}
int main()
{
int sorce;
string num, name, clas, title, department;
cout << "请分别输入学生人数和老师人数" << endl;
cin >> m >> n;
Student* student = new Student[m];
Teacher* teacher = new Teacher[n];
for (int i = 0; i < m; i++)
{
cout << "请输入第" << i + 1 << "个学生的编号:";
cin >> num;
cout << "请输入第" << i + 1 << "个学生的姓名:";
cin >> name;
cout << "请输入第" << i + 1 << "个学生的班级:";
cin >> clas;
cout << "请输入第" << i + 1 << "个学生的成绩:";
cin >> sorce;
student[i].Init(num, name, clas, sorce);
}
for (int i = 0; i < n; i++)
{
cout << "请输入第" << i + 1 << "个老师的编号:";
cin >> num;
cout << "请输入第" << i + 1 << "个老师的姓名:";
cin >> name;
cout << "请输入第" << i + 1 << "个老师的职称:";
cin >> title;
cout << "请输入第" << i + 1 << "个老师的部门:";
cin >> department;
teacher[i].Init(num, name, title, department);
}
cout << "按任意键后显示并保存在文件中" << endl;
system("pause");
exit();
for (int i = 0; i < m; i++)
{
student[i].save();
student[i].display();
}
for (int i = 0; i < n; i++)
{
teacher[i].save();
teacher[i].display();
}
ifstream ifs(TEXT, ios::in);
while (ifs >> num && ifs >> name && ifs >> clas && ifs >> sorce)
cout << num << name << clas << sorce;
ifs.close();
delete[]student;
delete[]teacher;
return 0;
}
好了,以上就是我对这次上机实验的分享!
第一次创作,有点小紧张,难免会有错误,请大家在评论区指正!谢谢大家!