如何用C语言设计一个成绩管理系统
时间: 2023-05-13 17:04:35 浏览: 53
一个基本的成绩管理系统可以包括以下功能:
1. 添加学生信息
2. 添加课程信息
3. 添加成绩信息
4. 修改成绩信息
5. 删除成绩信息
6. 查询成绩信息
以下是一个简单的 C 代码示例,用于实现上述功能:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_COURSES 10
typedef struct {
char name[20];
int id;
} Student;
typedef struct {
char name[20];
int id;
} Course;
typedef struct {
int student_id;
int course_id;
float score;
} Score;
Student students[MAX_STUDENTS];
Course courses[MAX_COURSES];
Score scores[MAX_STUDENTS * MAX_COURSES];
int num_students = 0;
int num_courses = 0;
int num_scores = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
return;
}
printf("Enter student name: ");
scanf("%s", students[num_students].name);
printf("Enter student ID: ");
scanf("%d", &students[num_students].id);
num_students++;
}
void add_course() {
if (num_courses >= MAX_COURSES) {
printf("Error: too many courses\n");
return;
}
printf("Enter course name: ");
scanf("%s", courses[num_courses].name);
printf("Enter course ID: ");
scanf("%d", &courses[num_courses].id);
num_courses++;
}
void add_score() {
int student_id, course_id;
float score;
printf("Enter student ID: ");
scanf("%d", &student_id);
printf("Enter course ID: ");
scanf("%d", &course_id);
printf("Enter score: ");
scanf("%f", &score);
scores[num_scores].student_id = student_id;
scores[num_scores].course_id = course_id;
scores[num_scores].score = score;
num_scores++;
}
void modify_score() {
int student_id, course_id;
float score;
printf("Enter student ID: ");
scanf("%d", &student_id);
printf("Enter course ID: ");
scanf("%d", &course_id);
printf("Enter new score: ");
scanf("%f", &score);
for (int i = 0; i < num_scores; i++) {
if (scores[i].student_id == student_id && scores[i].course_id == course_id) {
scores[i].score = score;
return;
}
}
printf("Error: score not found\n");
}
void delete_score() {
int student_id, course_id;
printf("Enter student ID: ");
scanf("%d", &student_id);
printf("Enter course ID: ");
scanf("%d", &course_id);
for (int i = 0; i < num_scores; i++) {
if (scores[i].student_id == student_id && scores[i].course_id == course_id) {
scores[i] = scores[num_scores - 1];
num_scores--;
return;
}
}
printf("Error: score not found\n");
}
void query_score() {
int student_id, course_id;
printf("Enter student ID: ");
scanf("%d", &student_id);
printf("Enter course ID: ");
scanf("%d", &course_id);
for (int i = 0; i < num_scores; i++) {
if (scores[i].student_id == student_id && scores[i].course_id == course_id) {
printf("Score: %.2f\n", scores[i].score);
return;
}
}
printf("Error: score not found\n");
}
int main() {
int choice;
while (1) {
printf("\n");
printf("1. Add student\n");
printf("2. Add course\n");
printf("3. Add score\n");
printf("4. Modify score\n");
printf("5. Delete score\n");
printf("6. Query score\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
add_course();
break;
case 3:
add_score();
break;
case 4:
modify_score();
break;
case 5:
delete_score();
break;
case 6:
query_score();
break;
case 7:
exit(0);
default:
printf("Error: invalid choice\n");
}
}
return 0;
}
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)