开发环境:visual studio2019
项目:学生管理系统
功能;1.信息录入,2.查询,3.增加,4.修改,5.删除
学生基本信息包括:学号,姓名,性别,数学成绩,英语成绩,计算机成绩和总成绩七个数据项
需求:用到文件操作
我将类与结构体写到LinkList.h头文件里,主函数与普通函数写到system.cpp文件里。
附上代码:
LinkList.h头文件(运用了单链表的知识)
#pragma once
#include
#include
using namespace std;
typedef struct student {//学生信息结构体
string name;//姓名
string id;//学号
string gender;//性别
float math;//数学成绩
float english;//英语成绩
float computer;//计算机成绩
float sumscore;//总成绩
student* next;//next指针
}student, * stu;
class LinkList {
public:
LinkList();//建立只有头结点的单链表
LinkList(string names, string ids, string genders, float maths, float englishs, float computers);//建立单链表
~LinkList();//析构函数
student* Locate(string names);//按名查找。
bool change(string names, float newmath, float newenglish, float newcomputer);//修改信息
void Insert(string names, string ids, string genders, float maths, float englishs, float computers);//增加操作
bool Delete(string names);//删除操作,
void PrintList();//遍历操作,按序号依次输入各元素
struct student* first;//单链表的头指针
//private:
};
LinkList::LinkList() {//类函数,建立只有头结点的单链表
first = new student;
first->next = nullptr;
}
LinkList::LinkList(string names, string ids, string genders, float maths, float englishs, float computers) {//类函数,建立单链表
float sumscores;
first = new student;
first->next = nullptr;
student* s = nullptr;
s = new student;
s->name = names;
s->id = ids;
s->gender = genders;
s->math = maths;
s->english = englishs;
s->computer = computers;
sumscores = maths + englishs + computers;
s->sumscore = sumscores;
s->next = first->next;
first->next = s;
}
LinkList::~LinkList() {//类函数,析构函数
student* p = first;
while (first != nullptr) {
first = first->next;
delete p;
p = first;
}
}
student* LinkList::Locate(string names) {//类函数,按名字查找。查找值为names的元素符号
student* p = first->next;
int count = 1;
while (p != nullptr) {
if (p->name == names)return p;
p = p->next;
count++;
}
return nullptr;//退出循环表明查找失败
}
void LinkList::Insert(string names, string ids, string genders, float maths, float englishs, float computers) {//类函数,增加操作
student* p = first, * s = nullptr;
float sumscores;
s = new student;
s->name = names;
s->id = ids;
s->gender = genders;
s->math = maths;
s->english = englishs;
s->computer = computers;
sumscores = maths + englishs + computers;
s->sumscore = sumscores;
s->next = p->next;
first->next = s;
}
bool LinkList::Delete(string names) {//类函数,删除操作
student* p = first->next, * s = first, * q = nullptr;
q = new student;
while (p != nullptr && p->name != names) {
p = p->next;
s = s->next;
}
if (p->name == names) {
s->next = p->next;
q = p;
delete q;
return true;
}
return false;
}
void LinkList::PrintList() {//类函数,遍历操作,按序号依次输出各元素
student* p = first->next;
cout << "姓名" << " " << "学号" << " " << "性别" << " " << "数学" << " " << "英语" << " " << "计算机" << " " << "总成绩" << endl;
while (p != nullptr) {
cout << p->name << "\t" << p->id << "\t" << p->gender << "\t" << p->math << "\t" << p->english << "\t" << p->computer << "\t" << p->sumscore << endl;
p = p->next;
}
cout << endl;
}
bool LinkList::change(string names,float newmath,float newenglish,float newcomputer ) {//类函数,按名字查找。查找值为names的元素符号,修改成绩
student* p = first->next;
while (p != nullptr) {
if (p->name == names)break;
p = p->next;
}
if (p == nullptr)return false;
if (newmath >= 0)p->math = newmath;
if (newenglish >= 0)p->english = newenglish;
if (newcomputer >= 0)p->computer = newcomputer;
p->sumscore = p->math + p->english + p->computer;
return true;
}
system.cpp源文件
#include
#include
#include
#include
using namespace std;
void readdata(LinkList *s) {//将文件里的信息提取到单链表中
student* p = s->first, * pp = nullptr,*ppp=nullptr;//*ppp指针是用于解决eof()函数所引起的末尾重复问题
ifstream out("student.dat", ios::binary);
if (!out) {
cout << "文件打开失败!" << endl;
return;
}
while (!out.eof()){
pp = new student;
if (out.peek() == EOF)break;
out >> pp->name >> pp->id >>pp->gender >>pp->math >> pp->english >> pp->computer >> pp->sumscore;
p ->next= pp;
ppp = p;
p = p->next;
}
ppp ->next = nullptr;
out.close();
}
void writedata(LinkList* s) {//将单链表信息写入文件
student* p = s->first->next;
ofstream in("student.dat", ios::binary);
if (!in) {
cout << "文件无法创建" << endl;
return;
}
while (p != nullptr) {
in << p->name << "\t" << p->id << "\t" <<p->gender<<"\t"<< p->math << "\t" << p->english << "\t" << p->computer << "\t" << p->sumscore << "\n";
p = p->next;
}
in.close();
}
void writein(LinkList* s) {//学生信息的录入,添加
int number(0);
string name, id,gender;
float math, english, computer;
int choose;
do {
cout << "学生的姓名:";
cin >> name;
cout << "学生的学号:";
cin >> id;
cout << "学生的性别:";
cin >> gender;
cout << "学生的数学成绩:";
cin >> math;
cout << "学生的英语成绩:";
cin >> english;
cout << "学生的计算机成绩:";
cin >> computer;
s->Insert(name, id, gender, math, english, computer);
cout << "请选择你接下来的操作(0:退出 任意;继续录入信息):";
cin >> choose;
} while (choose != 0);
}
void finddata(LinkList* s) {//查找函数
student* p;
int choose;
string names;
cout << "所有学生的信息" << endl;
s->PrintList();
while (true) {
cout <<"可以继续选择以下操作"<<endl
<< "1.姓名查询" << endl
<< "0.退出查询" << endl
<< "请选择你的操作:";
cin >> choose;
if (choose == 0)return;
else {
cout << "输入你想查询的名字:";
cin >> names;
p = s->Locate(names);
if (p == nullptr)cout << "数据库中不存在该学生信息。"<<endl<<endl;
else cout << p->name << "\t" << p->id << "\t" << p->gender << "\t" << p->math << "\t" << p->english << "\t" << p->computer << "\t" << p->sumscore << endl<<endl;
}
}
}
void changedata(LinkList* s) {//修改函数
string name;
float math, english, computer;
int choose;
bool judge;
do {
cout << "输入你想修改学生信息的姓名:";
cin >> name;
cout << "依次输出你想修改的数学,英语,计算机成绩(若不变则输入负数):";
cin >> math >> english >> computer;
judge = s->change(name, math, english, computer);
if (judge == false)cout << "修改失败,或许数据库中不存在该名同学"<<endl<<endl;
else cout << "修改成功!" << endl<<endl;
cout << "请选择你接下来进行的操作(0:退出 任意;继续修改信息)";
cin >> choose;
} while (choose != 0);
}
void deletedata(LinkList* s) {//删除函数
string name;
int choose;
bool judge;
do {
cout << "输入你想删除学生信息的姓名:";
cin >> name;
judge = s->Delete(name);
if (judge == false)cout << "删除失败,或许数据库中不存在该名同学" << endl << endl;
else cout << "删除成功!" << endl << endl;
cout << "请选择你接下来进行的操作(0:退出 任意;继续删除信息)";
cin >> choose;
} while (choose != 0);
}
int main() {//主函数
class LinkList* stud =new LinkList;
char choose;
bool judge=true;
readdata(stud);
while (judge) {
cout << "----------------------------------------"<< endl
<< " 欢迎使用小小城序员学生管理系统 " << endl
<< " 1.学生信息录入 " << endl
<< " 2.学生信息的查询 " << endl
<< " 3.添加学生信息 " << endl
<< " 4.修改学生信息 " << endl
<< " 5.删除学生信息 " << endl
<< " 6.退出系统 " << endl
<< "----------------------------------------" << endl;
cout << "请选择你的操作:";
cin >> choose;
switch (choose)
{
case '1':
writein(stud);
writedata(stud);
break;
case '2':
finddata(stud);
break;
case '3':
writein(stud);
writedata(stud);
break;
case '4':
changedata(stud);
writedata(stud);
break;
case '5':
deletedata(stud);
writedata(stud);
break;
case '6':
judge = false;
break;
default:
cout << "输入有误,请重新输入!";
break;
}
}
cout << "再见!";
}
本人能力有限,只能实现到这种程度。代码或许也不是很规范,实现的方法或许也不是很好,但我也一直在努力学习。程序中有不当的地方或者有待改进的地方,希望各位可以提出。