前言
c++课程作业–学生信息管理系统
在
原博客C++通讯录管理系统
https://www.csdn.net/tags/OtDagg2sODU2Ni1ibG9n.html
的基础上进行了一点修改。
学生信息管理系统
基本功能要求:
能使用文件的打开,关闭,读写等操作,实现
1.连续输入若干个学生信息(学号,姓名,电话号码),保存在文件中
2.可以从文件读出学生信息并显示在屏幕
可扩展功能:
1.输入学号,查找学生姓名,电话号码
2.输入姓名:查找电话号码
3.删除某个学生信息
4.修改学生电话号码
其他自定义的功能
代码
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <windows.h>
using namespace std;
/*学生数据元素*/
typedef struct {
int num; //编号
string name; //姓名
string phonenum; //电话
}ElemType;
/*链表*/
typedef struct Node {
ElemType data;
struct Node* next;
}Node, * LinkList;
/*函数声明列表*/
void Read_LinkList(LinkList& L); //读取信息
void Save_LinkList(const LinkList& L); //保存信息
void Init_LinkList(LinkList& L); //初始化链表
void Create_LinkList(LinkList& L); //添加学生信息
void Delete_LinkList(LinkList& L); //删除学生信息
void Query_LinkList(const LinkList& L); //查询
void Modify_LinkList(LinkList& L); //修改
void Print_LinkList(const LinkList& L); //显示
bool compare(const ElemType& t1, const ElemType& t2); //定义sort函数的关系
void Sort_LinkList(LinkList& L); //排序
void Clear_LinkList(LinkList& L); //清空
void menu(LinkList& L); //主控菜单
/*程序入口---主函数*/
int main()
{
LinkList L;
Init_LinkList(L);
Read_LinkList(L);
menu(L);
return 0;
}
/*读取信息函数*/
void Read_LinkList(LinkList& L)
{
Node* p = L;
ifstream infile("infomation.txt", ios::in);
int len = 0;
infile >> len;
ElemType tem;
while (len--)
{
infile >> tem.num >> tem.name >> tem.phonenum ;
Node* t = new Node;
t->data = tem;
t->next = NULL;
p->next = t;
p = p->next;
}
infile.close();
}
/*保存信息函数*/
void Save_LinkList(LinkList& L)
{
Node* t = L, * cnt = L;
ofstream outfile("infomation.txt", ios::out);
int len = 0;
while (cnt->next)
{
len++;
cnt = cnt->next;
}
outfile << len << endl;
while (t)
{
if (t != L)
outfile << t->data.num << "\t" << t->data.name << "\t" << t->data.phonenum << endl;
t = t->next;
}
outfile.close();
}
/*初始化链表函数*/
void Init_LinkList(LinkList& L)
{
L = new Node;
L->next = NULL;
}
/*添加学生信息函数*/
void Create_LinkList(LinkList& L)
{
system("cls");
cout << "\t\t\t**************添加学生信息***************" << endl;
Node* t = L;
int i = 1, flag = 1;
while (t->next)
{
i++;
t = t->next;
}
while (flag)
{
Node* p = new Node;
cout<<"\t\t\t输入学生编号:";
cin>>p-> data.num;
cout << "\t\t\t输入姓名:";
cin >> p->data.name;
cout << "\t\t\t输入电话:";
cin >> p->data.phonenum;
p->next = NULL;
t->next = p;
t = t->next;
Save_LinkList(L); //将改动保存至文件中
cout << "\t\t\t添加成功!是否继续添加?(1 是 0 否)" << endl;
cout << "\t\t\t请选择【0-1】:";
cin >> flag;
}
}
/*删除学生信息函数*/
void Delete_LinkList(LinkList& L)
{
system("cls");
cout << "\t\t\t**************删除学生信息***************" << endl;
int sel = 0;
Node* p = L, * t = NULL;
ElemType tem;
cout << "\t\t\t-----------------" << endl;
cout << "\t\t\t1 按学号删除" << endl;
cout << "\t\t\t2 按姓名删除" << endl;
cout << "\t\t\t3 返回主菜单" << endl;
cout << "\t\t\t-----------------" << endl;
cout << "\t\t\t请选择【1-3】:";
cin >> sel;
while (sel < 1 || sel>3)
{
cout << "\t\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if (sel == 1)
{
int flag = 0;
cout << "\t\t\t请输入待删除学生的编号:";
cin >> tem.num;
while (p->next)
{
t = p->next;
if (t->data.num == tem.num)
{
cout << "\t\t\t待删除学生的信息如下:" << endl;
cout<<"\t\t\t**************************************"<<endl;
cout << "\t\t\t学号\t" << "姓名\t" << "联系电话" << endl;
cout << "\t\t\t" << t->data.num << "\t" << t->data.name << "\t" << t->data.phonenum << endl;
cout<<"\t\t\t**************************************"<<endl;
flag = 1;
break;
}
p = p->next;
}
if (flag == 0) cout << "\t\t\t查无此人,无法删除!" << endl;
else
{
cout << "\t\t\t确认删除?(1 是 0 否)" << endl;
cout << "\t\t\t请选择【0-1】:";
cin >> sel;
while (sel < 0 || sel>1)
{
cout << "\t\t\t输入不合法,请重新选择【0-1】:";
cin >> sel;
}
if (sel == 0);
else if (sel == 1)
{
p->next = t->next;
delete t;
cout << "\t\t\t删除成功!" << endl;
Save_LinkList(L); //改动保存至文件中
}
}
cout << "\n\t\t\t";
system("pause");
Delete_LinkList(L);
}
else if (sel == 2)
{
int flag = 0;
cout << "\t\t\t请输入待删学生的姓名:";
cin >> tem.name;
while (p->next)
{
t = p->next;
if (t->data.name == tem.name)
{
cout << "\t\t\t待删除学生的信息如下:" << endl;
cout<<"\t\t\t**************************************"<<endl;
cout << "\t\t\t学号\t" << "姓名\t" << "联系电话" << endl;
cout << "\t\t\t" << t->data.num << "\t" << t->data.name << "\t" << t->data.phonenum << endl;
cout<<"\t\t\t**************************************"<<endl;
flag = 1;
break;
}
p = p->next;
}
if (flag == 0) cout << "\t\t\t查无此人,无法删除!" << endl;
else
{
cout << "\t\t\t确认删除?(1 是 0 否)" << endl;
cout << "\t\t\t请选择【0-1】:";
cin >> sel;
while (sel < 0 || sel>1)
{
cout << "\t\t\t输入不合法,请重新选择【0-1】:";
cin >> sel;
}
if (sel == 0);
else if (sel == 1)
{
p->next = t->next;
delete t;
cout << "\t\t\t删除成功!" << endl;
Save_LinkList(L); //改动保存至文件中
}
}
cout << "\n\t\t\t";
system("pause");
Delete_LinkList(L);
}
else if (sel == 3) return;
}
/*查询函数*/
void Query_LinkList(const LinkList& L)
{
system("cls");
cout << "\t\t\t**************查询功能***************" << endl;
int sel = 0;
Node* t = L;
ElemType tem;
cout << "\t\t\t-----------------" << endl;
cout << "\t\t\t1 按学号查询" << endl;
cout << "\t\t\t2 按姓名查询" << endl;
cout << "\t\t\t3 返回主菜单" << endl;
cout << "\t\t\t-----------------" << endl;
cout << "\t\t\t请选择【1-3】:";
cin >> sel;
while (sel < 1 || sel>3)
{
cout << "\t\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if (sel == 1)
{
int flag = 0;
cout << "\t\t\t请输入待查询学生的学号:";
cin >> tem.num;
while (t->next)
{
t = t->next;
if (t->data.num == tem.num)
{
cout << "\t\t\t待查询学生信息如下:" << endl;
cout<<"\t\t\t**************************************"<<endl;
cout << "\t\t\t姓名\t" << "联系电话\t" << endl;
cout << "\t\t\t" << t->data.name << "\t" << t->data.phonenum << endl;
cout<<"\t\t\t**************************************"<<endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "\t\t\t查无此人!" << endl;
cout << "\n\t\t\t";
system("pause");
Query_LinkList(L);
}
else if (sel == 2)
{
int flag = 0;
cout << "\t\t\t请输入待查询学生的姓名:";
cin >> tem.name;
while (t->next)
{
t = t->next;
if (t->data.name == tem.name)
{
cout << "\t\t\t待查询学生信息如下:" << endl;
cout<<"\t\t\t**************************************"<<endl;
cout << "\t\t\t联系电话" << endl;
cout << "\t\t\t"<< t->data.phonenum<< endl;
cout<<"\t\t\t**************************************"<<endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "\t\t\t查无此人!" << endl;
cout << "\n\t\t\t";
system("pause");
Query_LinkList(L);
}
else if (sel == 3)
{
return;
}
}
/*修改学生信息函数*/
void Modify_LinkList(LinkList& L)
{
system("cls");
cout << "\t\t\t**************修改学生信息***************" << endl;
int sel = 0;
Node* t = L;
ElemType tem;
cout << "\t\t\t-----------------" << endl;
cout << "\t\t\t1 按学号修改" << endl;
cout << "\t\t\t2 按姓名修改" << endl;
cout << "\t\t\t3 返回主菜单" << endl;
cout << "\t\t\t-----------------" << endl;
cout << "\t\t\t请选择【1-3】:";
cin >> sel;
while (sel < 1 || sel>3)
{
cout << "\t\t\t输入不合法,请重新输入【1-3】:";
cin >> sel;
}
if (sel == 1)
{
int flag = 0;
cout << "\t\t\t请输入待修改学生的学号:";
cin >> tem.num;
while (t->next)
{
t = t->next;
if (t->data.num == tem.num)
{
cout << "\t\t\t待修改学生信息如下:" << endl;
cout<<"\t\t\t**************************************"<<endl;
cout << "\t\t\t学号\t" << "姓名\t" << "联系电话\t"<< endl;
cout << "\t\t\t" << t->data.num << "\t" << t->data.name << "\t" << t->data.phonenum << endl;
cout<<"\t\t\t**************************************"<<endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "\t\t\t查无此人,无法修改!" << endl;
else
{
ElemType tem;
cout << "\t\t\t输入要修改的信息:" << endl;
// cout << "\t\t\t输入姓名:";
// cin >> tem.name;
cout << "\t\t\t输入新的【电话】:";
cin >> tem.phonenum;
tem.num = t->data.num;
tem.name=t->data.name;
cout << "\t\t\t确认修改?(1 是 0 否)" << endl;
cout << "\t\t\t请选择【0-1】:";
cin >> sel;
while (sel < 0 || sel>1)
{
cout << "\t\t\t输入不合法,请重新选择【0-1】:";
cin >> sel;
}
if (sel == 0);
else if (sel == 1)
{
t->data = tem;
cout << "\t\t\t修改成功!" << endl;
Save_LinkList(L); //将改动保存至文件中
}
}
cout << "\n\t\t\t";
system("pause");
Modify_LinkList(L);
}
else if (sel == 2)
{
int flag = 0;
cout << "\t\t\t请输入待修改学生的姓名:";
cin >> tem.name;
while (t->next)
{
t = t->next;
if (t->data.name == tem.name)
{
cout << "\t\t\t待修改学生的信息如下:" << endl;
cout<<"\t\t\t**************************************"<<endl;
cout << "\t\t\t学号\t" << "姓名\t" << "联系电话"<< endl;
cout << "\t\t\t" << t->data.num << "\t" << t->data.name << "\t" << t->data.phonenum << endl;
cout<<"\t\t\t**************************************"<<endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "\t\t\t查无此人,无法修改!" << endl;
else
{
ElemType tem;
cout << "\t\t\t输入修改后的学生信息:" << endl;
// cout << "\t\t\t输入姓名:";
// cin >> tem.name;
cout << "\t\t\t输入新的【电话】:";
cin >> tem.phonenum;
tem.num = t->data.num;
tem.name=t->data.name;
cout << "\t\t\t确认修改?(1 是 0 否)" << endl;
cout << "\t\t\t请选择【0-1】:";
cin >> sel;
while (sel < 0 || sel>1)
{
cout << "\t\t\t输入不合法,请重新选择【0-1】:";
cin >> sel;
}
if (sel == 0);
else if (sel == 1)
{
t->data = tem;
cout << "\t\t\t修改成功!" << endl;
Save_LinkList(L); //将改动保存至文件中
}
}
cout << "\n\t\t\t";
system("pause");
Delete_LinkList(L);
}
else if (sel == 3) return;
}
/*学生信息显示函数*/
void Print_LinkList(const LinkList& L)
{
system("cls");
cout << "\t\t\t***************学生信息***************" << endl;
Node* t = L->next;
cout << "\t\t\t-----------------------------------------------------" << endl;
cout << "\t\t\t学号\t" << "姓名\t" << "联系电话" << endl;
cout << "\t\t\t-----------------------------------------------------" << endl;
while (t)
{
cout << "\t\t\t" << t->data.num << "\t" << t->data.name << "\t" << t->data.phonenum<< endl;
t = t->next;
}
cout << "\t\t\t-----------------------------------------------------" << endl;
cout << "\t\t\t";
system("pause");
}
/*排序函数中sort函数的第三个参数*/
bool compare(const ElemType& t1, const ElemType& t2)
{
return t1.num < t2.num;
}
/*排序函数*/
void Sort_LinkList(LinkList& L)
{
system("cls");
cout << "\t\t\t***************排序功能***************" << endl;
Node* p = L;
int cnt = 0, i = 0;
while (p->next)
{
p = p->next;
cnt++;
}
ElemType* arr = new ElemType[cnt];
p = L;
while (p->next)
{
p = p->next;
arr[i++] = p->data;
}
sort(arr, arr + cnt, compare);
p = L, i = 0;
while (p->next)
{
p = p->next;
p->data = arr[i++];
// p->data.num = i;
}
cout << "\t\t\t对学生信息进行(学号)排序并整理如下:" << endl;
Node* t = L->next;
cout << "\t\t\t-----------------------------------------------------" << endl;
cout << "\t\t\t学号\t" << "姓名\t" << "联系电话" << endl;
cout << "\t\t\t-----------------------------------------------------" << endl;
while (t)
{
cout << "\t\t\t" << t->data.num << "\t" << t->data.name << "\t" << t->data.phonenum << endl;
t = t->next;
}
cout << "\t\t\t-----------------------------------------------------" << endl;
Save_LinkList(L); //改动保存至文件中
cout << "\t\t\t";
system("pause");
}
/*清空函数*/
void Clear_LinkList(LinkList& L)
{
int sel = 0;
system("cls");
cout << "\t\t\t**************清空功能(请谨慎选择)*************" << endl;
cout << "\t\t\t-----------------" << endl;
cout << "\t\t\t1 确认清空学生信息" << endl;
cout << "\t\t\t2 返回主菜单" << endl;
cout << "\t\t\t-----------------" << endl;
cout << "\t\t\t请慎重选择【1-2】:";
cin >> sel;
while (sel < 1 || sel>2)
{
cout << "\t\t\t输入不合法,请重新输入【1-2】:";
cin >> sel;
}
if (sel == 1)
{
Node* head = L;
if (head == NULL)
{
return;
}
//清空链表,是不清空头节点的,因此从第一个有数据的节点开始释放
Node* curNode = head->next;
while (curNode != NULL)
{
//先保住下一个节点的位置
Node* nextNode = curNode->next;
free(curNode);
curNode = nextNode;
}
//将头结点next指针置空
head->next = NULL;
Save_LinkList(L);
cout << "\n\t\t\t";
system("pause");
}
else if (sel == 2) return;
}
/*主控菜单函数*/
void menu(LinkList& L)
{
char sel;
system("cls");
cout << "\t\t\t***********欢迎来到学生信息管理系统***********" << endl;
cout << "\t\t\t你可以进行以下操作:" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 1 添加学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 2 删除学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 3 修改学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 4 查询学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 5 显示所有学生 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 6 排序 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 7 清空(慎选) |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 0 退出 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t请选择【0-7】:";
cin >> sel;
while (sel < '0' || sel>'7')
{
cout << "\t\t\t输入非法,请重新选择【0-7】:";
cin >> sel;
}
switch (sel)
{
case '1':
Create_LinkList(L);
menu(L);
break;
case '2':
Delete_LinkList(L);
menu(L);
break;
case '3':
Modify_LinkList(L);
menu(L);
break;
case '4':
Query_LinkList(L);
menu(L);
break;
case '5':
Print_LinkList(L);
menu(L);
break;
case '6':
Sort_LinkList(L);
menu(L);
break;
case '7':
Clear_LinkList(L);
menu(L);
break;
case '0':
exit(0);
default:
menu(L);
}
}
在原理播客-通讯录系统上进行一些小修改,使之成为学生信息管理系统
运行结果
菜单页面
操作一:显示所有学生
操作二:添加学生信息
查看是否成功
进入txt文件查看
操作三:删除学号为1001的学生
菜单【5】查看
查看txt文件
操作四:修改 1002 的号码
操作五:排序
操作六:查询信息
操作七:清空
注:代码在dev运行生成.exe文件 可能会有小伙伴运行没有生成.txt文件的(与代码和cpp文件在同一文件下 建议给一个单独的文件夹以免找不到txt文件)没生成可以换个方法试试,顺便说一下 这是全部的代码了,复制黏贴运行即可。