代码实现
主函数
#include<iostream>
#include<string.h>
#include "seqlist.h"
#include<cstdlib>
using namespace std;
int main()
{
int x = 100;
int pnum = 0, pmath = 0, peng = 0, pyuwen = 0, psum = 0;
string pname;
string psex;
StuNode* pfind;
SInfo stu;
cout << " ******************************************" << endl;
cout << " ******************************************" << endl;
cout << " ****** ******" << endl;
cout << " ****** 学生信息管理系统 ******" << endl;
cout << " ****** ******" << endl;
cout << " ******************************************" << endl;
cout << " ******************************************" << endl;
while (x != 0)
{
system("pause");
system("cls"); //清屏
ShowMenu();
cin >> x;
switch (x)
{
case 0:
stu.StuQuit();
break;
case 1:
stu.StuRead();
cout << "读入学生信息表:" << endl;
stu.ShowInfo();
break;
case 2:
stu.CreatSinfo();
cout << "请核对输入学生信息!" << endl;
stu.ShowInfo();
break;
case 3:
cout << "请输入添加学生信息:";
cout << "姓名:";
cin >> pname;
cout << "性别:";
cin >> psex;
cout << "学号:";
cin >> pnum;
cout << "数学成绩:";
cin >> pmath;
cout << "英语成绩:";
cin >> peng;
cout << "语文成绩:";
cin>> pyuwen;
stu.StuInsert(pname, psex, pnum, pmath, peng, pyuwen);
cout << "更新学生信息表..." << endl;
stu.ShowInfo();
break;
case 4:
cout << "请输入要删除学生学号:";
cin >> pnum;
stu.StuDelete(pnum);
cout << "更新学生信息表..." << endl;
stu.ShowInfo();
break;
case 5:
cout << "请输入要查找学生学号:";
cin >> pnum;
pfind = stu.StuFind1(pnum);
cout << "查找学生学号:" << pfind->num << endl;
cout << "姓名:" << pfind->name << endl;
cout << "性别:" << pfind->sex << endl;
cout << " 数学 " << pfind->math << endl;
cout << " 英语 " << pfind->eng << endl;
cout << " 语文 " << pfind->yuwen << endl;
cout<< " 总分 " << pfind->sum << endl;
break;
case 6:
cout << "请输入要查找学生的姓名:";
cin >> pname;
pfind = stu.StuFind2(pname);
cout << "查找学生学号:" << pfind->num << endl;
cout << "姓名:" << pfind->name << endl;
cout << "性别:" << pfind->sex << endl;
cout << " 数学 " << pfind->math << endl;
cout << " 英语 " << pfind->eng << endl;
cout << " 语文 " << pfind->yuwen << endl;
cout << " 总分 " << pfind->sum << endl;
break;
case 7:
cout << "请输入要查找学生的总分:";
cin >> psum;
pfind = stu.StuFind3(psum);
cout << "查找学生学号:" << pfind->num << endl;
cout << "姓名:" << pfind->name << endl;
cout << "性别:" << pfind->sex << endl;
cout << " 数学 " << pfind->math << endl;
cout << " 英语 " << pfind->eng << endl;
cout << " 语文 " << pfind->yuwen << endl;
cout << " 总分 " << pfind->sum << endl;
break;
case 8:
int i;
cout << "按语文成绩查找(1)按数学成绩查找(2)按英语成绩查找(3)" << endl;
cin >> i;
if (i == 1)
{
cout << "请输入要查找学生的语文分数:";
cin >> pyuwen;
pfind = stu.StuFind4("yuwem",pyuwen,peng,pmath);
cout << "查找学生学号:" << pfind->num << endl;
cout << "姓名:" << pfind->name << endl;
cout << "性别:" << pfind->sex << endl;
cout << " 数学 " << pfind->math << endl;
cout << " 英语 " << pfind->eng << endl;
cout << " 语文 " << pfind->yuwen << endl;
cout << " 总分 " << pfind->sum << endl;
}
else if (i == 2)
{
cout << "请输入要查找学生的数学分数:";
cin >> pmath;
pfind = stu.StuFind4("math", pyuwen, peng, pmath);
cout << "查找学生学号:" << pfind->num << endl;
cout << "姓名:" << pfind->name << endl;
cout << "性别:" << pfind->sex << endl;
cout << " 数学 " << pfind->math << endl;
cout << " 英语 " << pfind->eng << endl;
cout << " 语文 " << pfind->yuwen << endl;
cout << " 总分 " << pfind->sum << endl;
}
else if (i == 3)
{
cout << "请输入要查找学生的英语分数:";
cin >> peng;
pfind = stu.StuFind4("eng", pyuwen, peng, pmath);
cout << "查找学生学号:" << pfind->num << endl;
cout << "姓名:" << pfind->name << endl;
cout << "性别:" << pfind->sex << endl;
cout << " 数学 " << pfind->math << endl;
cout << " 英语 " << pfind->eng << endl;
cout << " 语文 " << pfind->yuwen << endl;
cout << " 总分 " << pfind->sum << endl;
}
else
{
cout << "参数输入错误!" << endl;
}
break;
case 9:
cout << "请输入要修改学生学号:";
cin >> pnum;
cout << "请重新输入学生信息:"<<endl;
cout << "姓名:";
cin >> pname;
cout << "性别:";
cin >> psex;
cout << "语文成绩:";
cin >> pyuwen;
cout << "数学成绩:";
cin >> pmath;
cout << "英语成绩:";
cin>> peng;
stu.StuModify(pname, psex, pnum, pmath, peng, pyuwen);
cout << "修改成功!" << endl;
cout << "更新学生信息表..." << endl;
stu.ShowInfo();
break;
case 10:
stu.ShowInfo();
break;
case 11:
stu.StuSave();
break;
case 12:
int p;
cout << "升序排序请输入(0)降序排序请输入(1):" << endl;
cin >> p;
if (p == 0)
{
stu.StuSort("descending");
stu.ShowInfo();
}
else if (p == 1)
{
stu.StuSort("ascending");
stu.ShowInfo();
}
else
{
cout << "请输入正确选择!" << endl;
}
break;
case 13:
stu.ShowInfo();
break;
}
}
system("pause");
return 0;
}
头文件
#pragma once
#define MAX_NUM 100
#include <String>
#include <fstream>
using namespace std;
bool quit = false;
struct StuNode {
int num;
int math, eng, yuwen;
int sum;
string name, sex;
StuNode* nextstu;
};
class SInfo {
StuNode* StuListHead;
public:
SInfo(); //构造函数
~SInfo(); //析构函数
void CreatSinfo(); //创建学生信息
void StuInsert(string sname, string ssex, int snum, int smath, int seng, int syuwen); //插入学生信息
void StuDelete(int snum); //删除学生信息
StuNode* StuFind1(int snum); //按学号查找学生信息
StuNode* StuFind2(string sname); //按姓名查找学生信息
StuNode* StuFind3(int ssum); //按照总分查找学生信息
StuNode* StuFind4(string sub, int syuwen, int seng, int smath);;//按照某科成绩查找学生信息
void StuModify(string name, string sex, int snum, int smath, int seng, int syuwen); //修改学生信息
void StuCopy(StuNode* ptemp, StuNode* p); //学生信息拷贝
void StuRead(); //从文件读入学生信息
void StuSave(); //保存学生信息到文件
void StuQuit();
void ShowInfo(); //遍历输出学生信息
void StuSort(string ch); //按照总分排序
};
void ShowMenu()
{
cout << "**********************************************************" << endl;
cout << "****** 学 生 信 息 管 理 系 统 ******" << endl;
cout << "****** 0.安全退出系统 ******" << 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 << "****** 9.修改已有学生信息 ******" << endl;
cout << "****** 10.输出所有学生信息 *****" << endl;
cout << "****** 11.保存现有学生信息 *****" << endl;
cout << "****** 12.对学生按照总分排序(ascending/descending)*****" << endl;
cout << "****** 13.遍历所有学生信息 *****" << endl;
cout << "**********************************************************" << endl;
cout << "\n\t\n\t\t请选择:";
}
SInfo::SInfo() //构造函数
{
StuListHead = new StuNode;
StuListHead->nextstu = NULL;
}
SInfo::~SInfo() //析构函数
{
StuNode* p;
while (StuListHead)
{
p = StuListHead;
StuListHead = StuListHead->nextstu;
delete p;
}
StuListHead = NULL;
}
void SInfo::CreatSinfo() //创建学生信息表
{
int n;
StuNode* p, * s;
p = StuListHead;
cout << "请输入学生人数:";
cin >> n;
for (int i = 1; i <= n; i++)
{
s = new StuNode;
cout << "请输入学生姓名:";
cin >> s->name;
cout << "请输入学生性别:";
cin >> s->sex;
cout << "请输入学生的学号:";
cin >> s->num;
cout << "请输入学生的语文成绩:";
cin >> s->yuwen;
cout << "请输入学生的数学成绩:";
cin >>s->math;
cout << "请输入学生的英语成绩:";
cin>> s->eng;
s->sum = s->math + s->eng + s->yuwen;
s->nextstu = p->nextstu;
p->nextstu = s;
p = p->nextstu;
}
if (p == NULL) //判断学生信息表是否创建成功
{
cout << "创建失败请重新创建!" << endl;
CreatSinfo();
}
}
void SInfo::ShowInfo() //遍历输出
{
StuNode* p;
cout << "姓名" << '\t' << "性别" << '\t' << "学号" << '\t' << "数学" << '\t' << "英语" << '\t' << "语文" << '\t' << "总分" << endl;
for (p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
cout << p->name<<'\t'<< p->sex <<'\t'<< p->num << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->sum << endl;
}
}
void SInfo::StuInsert(string sname, string ssex, int snum, int smath, int seng, int syuwen) //插入学生信息(头插法)
{
StuNode* s, * p;
s = new StuNode;
s->name = sname;
s->sex = ssex;
s->num = snum;
s->math = smath;
s->eng = seng;
s->yuwen = syuwen;
s->sum = s->math + s->eng + s->yuwen;
p = StuListHead;
s->nextstu = p->nextstu;
p->nextstu = s;
}
void SInfo::StuDelete(int snum)
{
StuNode* p, * ptemp;
p = StuListHead;
ptemp = p;
while (p->nextstu && p->num != snum) //循环终止条件为p->nextstu不为空 而且没有找到相应学号的学生
{
ptemp = p;
p = p->nextstu;
}
if (p->num == snum)
{
ptemp->nextstu = p->nextstu;
delete p;
}
else
{
cout << "未找到该学生信息!" << endl;
}
}
StuNode* SInfo::StuFind1(int snum)
{
StuNode* p;
p = StuListHead->nextstu;
while (p->nextstu && p->num != snum) //循环终止条件为p->nextstu不为空 而且没有找到相应学号的学生
{
p = p->nextstu;
}
if (p->num == snum)
{
return p;
}
else
{
cout << "未找到该学生信息!" << endl;
return NULL;
}
}
StuNode* SInfo::StuFind2(string sname)
{
StuNode* p;
p = StuListHead->nextstu;
while (p->nextstu && p->name != sname) //循环终止条件为p->nextstu不为空 而且没有找到相应名字的学生
{
p = p->nextstu;
}
if (p->name == sname)
{
return p;
}
else
{
cout << "未找到该学生信息!" << endl;
return NULL;
}
}
StuNode* SInfo::StuFind3(int ssum)
{
StuNode* p;
p = StuListHead->nextstu;
while (p->nextstu && p->sum != ssum) //循环终止条件为p->nextstu不为空 而且没有找到相应总分的学生
{
p = p->nextstu;
}
if (p->sum == ssum)
{
return p;
}
else
{
cout << "未找到该学生信息!" << endl;
return NULL;
}
}
StuNode* SInfo::StuFind4(string sub,int syuwen,int seng,int smath)
{
if (sub == "yuwen")
{
StuNode* p;
p = StuListHead->nextstu;
while (p->nextstu && p->yuwen != syuwen) //循环终止条件为p->nextstu不为空 而且没有找到相应语文成绩的学生
{
p = p->nextstu;
}
if (p->yuwen == syuwen)
{
return p;
}
else
{
cout << "未找到该学生信息!" << endl;
return NULL;
}
}
else if (sub == "eng")
{
StuNode* p;
p = StuListHead->nextstu;
while (p->nextstu && p->eng != seng) //循环终止条件为p->nextstu不为空 而且没有找到相应英语成绩的学生
{
p = p->nextstu;
}
if (p->eng == seng)
{
return p;
}
else
{
cout << "未找到该学生信息!" << endl;
return NULL;
}
}
else if (sub == "math")
{
StuNode* p;
p = StuListHead->nextstu;
while (p->nextstu && p->math != smath) //循环终止条件为p->nextstu不为空 而且没有找到相应数学成绩的学生
{
p = p->nextstu;
}
if (p->math == smath)
{
return p;
}
else
{
cout << "未找到该学生信息!" << endl;
return NULL;
}
}
else
{
cout << "查找条件出错!" << endl;
}
}
void SInfo::StuModify(string sname, string ssex, int snum, int smath, int seng, int syuwen)
{
StuNode* ItemStu = StuFind1(snum); //直接调用查找函数
if (ItemStu != NULL)
{
ItemStu->name = sname;
ItemStu->sex = ssex;
ItemStu->math = smath;
ItemStu->num = snum;
ItemStu->math = smath;
ItemStu->eng = seng;
ItemStu->yuwen = syuwen;
ItemStu->sum = ItemStu->math + ItemStu->eng + ItemStu->yuwen;
}
}
void SInfo::StuCopy(StuNode* ptemp, StuNode* p) //拷贝学生信息(将p的信息拷贝到ptemp中)
{
if (p == NULL)
{
cout << "拷贝目标为空!" << endl;
}
else
{
ptemp->num = p->num;
ptemp->name = p->name;
ptemp->sex = p->sex;
ptemp->math = p->math;
ptemp->eng = p->eng;
ptemp->yuwen = p->yuwen;
ptemp->sum = p->sum;
}
}
void SInfo::StuRead() //从文件读入数据
{
StuNode* p;
p = StuListHead;
ifstream in("StudentList.txt");
if (!in) { cout << "没有学生信息,请先录入学生信息!" << endl; return; }
while (!in.eof())
{
string name, sex;
int num, math, eng, yuwen, sum;
in >> name >> sex >> num >> math >> eng >> yuwen >> sum;
StuInsert(name, sex, num, math, eng, yuwen);
}
}
void SInfo::StuSave() //保存学生信息
{
StuNode* p;
p = StuListHead->nextstu;
ofstream out("StudentList.txt");
if (!out) { cout << "不能打开文件!" << endl; return; }
while (p != NULL)
{
out <<p->name<<'\t'<<p->sex<<'\t'<< p->num << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->sum << '\n';
p = p->nextstu;
}
}
void SInfo::StuQuit() //学生信息写入文件
{
char choice;
cout << "是否保存学生信息:?(Y/N)";
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
StuSave();
cout << "学生信息已保存..." << endl;
}
}
void SInfo::StuSort(string ch) //根据总分排序
{
if (ch == "ascending")
{
for (StuNode* p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode* q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->sum > q->sum)
{
StuNode* ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else if (ch =="descending")
{
for (StuNode* p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode* q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->sum < q->sum)
{
StuNode* ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else
{
cout << "排序条件出错!" << endl;
}
}
有待改进:
txt文档读入,第一条记录会出现重复读入,但是没有名字,目前找不出原因所在,但是可以通过删除学生信息,解决空白名字的那行输入;
关于警告的问题:
会有警告提示,目前无法解决,望有经验的朋友能不吝赐教,万分感谢!!