目录
系统要求
实现简介及其优点
C++代码
头文件
Student头文件
User头文件
源文件
Student源文件
User源文件
主函数源文件
结果展示
代码改进方法
本次设计主要采用类,结构体和链表结合的方式实现。结构体,作用一,因为有多个学科,例如语文,数学,英语等等,每个学科里面又包含了平时成绩,期末成绩和总成绩,十分复杂,所以利用结构体的方式以学科为单位,用struct结构体进行封装,结构体学科内部包含平时成绩,期末成绩和计算机计算的总成绩;作用二,由于系统需要账号才可以访问,所以创建一个结构体,以单个老师/管理员为单位,对注册的老师/管理员信息进行封装。类,分别定义了student类和user类,student封装学生的信息,封装了需要实现的功能,例如添加学生,删除学生,查询学生成绩等功能。类的形式使得代码整体看起来更加简洁,思路更加清晰;user类主要为了封装管理员和教师信息和创建管理员功能,如添加教师,删除教师,查询教师信息等功能。链表,为了分别记录学生,教师,管理员信息,分别创建链表,将信息链接起来,访问时,遍历即可,链表的优点在于只要是内存足够,可以无限添加学生信息,如果是采用数组的方式将定义固定创建的数量,如果想扩充,还需要利用copy函数,将数组扩容,此外,如果利用数组进行删除学生、或者教师信息时,会造成内存资源的浪费,如果将数组数据移动,则会影响效率,综上,选择了链表进行储存信息。在录入完成全部的信息后利用fstream库将信息保存到记事本中。
#ifndef Student_h
#define Student_h
#include
#include
using namespace std;
struct couse
{
string cousename;
int usual_performance; //平时成绩
int end_performance; //期末成绩
double fin_performance; //总成绩
double intfin_performance(couse &a); //计算总成绩 平时成绩30%+期末成绩70%
};
typedef struct student {
unsigned stu_id; //学号
string stu_name; //姓名
string couse; //课程名
struct couse Chinese;
struct couse math;
struct couse English;
struct couse physics;
struct couse chemistry;
struct couse biology;
student *stu_next = NULL; //射线结构链表
} student;
class Student
{
public:
Student(); //构造函数
~Student(); //析构函数
//录入学生信息
student *createByStdin(int model, student &tmp); // 从标准输入中创建学生,model = 1: 不读取学号,2:不读取姓名,其他,读取所有信息。
void logon(); // 连续录入学生信息
//查找
void Query() const; // 查询学生信息
student *findById(unsigned id) const; // 根据学号查找
student *findByName(const string &name) const; // 根据姓名查找
//删除
void deleteBy(); // 删除
bool deleteById(unsigned &id); // 根据学号删除
bool deleteByName(string &name); // 根据姓名删除
//修改
void modify(); // 修改
bool modifyById(); // 根据学号修改
bool modifyByName(); // 根据姓名修改
//显示
void Show() const; // 列出学生信息
//文件操作
void saveToFile(); // 保存到文件
void readFile(); // 读取文件
bool Student::insert(const student &astu);
unsigned headCount() const;
void findcouse(); //调出单科成绩
private:
student *head; //进入链表的指针
};
#endif
#ifndef USER_H
#define USER_H
#include
using namespace std;
typedef struct user
{
string m_name; //用户名
string m_password; //密码
user *next_user; //链表
}user;
class User
{
public:
User()
{
head = new user;
head->m_name = "admin";
head->m_password = "123";
head->next_user = NULL;
}
~User()
{
user *p = head, *q;
while (p)
{
q = p;
p = q->next_user;
delete q;
}
}
user *findByName(const string &name) const; //查找文件中的用户名
void readFile(); //读取文件
void saveFile(); //保存文件
void login(); //注册函数
bool logon(); //登陆函数
unsigned headCount(); //结构计数函数
bool insert(const user &astu); //插入函数
int deleteByName(); //删除函数
void show(); //显示函数
void findtailshowname(const string &name) const; //根据姓名,发现具体老师
void showtailshowname(); //输入姓名,查询具体老师
user* FindByname(string &name);
void data(); //导出学科数据
/*bool menutea();*/
private:
user *head;
//User *Head;
};
#endif
#include "Student.h"
#include
using namespace std;
string filename("untitled");
void show(student *p);
void Student::findcouse()
{
cout << "请输入需要导出数据的科目名称" << endl;
string name;
cin >> name;
if (name == "语文")
{
student *p;
string new_file;
cout << "请输入欲保存的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount();
file << len << endl;
student *pHead = head->stu_next;
for (p = head; p->stu_next != NULL; p = p->stu_next)
{
file << pHead->stu_name << " ";
file << "平均成绩:" << pHead->Chinese.usual_performance <<
"期末成绩:" << pHead->Chinese.end_performance << " "
<< "总成绩" << pHead->Chinese.fin_performance << endl;
}
file.close();
cout << "录入完成" << endl;
}
if (name == "数学")
{
student *p;
string new_file;
cout << "请输入欲保存的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount();
file << len << endl;
student *pHead = head->stu_next;
for (p = head; p->stu_next != NULL; p = p->stu_next)
{
file << pHead->stu_name << " ";
file << "平均成绩:" << pHead->math.usual_performance <<
"期末成绩:" << pHead->math.end_performance << " "
<< "总成绩" << pHead->math.fin_performance << endl;
}
file.close();
cout << "录入完成" << endl;
}
if (name == "英语")
{
student *p;
string new_file;
cout << "请输入欲保存的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount();
file << len << endl;
student *pHead = head->stu_next;
for (p = head; p->stu_next != NULL; p = p->stu_next)
{
file << pHead->stu_name << " ";
file << "平均成绩:" << pHead->English.usual_performance <<
"期末成绩:" << pHead->English.end_performance << " "
<< "总成绩" << pHead->English.fin_performance << endl;
}
file.close();
cout << "录入完成" << endl;
}
if (name == "物理")
{
student *p;
string new_file;
cout << "请输入欲保存的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount();
file << len << endl;
student *pHead = head->stu_next;
for (p = head; p->stu_next != NULL; p = p->stu_next)
{
file << pHead->stu_name << " ";
file << "平均成绩:" << pHead->physics.usual_performance <<
"期末成绩:" << pHead->physics.end_performance << " "
<< "总成绩" << pHead->physics.fin_performance << endl;
}
file.close();
cout << "录入完成" << endl;
}
if (name == "化学")
{
student *p;
string new_file;
cout << "请输入欲保存的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount();
file << len << endl;
student *pHead = head->stu_next;
for (p = head; p->stu_next != NULL; p = p->stu_next)
{
file << pHead->stu_name << " ";
file << "平均成绩:" << pHead->chemistry.usual_performance <<
"期末成绩:" << pHead->chemistry.end_performance << " "
<< "总成绩" << pHead->chemistry.fin_performance << endl;
}
file.close();
cout << "录入完成" << endl;
}
if (name == "生物")
{
student *p;
string new_file;
cout << "请输入欲保存的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount();
file << len << endl;
student *pHead = head->stu_next;
for (p = head; p->stu_next != NULL; p = p->stu_next)
{
file << pHead->stu_name << " ";
file << "平均成绩:" << pHead->biology.usual_performance <<
"期末成绩:" << pHead->biology.end_performance << " "
<< "总成绩" << pHead->biology.fin_performance << endl;
}
file.close();
cout << "录入完成" << endl;
}
}
double couse::intfin_performance(couse &a)
{
return (a.usual_performance*0.3 + a.end_performance*0.7);
}
Student::Student()
{
head = new student;
head->stu_id = 0;
head->stu_name = "No name";
head->stu_next = NULL;
}
Student::~Student()
{
student *p = head, *q;
while (p)
{
q = p;
p = q->stu_next;
delete q;
}
}
//注册函数
void Student::logon()
{
student tmp;
cout << "学号 (0 结束输入 ): ";
cin >> tmp.stu_id;
while (tmp.stu_id)
{
if (findById(tmp.stu_id) == NULL)
{
tmp.Chinese.cousename = "语文";
tmp.math.cousename = "数学";
tmp.math.cousename = "英语";
tmp.physics.cousename = "物理";
tmp.chemistry.cousename = "化学";
tmp.biology.cousename = "生物";
cout << "姓 名: " << endl;
cin >> tmp.stu_name;
cout << "语 文: " << endl;
cout << "平时成绩:" << endl;
cin >> tmp.Chinese.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.Chinese.end_performance;
tmp.Chinese.fin_performance = tmp.Chinese.intfin_performance(tmp.Chinese);
cout << "总成绩为:" << tmp.Chinese.fin_performance << endl;
cout << endl;
cout << "数 学: " << endl;
cout << "平时成绩:" << endl;
cin >> tmp.math.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.math.end_performance;
tmp.math.fin_performance = tmp.math.intfin_performance(tmp.math);
cout << "总成绩为:" << tmp.math.fin_performance << endl;
cout << endl;
cout << "英 语 : " << endl;
cout << "平时成绩:" << endl;
cin >> tmp.English.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.English.end_performance;
tmp.English.fin_performance = tmp.English.intfin_performance(tmp.English);
cout << "总成绩为:" << tmp.English.fin_performance << endl;
cout << endl;
cout << "物 理: " << endl;
cout << "平时成绩:" << endl;
cin >> tmp.physics.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.physics.end_performance;
tmp.physics.fin_performance = tmp.physics.intfin_performance(tmp.physics);
cout << "总成绩为:" << tmp.physics.fin_performance << endl;
cout << endl;
cout << "化 学:" << endl;
cout << "平时成绩:" << endl;
cin >> tmp.chemistry.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.chemistry.end_performance;
tmp.chemistry.fin_performance = tmp.chemistry.intfin_performance(tmp.chemistry);
cout << "总成绩为:" << tmp.chemistry.fin_performance << endl;
cout << endl;
cout << "生 物:" << endl;
cout << "平时成绩:" << endl;
cin >> tmp.biology.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.biology.end_performance;
tmp.biology.fin_performance = tmp.biology.intfin_performance(tmp.biology);
cout << "总成绩为:" << tmp.biology.fin_performance << endl;
cout << endl;
insert(tmp);
}
else cout << "重复的学号: " << tmp.stu_id << endl;
cout << "添加成功" << endl;
cout << "学号 (0 结束输入): ";
cin >> tmp.stu_id;
}
}
//查找函数
void Student::Query() const {
int select;
unsigned id;
string name;
string sex;
string email;
student *p;
cout << "1、按学号查询\n2、按姓名查询\n3、返回\n";
cin >> select;
switch (select)
{
case 1: cout << "请输入学号: "; cin >> id;
if ((p = findById(id))) show(p->stu_next);
break;
case 2: cout << "请输入姓名: "; cin >> name;
if ((p = findByName(name))) show(p->stu_next);
break;
case 3: return;
default: cout << "选择错误. \n";
}
}
student *Student::findByName(const string &name) const {
student *p;
for (p = head; p->stu_next; p = p->stu_next)
if (p->stu_next->stu_name == name) return p;
cout << "没有找到输入的姓名" << endl;
return NULL;
}
student *Student::findById(unsigned id) const
{
student *p;
for (p = head; p->stu_next; p = p->stu_next)
if (p->stu_next->stu_id == id) return p;
return NULL;
}
//删除函数
void Student::deleteBy() {
int select;
unsigned id;
string name;
cout << "1、按学号删除\n2、按姓名删除\n0、返回\n";
cin >> select;
switch (select)
{
case 1: cout << "请输入学号: "; cin >> id;
deleteById(id);
break;
case 2: cout << "请输入姓名: "; cin >> name;
deleteByName(name);
break;
case 0: return;
default: cout << "选择错误. \n";
}
}
bool Student::deleteById(unsigned &id) {
student *q, *p;
p = findById(id);
if (p == NULL)
{
cout << "没有找到学号是 \"" << id << "\" 的学生,删除失败! \n";
return false;
}
q = p->stu_next;
p->stu_next = q->stu_next;
delete q;
cout << "成功删除 " << id << " 的信息. \n";
return true;
}
bool Student::deleteByName(string &name) {
student *q, *p;
p = findByName(name);
if (p == NULL) {
cout << "没有找到姓名是 \"" << name << "\" 的学生, 删除失败! \n";
return false;
}
q = p->stu_next;
p->stu_next = q->stu_next;
delete q;
cout << "成功删除 " << name << " 的信息. \n";
return true;
}
//修改函数
void Student::modify() {
int select;
cout << "1、按学号修改\n2、按姓名修改\n0、返回\n";
cin >> select;
switch (select) {
case 1: if (modifyById()) cout << "修改成功. \n"; break;
case 2: if (modifyByName()) cout << "修改成功. \n"; break;
case 0: return;
default: cout << "选择错误. \n";
}
}
bool Student::modifyById()
{
student *p;
unsigned id;
cout << "输入要修改的学号: ";
cin >> id;
p = findById(id);
if (p == NULL)
{
cout << "没有找到学号是 \"" << id << "\" 的学生, 修改失败! \n";
return false;
}
createByStdin(1, *(p->stu_next));
return true;
}
bool Student::modifyByName()
{
student *p;
string name;
cout << "输入要修改人的姓名: ";
cin >> name;
p = findByName(name);
if (p == NULL) {
cout << "没有找到姓名是 \"" << name << "\" 的学生, 修改失败! \n";
return false;
}
createByStdin(2, *(p->stu_next));
return true;
}
student *Student::createByStdin(int model, student &tmp)
{
if (model != 1) { cout << "学 号: "; cin >> tmp.stu_id; }
if (model != 2) { cout << "姓 名: "; cin >> tmp.stu_name; }
cout << "语 文: " << endl;
cout << "平时成绩:" << endl;
cin >> tmp.Chinese.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.math.end_performance;
tmp.Chinese.fin_performance = tmp.Chinese.intfin_performance(tmp.Chinese);
cout << "总成绩为:" << tmp.Chinese.fin_performance << endl;
cout << endl;
cout << "数 学: " << endl;
cout << "平时成绩:" << endl;
cin >> tmp.math.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.math.end_performance;
tmp.math.fin_performance = tmp.math.intfin_performance(tmp.math);
cout << "总成绩为:" << tmp.math.fin_performance << endl;
cout << endl;
cout << "英 语: " << endl;
cout << "平时成绩:" << endl;
cin >> tmp.English.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.English.end_performance;
tmp.English.fin_performance = tmp.English.intfin_performance(tmp.math);
cout << "总成绩为:" << tmp.English.fin_performance << endl;
cout << endl;
cout << "物 理: " << endl;
cout << "平时成绩:" << endl;
cin >> tmp.physics.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.physics.end_performance;
tmp.physics.fin_performance = tmp.physics.intfin_performance(tmp.physics);
cout << "总成绩为:" << tmp.physics.fin_performance << endl;
cout << endl;
cout << "化 学:" << endl;
cout << "平时成绩:" << endl;
cin >> tmp.chemistry.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.chemistry.end_performance;
tmp.chemistry.fin_performance = tmp.chemistry.intfin_performance(tmp.chemistry);
cout << "总成绩为:" << tmp.chemistry.fin_performance << endl;
cout << endl;
cout << "生 物:" << endl;
cout << "平时成绩:" << endl;
cin >> tmp.biology.usual_performance;
cout << "期末成绩:" << endl;
cin >> tmp.biology.end_performance;
tmp.biology.fin_performance = tmp.biology.intfin_performance(tmp.biology);
cout << "总成绩为:" << tmp.biology.fin_performance << endl;
cout << endl;
return &tmp;
}
//文件操作函数
void Student::saveToFile()
{
string new_file;
cout << "请输入欲保存的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount();
file << len << endl;
student *pHead = head->stu_next;
while (pHead != NULL)
{
file << pHead->stu_id << endl;
file << pHead->stu_name << endl;
file << pHead->Chinese.cousename << endl;
file << pHead->Chinese.usual_performance << endl;
file << pHead->Chinese.end_performance << endl;
file << pHead->Chinese.fin_performance << endl;
file << pHead->English.cousename << endl;
file << pHead->English.usual_performance << endl;
file << pHead->English.end_performance << endl;
file << pHead->English.fin_performance << endl;
file << pHead->math.cousename << endl;
file << pHead->math.usual_performance << endl;
file << pHead->math.end_performance << endl;
file << pHead->math.fin_performance << endl;
file << pHead->physics.cousename << endl;
file << pHead->physics.usual_performance << endl;
file << pHead->physics.end_performance << endl;
file << pHead->physics.fin_performance << endl;
file << pHead->chemistry.cousename << endl;
file << pHead->chemistry.usual_performance << endl;
file << pHead->chemistry.end_performance << endl;
file << pHead->chemistry.fin_performance << endl;
file << pHead->biology.cousename << endl;
file << pHead->biology.usual_performance << endl;
file << pHead->biology.end_performance << endl;
file << pHead->biology.fin_performance << endl;
pHead = pHead->stu_next;
}
file.close();
cout << "保存成功. " << endl;
filename = new_file;
return;
}
void Student::readFile()
{
string new_file;
cout << "请输入欲读取的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::in);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = 0;
file >> len;
if (!len)
{
cout << "文件数据异常. " << endl;
return;
}
student pHead;
file.get();
while (len--)
{
file >> pHead.stu_id;
file >> pHead.stu_name;
file >> pHead.Chinese.cousename;
file >> pHead.Chinese.usual_performance;
file >> pHead.Chinese.end_performance;
file >> pHead.Chinese.fin_performance;
file >> pHead.math.cousename;
file >> pHead.math.usual_performance;
file >> pHead.math.end_performance;
file >> pHead.math.fin_performance;
file >> pHead.English.cousename;
file >> pHead.English.usual_performance;
file >> pHead.English.end_performance;
file >> pHead.English.fin_performance;
file >> pHead.physics.cousename;
file >> pHead.physics.usual_performance;
file >> pHead.physics.end_performance;
file >> pHead.physics.fin_performance;
file >> pHead.chemistry.cousename;
file >> pHead.chemistry.usual_performance;
file >> pHead.chemistry.end_performance;
file >> pHead.chemistry.fin_performance;
file >> pHead.biology.cousename;
file >> pHead.biology.usual_performance;
file >> pHead.biology.end_performance;
file >> pHead.biology.fin_performance;
insert(pHead);
}
file.close();
cout << "文件读入成功. " << endl;
filename = new_file;
return;
}
bool Student::insert(const student &astu)
{
student *newnode, *p = head;
if (p->stu_next == NULL) //当没有下一个数据,则新建一个空间来储存修改数据
{
p->stu_next = new student(astu); //用传入的已修改结构初始化
p->stu_next->stu_next = NULL; //NULL处理再下一个堆
return true;
}
while (p->stu_next) //当存在下一个堆
{
if (p->stu_next->stu_id == astu.stu_id) //判重
{
cout << "重复的学号, 插入失败! \n";
return false;
}
if (p->stu_next->stu_id > astu.stu_id) //如果下一个堆的id大于传入id
{
newnode = new student(astu); //newnode指向初始修改结构
newnode->stu_next = p->stu_next;
p->stu_next = newnode; // 移位
return true;
}
p = p->stu_next; //既不大于也不重复就后移添加
}
p->stu_next = new student(astu);
p->stu_next->stu_next = NULL;
return true;
}
//显示函数
void Student::Show() const {
student *p;
cout << "----------------------------------------------------------\n";
for (p = head->stu_next; p; p = p->stu_next)
{
show(p);
}
cout << "----------------------------------------------------------\n";
}
void show(student *p)
{
cout << " 学号:" << p->stu_id << endl;
cout << " 姓名: " << p->stu_name << endl;
cout << " 语文: 平时成绩:" << p->Chinese.usual_performance
<< " 期末成绩:" << p->Chinese.end_performance
<< " 总评成绩:" << p->Chinese.fin_performance << endl;
cout << " 英语: 平时成绩:" << p->English.usual_performance
<< " 期末成绩:" << p->English.end_performance
<< " 总评成绩:" << p->English.fin_performance << endl;
cout << " 数学: 平时成绩:" << p->math.usual_performance
<< " 期末成绩:" << p->math.end_performance
<< " 总评成绩:" << p->math.fin_performance << endl;
cout << " 物理: 平时成绩:" << p->physics.usual_performance
<< " 期末成绩:" << p->physics.end_performance
<< " 总评成绩:" << p->physics.fin_performance << endl;
cout << " 化学: 平时成绩:" << p->chemistry.usual_performance
<< " 期末成绩:" << p->chemistry.end_performance
<< " 总评成绩:" << p->chemistry.fin_performance << endl;
cout << " 生物: 平时成绩:" << p->biology.usual_performance
<< " 期末成绩:" << p->biology.end_performance
<< " 总评成绩:" << p->biology.fin_performance << endl;
}
unsigned Student::headCount() const
{
unsigned cnt = 0;
student *p;
for (p = head->stu_next; p; p = p->stu_next, ++cnt);
return cnt;
}
#include"user.h"
#include
#include
using namespace std;
void User::readFile()
{
string new_file = "user";
fstream file;
file.open(new_file.c_str(), ios::in);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = 0;
file >> len;
if (!len)
{
cout << "已新建管理员数据文件,请注册管理员账号。 " << endl;
login();
return;
}
user pHead;
file.get();
while (len--)
{
file >> pHead.m_name;
file >> pHead.m_password;
insert(pHead);
}
file.close();
cout << "请输入管理员账号以登陆. " << endl;
return;
}
void User::saveFile()
{
string new_file = "user";
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount() - 1;
file << len << endl;
user *pHead = head->next_user;
while (pHead != NULL)
{
file << pHead->m_name << endl;
file << pHead->m_password << endl;
pHead = pHead->next_user;
}
file.close();
return;
}
bool User::insert(const user &astu)
{
user *p = head;
if (p->next_user == NULL) //当没有下一个数据,则新建一个空间来储存修改数据
{
p->next_user = new user(astu); //用传入的已修改结构初始化
p->next_user->next_user = NULL;
return true;
}
while (p->next_user)
{
if (p->next_user->m_name == astu.m_name) //判重
{
cout << "重复的用户名, 插入失败! \n";
return false;
}
p = p->next_user; //既不大于也不重复就后移添加
}
p->next_user = new user(astu);
p->next_user->next_user = NULL;
return true;
}
unsigned User::headCount()
{
unsigned cnt = 0;
user *p;
for (p = head; p; p = p->next_user, ++cnt);
return cnt;
}
user* User::FindByname(string &name)
{
user *p;
for (p = head; p != NULL; p = p->next_user)
if (p->m_name == name) return p;
return NULL;
}
user *User::findByName(const string &name) const
{
user *p;
for (p = head; p->next_user != NULL; p = p->next_user)
if (p->next_user->m_name == name) return p;
return NULL;
}
int User::deleteByName()
{
user *q, *p, *w;
cout << "请输入用户名" << endl;
string name;
cin >> name;
p = findByName(name);
if (p == NULL)
{
cout << "没有找到账号 \"" << name << "\" ,删除失败! \n";
return 0;
}
q = p;
w = p->next_user;
p->next_user = w->next_user;
delete w;
cout << "成功删除 " << name << " 的账号. \n";
saveFile();
return 0;
}
void User::findtailshowname(const string &name)const
{
user *p;
for (p = head; p->next_user != NULL; p = p->next_user)
{
if (p->next_user->m_name == name)
{
cout << "账号:" << p->next_user->m_name << " 密码:" << p->next_user->m_password << endl;
}
}
}
void User::showtailshowname()
{
cout << "请输入需要查询老师的姓名" << endl;
string name;
cin >> name;
findtailshowname(name);
}
void User::show()
{
user *p;
cout << "----------------------------------------------------------\n";
for (p = head->next_user; p; p = p->next_user)
{
cout << " 用户名:" << p->m_name << " 密码: " << p->m_password << endl;
}
cout << "----------------------------------------------------------\n";
}
void User::login()
{
user tmp;
cout << "新用户名: ";
cin >> tmp.m_name;
if (findByName(tmp.m_name) == NULL)
{
cout << "新密码: ";
cin >> tmp.m_password;
insert(tmp);
}
else
{
cout << "重复的用户名: " << tmp.m_name << endl;
login();
}
saveFile();
}
bool User::logon()
{
cout << " 登录" << endl;
cout << "用户名:";
string name;
cin >> name;
user *p;
if (findByName(name))
{
p = findByName(name);
cout << "密码:";
string password;
cin >> password;
if (p->next_user->m_password == password)
{
cout << "登陆成功" << endl;
return true;
}
else
{
cout << "密码输入错误" << endl;
logon();
}
}
else
{
cout << "没有找到相关用户名!" << endl;
logon();
}
return 0;
}
#include
#include "Student.h"
#include "User.h"
using namespace std;
void Menutea()
{
cout << endl;
cout << "*********************************************" << endl;
cout << "** *学生管理教师系统* **" << endl;
cout << "** 1、读取学生成绩 **" << endl;
cout << "** 2、保存学生成绩 ** " << endl;
cout << "** 3、添加学生信息 **" << endl;
cout << "** 4、修改学生信息 **" << endl;
cout << "** 5、浏览学生成绩 **" << endl;
cout << "** 6、查询具体学生成绩 **" << endl;
cout << "** 7、删除学生成绩 **" << endl;
cout << "** 8、导出单科数据 **" << endl;
cout << "** 0、退出 **" << endl;
cout << "*********************************************" << endl;
cout << endl;
}
void usemenu()
{
cout << endl;
cout << "*********************************************" << endl;
cout << "** *学生管理管理员系统* **" << endl;
cout << "** 1、读取学生成绩 **" << endl;
cout << "** 2、保存学生成绩 ** " << endl;
cout << "** 3、添加老师账号 **" << endl;
cout << "** 4、查找教师账号 **" << endl;
cout << "** 5、删除老师账号 **" << endl;
cout << "** 6、查询具体学生成绩 **" << endl;
cout << "** 7、查询具体老师账号 **" << endl;
cout << "** 8、查询所有学生成绩 **" << endl;
cout << "** 0、退出 **" << endl;
cout << "*********************************************" << endl;
cout << endl;
}
void menu() {
cout << endl;
cout << "+-----------------------------------------------+\n";
cout << "| 学生信息管理系统 |\n";
cout << "+---------------------------------------------+\n";
cout << "| 1、添加老师账号 | 6、添加新学生信息 |\n";
cout << "| 2、查看老师账号 | 7、修改学生信息 |\n";
cout << "| 3、删除老师账号 | 8、显示学生信息 |\n";
cout << "| 4、读取学生信息 | 9、查询学生信息 |\n";
cout << "| 5、保存学生信息 | 10、删除学生信息 |\n";
cout << "+-----------------------------------------------+\n";
cout << "| 0、退出 |\n";
cout << "+-----------------------------------------------+\n";
cout << endl;
}
void showtea()
{
}
int main()
{
cout << "学生成绩管理系统" << endl;
cout << "请输入您的身份(管理员/教师)" << endl;
string name;
cin >> name;
User u1;
u1.readFile();
if (u1.logon())
{
Student m_stu;
int choice;
char str[20];
if (name == "教师")
{
cout << "欢迎来到学生成绩管理教师系统" << endl;
Menutea();
do {
cout << "请输入指令:";
cin >> choice;
while (!cin) //判断choice是否输入的是整数,如果是正确类型,cin=0
{
cin.clear(); //清空cin流
cin >> str; //赋值错误输入到str
cout << "请输入正确的指令:" << endl;
cin >> choice;
}
switch (choice) {
case 1: m_stu.readFile(); Menutea(); break;
case 2: m_stu.saveToFile(); Menutea(); break;
case 3: m_stu.logon(); Menutea(); break;
case 4: m_stu.modify(); Menutea(); break;
case 5: m_stu.Show(); Menutea(); break;
case 6: m_stu.Query(); Menutea(); break;
case 7: m_stu.deleteBy(); Menutea(); break;
case 8:m_stu.findcouse(); Menutea(); break;
case 0:; break;
default: cout << "请输入正确的选项" << endl;
menu(); break;
}
} while (choice);
return 0;
}
else if (name == "管理员")
{
cout << "欢迎来到学生成绩管理管理员系统" << endl;
usemenu();
do {
cout << "请输入指令:";
cin >> choice;
while (!cin) //判断choice是否输入的是整数,如果是正确类型,cin=0
{
cin.clear(); //清空cin流
cin >> str; //赋值错误输入到str
cout << "请输入正确的指令:" << endl;
cin >> choice;
}
switch (choice)
{
case 1: m_stu.readFile(); usemenu(); break;
case 2: m_stu.saveToFile(); usemenu(); break;
case 3: u1.login(); usemenu(); break;
case 4: u1.show(); usemenu(); break;
case 5: u1.deleteByName(); usemenu(); break;
case 6: m_stu.Query(); usemenu(); break;
case 7:u1.showtailshowname(); usemenu; break;
case 8: m_stu.Show(); usemenu; break;
case 0: break;
default: cout << "请输入正确的选项" << endl;
menu(); break;
}
} while (choice);
return 0;
}
}
}
这里展示部分运行结果
该系统基本功能已经实现,但是由于时间的原因,当时写的时候面对错误操作,没有报异常,而是直接使得程序崩溃,为了使代码更加完善,可以使用try-catch语句进行一个错误操作的异常提示,重新操作。此代码由于当时学习的知识不够完善,实现还可以使用STL库函数进行链表的操作,储存,实现该系统,使用STL库会使得代码更加简洁,简单,避免自己敲写链表而出现的失误,造成bug。