简单的学生成绩管理c++
时间: 2023-10-02 20:12:14 浏览: 36
以下是一个简单的学生成绩管理系统的 C++ 代码示例,包括学生信息、课程信息和成绩信息的录入、查询和统计:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
int id;
string name;
string gender;
string phone;
string address;
};
struct Course {
int id;
string name;
int credit;
string teacher;
};
struct Score {
int student_id;
int course_id;
int score;
};
vector<Student> students;
vector<Course> courses;
vector<Score> scores;
void add_student() {
Student student;
cout << "请输入学生信息:" << endl;
cout << "学号:";
cin >> student.id;
cout << "姓名:";
cin >> student.name;
cout << "性别:";
cin >> student.gender;
cout << "电话:";
cin >> student.phone;
cout << "地址:";
cin >> student.address;
students.push_back(student);
cout << "添加成功!" << endl;
}
void add_course() {
Course course;
cout << "请输入课程信息:" << endl;
cout << "课程编号:";
cin >> course.id;
cout << "课程名称:";
cin >> course.name;
cout << "学分:";
cin >> course.credit;
cout << "任课教师:";
cin >> course.teacher;
courses.push_back(course);
cout << "添加成功!" << endl;
}
void add_score() {
Score score;
cout << "请输入成绩信息:" << endl;
cout << "学号:";
cin >> score.student_id;
cout << "课程编号:";
cin >> score.course_id;
cout << "成绩:";
cin >> score.score;
scores.push_back(score);
cout << "添加成功!" << endl;
}
void show_students() {
cout << "学号\t姓名\t性别\t电话\t地址" << endl;
for (auto it = students.begin(); it != students.end(); it++) {
cout << it->id << "\t" << it->name << "\t" << it->gender << "\t" << it->phone << "\t" << it->address << endl;
}
}
void show_courses() {
cout << "课程编号\t课程名称\t学分\t任课教师" << endl;
for (auto it = courses.begin(); it != courses.end(); it++) {
cout << it->id << "\t" << it->name << "\t" << it->credit << "\t" << it->teacher << endl;
}
}
void show_scores() {
cout << "学号\t课程编号\t成绩" << endl;
for (auto it = scores.begin(); it != scores.end(); it++) {
cout << it->student_id << "\t" << it->course_id << "\t" << it->score << endl;
}
}
void show_score_statistics() {
int course_id;
cout << "请输入课程编号:";
cin >> course_id;
int total_score = 0;
int count = 0;
for (auto it = scores.begin(); it != scores.end(); it++) {
if (it->course_id == course_id) {
total_score += it->score;
count++;
}
}
if (count > 0) {
float average_score = (float)total_score / count;
cout << "课程平均成绩:" << average_score << endl;
} else {
cout << "没有该课程的成绩信息!" << endl;
}
}
int main() {
int choice;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 添加课程" << endl;
cout << "3. 添加成绩" << endl;
cout << "4. 查看学生列表" << endl;
cout << "5. 查看课程列表" << endl;
cout << "6. 查看成绩列表" << endl;
cout << "7. 查看课程平均成绩" << endl;
cout << "8. 退出程序" << endl;
cout << "请输入操作编号:";
cin >> choice;
switch (choice) {
case 1:
add_student();
break;
case 2:
add_course();
break;
case 3:
add_score();
break;
case 4:
show_students();
break;
case 5:
show_courses();
break;
case 6:
show_scores();
break;
case 7:
show_score_statistics();
break;
case 8:
return 0;
default:
cout << "无效操作!" << endl;
break;
}
}
return 0;
}
```
以上代码中包含了学生、课程和成绩信息的结构体定义,以及添加、查看和统计成绩的函数实现。在主函数中使用一个循环来实现用户选择操作的功能,用户可以根据需要选择添加学生、添加课程、添加成绩、查看学生列表、查看课程列表、查看成绩列表、查看课程平均成绩或退出程序。
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)