使用整型数组保存30名学生的学号,6门课程的编号,以及对应的成绩,使 用字符数组保存这30名学生学号和姓名,使用字符数组保存这6门课程编号对应 的课程名称,能通过学号和课程号将三个数组进行关联。能够按学号或姓名查询 所有课程的成绩,能添加/删除/更新学生各门课程的成绩记录:能够修改指定姓 名或学号的学生的课程成绩;显示指定课程编号输出60分以下、60-79、80-89 90分以上各分数段的学生信息。。 要求: ①提供系统操作的主界面: 2通过学号查询该学生所有课程的总分和平均分 通过课程编号查询该门课程的平均分,大于平均分的学生姓名 ④自行设计相关功能: 写对不同的功能设计不同的函数,完成函数的级计和调用。
时间: 2023-06-19 21:08:58 浏览: 109
实现这个系统,需要设计以下几个函数:
1. `void init()`:初始化学生学号、课程编号和成绩数组,以及学生姓名和课程名称数组。
2. `void addStudent()`:添加学生信息,包括学号和姓名。
3. `void deleteStudent()`:删除指定学生信息,包括学号、姓名和成绩记录。
4. `void updateScore()`:更新指定学生指定课程的成绩记录。
5. `void queryScoreByStudent()`:按学生学号或姓名查询所有课程的成绩。
6. `void queryScoreByCourse()`:按课程编号查询该门课程的平均分和大于平均分的学生姓名。
7. `void queryScoreByGrade()`:按课程编号查询该门课程的成绩分数段和对应学生信息。
下面是一个可能的实现:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_STUDENT_NUM = 30;
const int MAX_COURSE_NUM = 6;
const int SCORE_BOUNDARIES[3] = {60, 80, 90};
int studentIds[MAX_STUDENT_NUM];
int courseIds[MAX_COURSE_NUM];
int scores[MAX_STUDENT_NUM][MAX_COURSE_NUM];
char studentNames[MAX_STUDENT_NUM][20];
char courseNames[MAX_COURSE_NUM][20];
int studentNum = 0;
int courseNum = 0;
void init() {
// 初始化学生学号、课程编号和成绩数组
for (int i = 0; i < MAX_STUDENT_NUM; i++) {
studentIds[i] = -1;
for (int j = 0; j < MAX_COURSE_NUM; j++) {
scores[i][j] = -1;
}
}
for (int i = 0; i < MAX_COURSE_NUM; i++) {
courseIds[i] = -1;
}
// 初始化学生姓名和课程名称数组
strcpy(studentNames[0], "张三");
strcpy(studentNames[1], "李四");
strcpy(studentNames[2], "王五");
strcpy(courseNames[0], "数学");
strcpy(courseNames[1], "语文");
strcpy(courseNames[2], "英语");
// 初始化学生学号和课程编号
studentIds[0] = 1001;
studentIds[1] = 1002;
studentIds[2] = 1003;
courseIds[0] = 2001;
courseIds[1] = 2002;
courseIds[2] = 2003;
studentNum = 3;
courseNum = 3;
}
int findStudentIndexById(int id) {
for (int i = 0; i < studentNum; i++) {
if (studentIds[i] == id) {
return i;
}
}
return -1;
}
int findStudentIndexByName(const char* name) {
for (int i = 0; i < studentNum; i++) {
if (strcmp(studentNames[i], name) == 0) {
return i;
}
}
return -1;
}
int findCourseIndexById(int id) {
for (int i = 0; i < courseNum; i++) {
if (courseIds[i] == id) {
return i;
}
}
return -1;
}
void addStudent() {
if (studentNum == MAX_STUDENT_NUM) {
cout << "学生人数已达上限,无法添加。" << endl;
return;
}
int id;
char name[20];
cout << "请输入学号:";
cin >> id;
// 检查学号是否已存在
if (findStudentIndexById(id) != -1) {
cout << "该学号已存在,请重新输入。" << endl;
return;
}
cout << "请输入姓名:";
cin >> name;
// 添加学号和姓名
studentIds[studentNum] = id;
strcpy(studentNames[studentNum], name);
studentNum++;
cout << "添加成功。" << endl;
}
void deleteStudent() {
int id;
char name[20];
cout << "请输入要删除的学生的学号或姓名:";
cin >> name;
// 先按学号查找,再按姓名查找
int index = findStudentIndexById(atoi(name));
if (index == -1) {
index = findStudentIndexByName(name);
}
if (index == -1) {
cout << "未找到该学生。" << endl;
return;
}
// 删除学生信息
studentIds[index] = -1;
strcpy(studentNames[index], "");
// 删除成绩记录
for (int i = 0; i < courseNum; i++) {
scores[index][i] = -1;
}
cout << "删除成功。" << endl;
}
void updateScore() {
int id;
char name[20];
int courseId;
int score;
cout << "请输入要修改成绩的学生的学号或姓名:";
cin >> name;
// 先按学号查找,再按姓名查找
int index = findStudentIndexById(atoi(name));
if (index == -1) {
index = findStudentIndexByName(name);
}
if (index == -1) {
cout << "未找到该学生。" << endl;
return;
}
cout << "请输入要修改成绩的课程编号:";
cin >> courseId;
// 查找课程编号的索引
int courseIndex = findCourseIndexById(courseId);
if (courseIndex == -1) {
cout << "未找到该课程。" << endl;
return;
}
cout << "请输入新的成绩:";
cin >> score;
scores[index][courseIndex] = score;
cout << "修改成功。" << endl;
}
void queryScoreByStudent() {
int id;
char name[20];
cout << "请输入要查询成绩的学生的学号或姓名:";
cin >> name;
// 先按学号查找,再按姓名查找
int index = findStudentIndexById(atoi(name));
if (index == -1) {
index = findStudentIndexByName(name);
}
if (index == -1) {
cout << "未找到该学生。" << endl;
return;
}
cout << "学号:" << studentIds[index] << ",姓名:" << studentNames[index] << endl;
for (int i = 0; i < courseNum; i++) {
cout << courseNames[i] << ":" << scores[index][i] << endl;
}
}
void queryScoreByCourse() {
int courseId;
cout << "请输入要查询成绩的课程编号:";
cin >> courseId;
// 查找课程编号的索引
int courseIndex = findCourseIndexById(courseId);
if (courseIndex == -1) {
cout << "未找到该课程。" << endl;
return;
}
int sum = 0;
int count = 0;
// 计算平均分
for (int i = 0; i < studentNum; i++) {
if (scores[i][courseIndex] != -1) {
sum += scores[i][courseIndex];
count++;
}
}
if (count == 0) {
cout << "该课程还没有成绩记录。" << endl;
return;
}
double average = (double)sum / count;
cout << "平均分:" << average << endl;
cout << "成绩大于平均分的学生姓名:" << endl;
for (int i = 0; i < studentNum; i++) {
if (scores[i][courseIndex] > average) {
cout << studentNames[i] << endl;
}
}
}
void queryScoreByGrade() {
int courseId;
cout << "请输入要查询成绩的课程编号:";
cin >> courseId;
// 查找课程编号的索引
int courseIndex = findCourseIndexById(courseId);
if (courseIndex == -1) {
cout << "未找到该课程。" << endl;
return;
}
int count[3] = {0, 0, 0};
cout << "60分以下:" << endl;
for (int i = 0; i < studentNum; i++) {
if (scores[i][courseIndex] < SCORE_BOUNDARIES[0]) {
cout << "学号:" << studentIds[i] << ",姓名:" << studentNames[i] << ",成绩:" << scores[i][courseIndex] << endl;
count[0]++;
}
}
cout << "60-79分:" << endl;
for (int i = 0; i < studentNum; i++) {
if (scores[i][courseIndex] >= SCORE_BOUNDARIES[0] && scores[i][courseIndex] < SCORE_BOUNDARIES[1]) {
cout << "学号:" << studentIds[i] << ",姓名:" << studentNames[i] << ",成绩:" << scores[i][courseIndex] << endl;
count[1]++;
}
}
cout << "80-89分:" << endl;
for (int i = 0; i < studentNum; i++) {
if (scores[i][courseIndex] >= SCORE_BOUNDARIES[1] && scores[i][courseIndex] < SCORE_BOUNDARIES[2]) {
cout << "学号:" << studentIds[i] << ",姓名:" << studentNames[i] << ",成绩:" << scores[i][courseIndex] << endl;
count[2]++;
}
}
cout << "90分以上:" << endl;
for (int i = 0; i < studentNum; i++) {
if (scores[i][courseIndex] >= SCORE_BOUNDARIES[2]) {
cout << "学号:" << studentIds[i] << ",姓名:" << studentNames[i] << ",成绩:" << scores[i][courseIndex] << endl;
}
}
cout << "成绩分布:" << endl;
cout << "60分以下:" << count[0] << "人," << (double)count[0] / studentNum * 100 << "%" << endl;
cout << "60-79分:" << count[1] << "人," << (double)count[1] / studentNum * 100 << "%" << endl;
cout << "80-89分:" << count[2] << "人," << (double)count[2] / studentNum * 100 << "%" << endl;
cout << "90分以上:" << studentNum - count[0] - count[1] - count[2] << "人," << (double)(studentNum - count[0] - count[1] - count[2]) / studentNum * 100 << "%" << endl;
}
int main() {
init();
int choice;
do {
cout << "请选择操作:" << endl;
cout << "1. 添加学生信息" << endl;
cout << "2. 删除学生信息" << endl;
cout << "3. 更新学生成绩" << endl;
cout << "4. 按学号或姓名查询学生成绩" << endl;
cout << "5. 按课程编号查询课程成绩" << endl;
cout << "6. 按课程编号查询成绩分布" << endl;
cout << "0. 退出" << endl;
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
updateScore();
break;
case 4:
queryScoreByStudent();
break;
case 5:
queryScoreByCourse();
break;
case 6:
queryScoreByGrade();
break;
case 0:
break;
default:
cout << "无效的操作,请重新选择。" << endl;
break;
}
} while (choice != 0);
return 0;
}
```
阅读全文