赞
踩
采用顺序存储完成学生信息的管理。学生信息包括:学号、姓名、专业、班级、性别、年龄等。要求实现学生信息数据的录入、插入、删除、查找、修改、排序等。具体步骤包括:定义顺序表的存储结构、实现相应基本操作、实现学生信息数据的管理;对顺序表中的学生信息按学号从小到大排序。
- #include <iostream>
- #include <string>
- #include <fstream>
-
- using namespace std;
- //定义学生信息结构体
- typedef struct
- {
- int number;
- char name[10];
- char major[10];
- char grade[10];
- char sex[10];
- int age;
- }Stu;
- typedef struct
- {
- Stu* ST;
- int length;
- }SqList;
- //定义存储学生数
- #define SIZE 500
- class Student
- {
- public:
- void InputList(Stu* S); //输入函数
- int menu(); //菜单
- void InitList(SqList* L); //初始化函数
- void InsertList(SqList* L, int i, Stu S); //插入函数
- void OutputList(SqList* L); //输出函数
- void SearchList(SqList* L); //查找函数
- void DeleteList(SqList* L); //删除函数
- void SortList(SqList* L); //排序函数
- void UpdataList(SqList* L); //修改函数
- };
-
- int main(int argc, const char** agrv)
- {
- Student student;
- SqList L;
- Stu S;
- student.InitList(&L);
- while (1)
- {
- int order = student.menu();
- switch (order)
- {
- case 0:
- {
- return 0;
- }
- case 1:
- {
- student.InputList(&S);
- student.InsertList(&L, 1, S);
- break;
- }
- case 2:
- {
- student.InputList(&S);
- int w = 1;
- cout << "请输入插入位置:";
- cin >> w;
- student.InsertList(&L, w, S);
- break;
- }
- case 3:
- {
- student.SearchList(&L);
- break;
- }
- case 4:
- {
- student.DeleteList(&L);
- break;
- }
- case 5:
- {
- student.OutputList(&L);
- break;
- }
- case 6:
- {
- student.SortList(&L);
- break;
- }
- case 7:
- {
- student.UpdataList(&L);
- break;
- }
- default:
- cout << "输入有误,请重新输入!" << endl;
- break;
- }
- }
- return 0;
- }
- //函数名:菜单显示函数
- //函数功能:显示操作选项
- //参数:无
- //返回值:选项值
- int Student::menu()
- {
- int n;
- while (1)
- {
- cout << "*************欢迎使用学生信息管理系统*************" << endl;
- cout << "1.录入学生信息 2.插入学生数据" << endl;
- cout << "3.查找学生信息 4.删除学生信息" << endl;
- cout << "5.显示学生信息 6.按学号排序学生信息" << endl;
- cout << "7.修改学生信息 0.退出" << endl;
- cout << "**************************************************" << endl;
- cout << "请选择操作:";
- cin >> n;
- return n;
- }
-
- }
- //函数名:初始化
- //函数功能:初始化线性表
- //参数:表L
- //返回值:无
- void Student::InitList(SqList* L)
- {
- L->ST = (Stu*)malloc(SIZE * sizeof(Stu));
- if (L->ST == NULL)
- {
- cout << "内存申请失败!" << endl;
- exit(1);
- }
- L->length = 0;
- }
- //函数名:插入函数
- //函数功能:在指定位置插入记录
- //参数:表L,插入位置i,记录S
- //返回值:无
- void Student::InsertList(SqList* L, int i, Stu S)
- {
- if (i<1 || i>L->length + 1)
- {
- cout << "插入位置异常!" << endl;
- }
- else if (L->length == SIZE)
- {
- cout << "超过最大存储数量!" << endl;
- }
- else
- {
- for (int j = L->length; j > i - 1; j--)
- L->ST[j] = L->ST[j - 1];
- L->ST[i - 1] = S;
- L->length++;
- cout << "输入成功!" << endl;
- }
- }
- //函数名:输入函数
- //函数功能:输入信息
- //参数:记录S
- //返回值:无
- void Student::InputList(Stu* S)
- {
- cout << "请输入学号:";
- cin >> S->number;
- cout << "请输入姓名:";
- cin >> S->name;
- cout << "请输入专业:";
- cin >> S->major;
- cout << "请输入班级:";
- cin >> S->grade;
- cout << "请输入性别:";
- cin >> S->sex;
- cout << "请输入年龄:";
- cin >> S->age;
- }
- //函数名:输出函数
- //函数功能:输出信息
- //参数:表L
- //返回值:无
- void Student:: OutputList(SqList* L)
- {
- if (L->length != 0)
- {
- for (int i = 0; i < L->length; i++)
- {
- cout << L->ST[i].number << '\t';
- cout << L->ST[i].name << '\t';
- cout << L->ST[i].major << '\t';
- cout << L->ST[i].grade << '\t';
- cout << L->ST[i].sex << '\t';
- cout << L->ST[i].age << '\t';
- cout << endl;
- }
-
- }
- else
- {
- cout << "没有学生信息,请先录入!" << endl;
- }
- }
- //函数名:查找函数
- //函数功能:按名字查找指定学生信息
- //参数:表L
- //返回值:无
- void Student::SearchList(SqList* L)
- {
- char Sname[10];
- int i;
- cout << "请输入要查询的学生姓名:";
- cin >> Sname;
- for (i = 0; i < L->length; i++)
- {
- if (!strcmp(L->ST[i].name, Sname))
- {
- cout << "已找到该学生!" << endl;
- cout << L->ST[i].number << '\t';
- cout << L->ST[i].name << '\t';
- cout << L->ST[i].major << '\t';
- cout << L->ST[i].grade << '\t';
- cout << L->ST[i].sex << '\t';
- cout << L->ST[i].age << endl;
- break;
- }
- }
- if (i == L->length)
- {
- cout << "没有找到该学生!" << endl;
- }
- }
- //函数名:删除函数
- //函数功能:删除指定姓名的学生
- //参数:表L
- //返回值:无
- void Student:: DeleteList(SqList* L)
- {
- char Dname[10];
- int i;
- cout << "请输入要删除的学生姓名:";
- cin >> Dname;
- for (i = 0; i < L->length; i++)
- {
- if (!strcmp(L->ST[i].name, Dname))
- {
- for (int j = i + 1; j < L->length; j++)
- {
- L->ST[j - 1] = L->ST[j];
- }
- L->length--;
- break;
- }
- }
- if (i == L->length)
- {
- cout << "没有找到该学生!";
- }
- else
- {
- cout << "删除完成!" << endl;
- }
- }
- //函数名:排序函数
- //函数功能:按学号从小到大排序
- //参数:表L
- //返回值:无
- void Student::SortList(SqList* L)
- {
- for (int i = 0; i < L->length; i++)
- {
- for (int j = 0; j < L->length - i - 1; j++)
- {
- if (L->ST[j].number > L->ST[j + 1].number)
- {
- SqList T;
- InitList(&T);
- T.ST[0] = L->ST[j];
- L->ST[j] = L->ST[j + 1];
- L->ST[j + 1] = T.ST[0];
- }
- }
- }
- cout << "排序完成!" << endl;
- }
- //函数名:修改函数
- //函数功能:修改指定姓名的学生的信息
- //参数:表L
- //返回值:无
- void Student::UpdataList(SqList* L)
- {
- char Uname[10];
- int i;
- int m = 0;
- cout << "请输入要修改信息的学生姓名:";
- cin >> Uname;
- for (i = 0; i < L->length; i++)
- {
- if (!strcmp(L->ST[i].name, Uname))
- {
- cout << "已找到该学生!" << endl;;
-
- cout << L->ST[i].number << '\t';
- cout << L->ST[i].name << '\t';
- cout << L->ST[i].major << '\t';
- cout << L->ST[i].grade << '\t';
- cout << L->ST[i].sex << '\t';
- cout << L->ST[i].age << endl;
- break;
- }
- }
- if (i == L->length)
- {
- cout << "没有找到该学生!";
- return;
- }
- cout << "1.number 2.name 3.major" << endl;
- cout << "4.grade 5.sex 6.age" << endl;
- cout << "请输入想要修改的信息:";
- cin >> m;
- switch (m)
- {
- case 1:
- cout << "请输入新的学号:";
- cin >> L->ST[i].number;
- break;
- case 2:
- cout << "请输入新的姓名:";
- cin >> L->ST[i].name;
- break;
- case 3:
- cout << "请输入新的专业:";
- cin >> L->ST[i].major;
- break;
- case 4:
- cout << "请输入新的班级:";
- cin >> L->ST[i].grade;
- break;
- case 5:
- cout << "请输入新的性别:";
- cin >> L->ST[i].sex;
- break;
- case 6:
- cout << "请输入新的年龄:";
- cin >> L->ST[i].age;
- break;
- default:
- cout << "输入有误,请重新操作!";
- return;
- }
- cout << "修改完成!" << endl;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。