请你使用DEVC++,基于单链表存储结构,完成学生信息链表创建、学生信息输入、学生信息 查询、学生信息修改、学生信息删除、学生信息输出(全部或部分输出)或保存到文件,学生相关排序。
时间: 2024-01-12 22:04:47 浏览: 86
基于C++链表的学生信息管理
5星 · 资源好评率100%
好的,以下是基于单链表存储结构的学生信息管理系统的实现代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 定义学生信息结构体
struct Student {
int id; // 学号
string name; // 姓名
int age; // 年龄
string gender; // 性别
double score; // 成绩
Student* next; // 指向下一个节点的指针
};
// 初始化链表
Student* init() {
Student* head = new Student;
head->next = NULL;
return head;
}
// 添加学生信息
void add(Student* head) {
Student* p = head;
while (p->next != NULL) {
p = p->next;
}
Student* newStudent = new Student;
cout << "请输入学生信息:\n";
cout << "学号:";
cin >> newStudent->id;
cout << "姓名:";
cin >> newStudent->name;
cout << "年龄:";
cin >> newStudent->age;
cout << "性别:";
cin >> newStudent->gender;
cout << "成绩:";
cin >> newStudent->score;
newStudent->next = NULL;
p->next = newStudent;
cout << "学生信息添加成功!\n";
}
// 查询学生信息
void query(Student* head) {
int id;
cout << "请输入要查询的学生学号:";
cin >> id;
Student* p = head->next;
while (p != NULL) {
if (p->id == id) {
cout << "学号:" << p->id << " 姓名:" << p->name << " 年龄:" << p->age << " 性别:" << p->gender << " 成绩:" << p->score << endl;
return;
}
p = p->next;
}
cout << "未找到该学生的信息!\n";
}
// 修改学生信息
void modify(Student* head) {
int id;
cout << "请输入要修改的学生学号:";
cin >> id;
Student* p = head->next;
while (p != NULL) {
if (p->id == id) {
cout << "请输入修改后的学生信息:\n";
cout << "姓名:";
cin >> p->name;
cout << "年龄:";
cin >> p->age;
cout << "性别:";
cin >> p->gender;
cout << "成绩:";
cin >> p->score;
cout << "学生信息修改成功!\n";
return;
}
p = p->next;
}
cout << "未找到该学生的信息!\n";
}
// 删除学生信息
void remove(Student* head) {
int id;
cout << "请输入要删除的学生学号:";
cin >> id;
Student* p = head;
while (p->next != NULL) {
if (p->next->id == id) {
Student* temp = p->next;
p->next = temp->next;
delete temp;
cout << "学生信息删除成功!\n";
return;
}
p = p->next;
}
cout << "未找到该学生的信息!\n";
}
// 输出学生信息
void print(Student* head) {
Student* p = head->next;
while (p != NULL) {
cout << "学号:" << p->id << " 姓名:" << p->name << " 年龄:" << p->age << " 性别:" << p->gender << " 成绩:" << p->score << endl;
p = p->next;
}
}
// 保存学生信息到文件
void save(Student* head) {
ofstream outfile("student.txt");
if (!outfile.is_open()) {
cout << "无法打开文件!\n";
return;
}
Student* p = head->next;
while (p != NULL) {
outfile << p->id << " " << p->name << " " << p->age << " " << p->gender << " " << p->score << endl;
p = p->next;
}
outfile.close();
cout << "学生信息保存成功!\n";
}
// 从文件加载学生信息
void load(Student* head) {
ifstream infile("student.txt");
if (!infile.is_open()) {
cout << "无法打开文件!\n";
return;
}
while (!infile.eof()) {
Student* newStudent = new Student;
infile >> newStudent->id >> newStudent->name >> newStudent->age >> newStudent->gender >> newStudent->score;
newStudent->next = NULL;
Student* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = newStudent;
}
infile.close();
cout << "学生信息加载成功!\n";
}
// 根据学号排序学生信息
void sortById(Student* head) {
Student* p, * q, * tail;
tail = NULL;
while (head->next != tail) {
p = head;
q = p->next;
while (q->next != tail) {
if (q->id > q->next->id) {
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
}
p = p->next;
q = p->next;
}
tail = q;
}
cout << "学生信息按学号排序成功!\n";
}
// 根据成绩排序学生信息
void sortByScore(Student* head) {
Student* p, * q, * tail;
tail = NULL;
while (head->next != tail) {
p = head;
q = p->next;
while (q->next != tail) {
if (q->score < q->next->score) {
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
}
p = p->next;
q = p->next;
}
tail = q;
}
cout << "学生信息按成绩排序成功!\n";
}
int main() {
Student* head = init();
int choice;
while (true) {
cout << "请选择操作:\n";
cout << "1. 添加学生信息\n";
cout << "2. 查询学生信息\n";
cout << "3. 修改学生信息\n";
cout << "4. 删除学生信息\n";
cout << "5. 输出学生信息\n";
cout << "6. 保存学生信息到文件\n";
cout << "7. 从文件加载学生信息\n";
cout << "8. 根据学号排序学生信息\n";
cout << "9. 根据成绩排序学生信息\n";
cout << "0. 退出程序\n";
cin >> choice;
switch (choice) {
case 1:
add(head);
break;
case 2:
query(head);
break;
case 3:
modify(head);
break;
case 4:
remove(head);
break;
case 5:
print(head);
break;
case 6:
save(head);
break;
case 7:
load(head);
break;
case 8:
sortById(head);
break;
case 9:
sortByScore(head);
break;
case 0:
return 0;
default:
cout << "无效操作!\n";
break;
}
}
}
```
这个学生信息管理系统实现了添加、查询、修改、删除、输出、保存到文件、从文件加载、根据学号排序、根据成绩排序等功能,可以通过菜单进行选择。你可以将以上代码保存为`.cpp`文件,使用DEVC++进行编译和运行。
阅读全文