用clion编写一个学生管理系统
时间: 2023-09-16 18:09:14 浏览: 243
基于c语言的学生成绩管理系统.zip
好的,以下是一个简单的学生管理系统的代码,你可以在CLion中进行编写和调试。
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name;
int id;
int age;
char gender;
double score;
Student(string n, int i, int a, char g, double s) : name(n), id(i), age(a), gender(g), score(s) {}
};
vector<Student> students;
bool compareScore(Student a, Student b) {
return a.score > b.score;
}
void addStudent() {
string name;
int id, age;
char gender;
double score;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生学号:";
cin >> id;
cout << "请输入学生年龄:";
cin >> age;
cout << "请输入学生性别(M/F):";
cin >> gender;
cout << "请输入学生成绩:";
cin >> score;
students.push_back(Student(name, id, age, gender, score));
cout << "学生添加成功!" << endl;
}
void searchStudent() {
int id;
cout << "请输入要查找的学生学号:";
cin >> id;
for (auto s : students) {
if (s.id == id) {
cout << "姓名:" << s.name << endl;
cout << "学号:" << s.id << endl;
cout << "年龄:" << s.age << endl;
cout << "性别:" << s.gender << endl;
cout << "成绩:" << s.score << endl;
return;
}
}
cout << "没有找到该学生!" << endl;
}
void deleteStudent() {
int id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (auto it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
students.erase(it);
cout << "学生已删除!" << endl;
return;
}
}
cout << "没有找到该学生!" << endl;
}
void printStudents() {
sort(students.begin(), students.end(), compareScore);
cout << "学生列表:" << endl;
for (auto s : students) {
cout << s.name << " " << s.id << " " << s.age << " " << s.gender << " " << s.score << endl;
}
}
int main() {
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 查找学生" << endl;
cout << "3. 删除学生" << endl;
cout << "4. 查看学生列表" << endl;
cout << "5. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
searchStudent();
break;
case 3:
deleteStudent();
break;
case 4:
printStudents();
break;
case 5:
return 0;
default:
cout << "无效的操作!" << endl;
break;
}
}
}
```
这个学生管理系统可以进行以下操作:
1. 添加学生
2. 查找学生
3. 删除学生
4. 查看学生列表
5. 退出程序
你可以根据需要进行修改和扩展。
阅读全文