数据结构使用顺序表创建一个学生信息管理系统,仅包含赋值、查找、输出功能
时间: 2023-05-29 14:03:37 浏览: 109
学生管理系统的设计与实现
#include <iostream>
using namespace std;
const int max_size = 100; //定义顺序表的最大长度
struct student {
string name; //学生姓名
int age; //学生年龄
string gender; //学生性别
string major; //学生专业
};
struct SeqList {
student data[max_size]; //定义存储学生信息的数组
int length; //记录当前数组中存储的学生数量
};
//初始化顺序表
void InitList(SeqList &L) {
L.length = 0;
}
//插入学生信息
void InsertList(SeqList &L, student stu) {
if (L.length >= max_size) {
cout << "顺序表已满,无法插入!" << endl;
return;
}
L.data[L.length] = stu;
L.length++;
cout << "插入成功!" << endl;
}
//按姓名查找学生信息
void FindByName(SeqList L, string name) {
bool flag = false; //标记是否找到该学生
for (int i = 0; i < L.length; i++) {
if (L.data[i].name == name) {
cout << "姓名:" << L.data[i].name << endl;
cout << "年龄:" << L.data[i].age << endl;
cout << "性别:" << L.data[i].gender << endl;
cout << "专业:" << L.data[i].major << endl;
flag = true;
break;
}
}
if (!flag) {
cout << "未找到该学生!" << endl;
}
}
//输出所有学生信息
void PrintList(SeqList L) {
for (int i = 0; i < L.length; i++) {
cout << "姓名:" << L.data[i].name << endl;
cout << "年龄:" << L.data[i].age << endl;
cout << "性别:" << L.data[i].gender << endl;
cout << "专业:" << L.data[i].major << endl;
cout << endl;
}
}
int main() {
SeqList L;
InitList(L);
int choice;
student stu;
string name;
while (true) {
cout << "请选择操作:" << endl;
cout << "1.插入学生信息 2.按姓名查找学生信息 3.输出所有学生信息 4.退出" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入学生姓名、年龄、性别、专业:" << endl;
cin >> stu.name >> stu.age >> stu.gender >> stu.major;
InsertList(L, stu);
break;
case 2:
cout << "请输入要查找的学生姓名:" << endl;
cin >> name;
FindByName(L, name);
break;
case 3:
PrintList(L);
break;
case 4:
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
阅读全文