用c++写出一个成绩管理系统 要求: 1. 需要用结构体定义管理系统的基本元素,如学生,车辆等(不建议使用类,类是下学期实验课的内容) 2. 需要有增删改查的操作,可以使用数组存储、链表存储,或指针结构体等
时间: 2024-03-08 22:48:48 浏览: 21
好的,以下是一个简单的成绩管理系统的 C++ 代码,使用了结构体和链表存储:
```c++
#include <iostream>
#include <string>
using namespace std;
// 学生结构体
struct Student {
string name;
int id;
float score;
Student* next;
};
// 添加学生
void addStudent(Student*& head) {
Student* newStudent = new Student;
cout << "请输入学生姓名:";
cin >> newStudent->name;
cout << "请输入学生学号:";
cin >> newStudent->id;
cout << "请输入学生成绩:";
cin >> newStudent->score;
newStudent->next = nullptr;
if (head == nullptr) {
head = newStudent;
} else {
Student* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newStudent;
}
cout << "添加成功!" << endl;
}
// 删除学生
void deleteStudent(Student*& head, int id) {
if (head == nullptr) {
cout << "该学生不存在!" << endl;
return;
}
if (head->id == id) {
Student* temp = head;
head = head->next;
delete temp;
cout << "删除成功!" << endl;
return;
}
Student* current = head;
while (current->next != nullptr && current->next->id != id) {
current = current->next;
}
if (current->next == nullptr) {
cout << "该学生不存在!" << endl;
return;
}
Student* temp = current->next;
current->next = temp->next;
delete temp;
cout << "删除成功!" << endl;
}
// 修改学生信息
void modifyStudent(Student* head, int id) {
Student* current = head;
while (current != nullptr && current->id != id) {
current = current->next;
}
if (current == nullptr) {
cout << "该学生不存在!" << endl;
return;
}
cout << "请输入学生姓名:";
cin >> current->name;
cout << "请输入学生学号:";
cin >> current->id;
cout << "请输入学生成绩:";
cin >> current->score;
cout << "修改成功!" << endl;
}
// 查询学生信息
void queryStudent(Student* head, int id) {
Student* current = head;
while (current != nullptr && current->id != id) {
current = current->next;
}
if (current == nullptr) {
cout << "该学生不存在!" << endl;
return;
}
cout << "学生姓名:" << current->name << endl;
cout << "学生学号:" << current->id << endl;
cout << "学生成绩:" << current->score << endl;
}
int main() {
Student* head = nullptr;
int choice, id;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 删除学生" << endl;
cout << "3. 修改学生信息" << endl;
cout << "4. 查询学生信息" << endl;
cout << "5. 退出" << endl;
cin >> choice;
switch (choice) {
case 1:
addStudent(head);
break;
case 2:
cout << "请输入学生学号:";
cin >> id;
deleteStudent(head, id);
break;
case 3:
cout << "请输入学生学号:";
cin >> id;
modifyStudent(head, id);
break;
case 4:
cout << "请输入学生学号:";
cin >> id;
queryStudent(head, id);
break;
case 5:
return 0;
default:
cout << "无效操作!" << endl;
break;
}
}
return 0;
}
```
该系统使用链表存储学生信息,每个学生信息包括姓名、学号和成绩。可以进行添加、删除、修改和查询操作。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)