学生成绩管理系统c++用while switch
时间: 2023-12-19 15:06:47 浏览: 94
学生成绩管理系统是一种用C++编写的程序,它可以帮助学校或老师管理学生的成绩信息。该程序通常包括以下功能:添加学生信息、删除学生信息、修改学生信息、查询学生信息、统计学生总数、计算学生平均分等。在实现这些功能时,通常会使用while循环和switch语句来实现菜单的显示和用户输入的处理。通过while循环,程序可以一直运行,直到用户选择退出程序。而通过switch语句,程序可以根据用户的选择执行相应的操作。在具体实现时,还需要使用链表等数据结构来存储学生信息,同时也需要使用指针等C++语言的特性来实现各种操作。
相关问题
学生成绩管理系统c++
好的,你需要一个学生成绩管理系统的C++代码。以下是一个简单的示例:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
int id;
vector<int> grades;
};
vector<Student> students;
void addStudent() {
Student student;
cout << "Enter student name: ";
cin >> student.name;
cout << "Enter student ID: ";
cin >> student.id;
students.push_back(student);
}
void addGrade() {
int id, grade;
cout << "Enter student ID: ";
cin >> id;
cout << "Enter grade: ";
cin >> grade;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students[i].grades.push_back(grade);
return;
}
}
cout << "Student with ID " << id << " not found!" << endl;
}
void printStudent(Student student) {
cout << "Name: " << student.name << endl;
cout << "ID: " << student.id << endl;
cout << "Grades: ";
for (int i = 0; i < student.grades.size(); i++) {
cout << student.grades[i] << " ";
}
cout << endl;
}
void printStudents() {
for (int i = 0; i < students.size(); i++) {
printStudent(students[i]);
}
}
int main() {
int choice;
cout << "Student Grade Management System" << endl;
cout << "1. Add Student" << endl;
cout << "2. Add Grade" << endl;
cout << "3. Print Students" << endl;
cout << "4. Quit" << endl;
do {
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
addGrade();
break;
case 3:
printStudents();
break;
case 4:
cout << "Goodbye!" << endl;
break;
default:
cout << "Invalid choice!" << endl;
break;
}
} while (choice != 4);
return 0;
}
```
这个程序使用一个 `Student` 结构体来表示学生,包含名称、ID和成绩。然后使用一个 `vector` 来存储所有学生。主函数提供了以下功能:
- 添加学生
- 添加成绩
- 打印所有学生
- 退出程序
你可以根据需要修改代码以满足任何其他要求。
学生成绩管理系统c++ 链表
### C++ 实现学生成绩管理系统的链表
#### 定义学生节点结构体
为了构建一个基于链表的学生信息管理系统,首先需要定义表示单个学生的数据结构。这里采用 `struct` 来创建名为 `StudentNode` 的节点。
```cpp
#include <iostream>
#include <string>
using namespace std;
struct StudentNode {
string name;
int id;
float score;
StudentNode* next;
StudentNode(string n, int i, float s) : name(n), id(i), score(s), next(nullptr) {}
};
```
#### 创建链表操作函数
接下来编写一些基本的操作来维护这个链表,比如插入新记录、遍历显示所有记录以及计算总人数等功能。
##### 插入新的学生信息到链表头部
此方法允许向现有列表前端添加一个新的学生条目。
```cpp
void insertAtHead(StudentNode*& head, const string& name, int id, float score) {
StudentNode* newNode = new StudentNode(name, id, score);
newNode->next = head;
head = newNode;
}
```
##### 统计当前链表中的记录总数
该功能用于获取整个链表内存储了多少位同学的信息。
```cpp
int ListCount(const StudentNode* head) { // 统计当前链表的记录总数,返回一个整数 [^1]
int count = 0;
while (head != nullptr) {
++count;
head = head->next;
}
return count;
}
```
##### 显示全部学生信息
通过循环访问每一个节点并打印其内容可以完成这一任务。
```cpp
void displayAll(const StudentNode* head) {
cout << "ID\tName\tScore\n";
while (head != nullptr) {
cout << head->id << "\t" << head->name << "\t" << head->score << endl;
head = head->next;
}
}
```
#### 主程序框架
最后,在主函数中初始化链表头指针,并提供菜单让用户选择不同的操作选项。
```cpp
int main() {
StudentNode* studentList = nullptr;
char choice;
do {
cout << "(A)dd a record | (D)isplay all records | (Q)uit: ";
cin >> choice;
switch(tolower(choice)) {
case 'a': {
string name;
int id;
float score;
cout << "Enter Name: "; cin.ignore(); getline(cin, name);
cout << "Enter ID: "; cin >> id;
cout << "Enter Score: "; cin >> score;
insertAtHead(studentList, name, id, score);
break;
}
case 'd':
displayAll(studentList);
break;
default:
if (tolower(choice) != 'q') cout << "Invalid option.\n";
}
} while (tolower(choice) != 'q');
// 清理内存资源
while (studentList != nullptr) {
StudentNode* temp = studentList;
studentList = studentList->next;
delete temp;
}
return 0;
}
```
阅读全文
相关推荐











