编写一段代码:录入每个学生的学号、姓名和各科成绩;分别计算每门课程及每个学生的总分和平均分;按照总分进行升序或者降序排序;按照学号升序排序;插入学生一条记录;按照姓名查找,并删除一条记录;查找并显示一条信息;输出每个学生的学号、姓名、各科成绩、总分和分均分
时间: 2023-12-16 19:18:32 浏览: 116
下面是一份基于C语言的简单学生成绩管理系统代码,包括了录入学生信息、计算总分和平均分、排序、插入、查找、删除和输出等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
#define MAX_NAME_LEN 20
#define MAX_SUBJECT_NUM 5
typedef struct {
char id[10];
char name[MAX_NAME_LEN];
int score[MAX_SUBJECT_NUM];
int total_score;
float avg_score;
} Student;
Student students[MAX_STUDENT_NUM];
int student_count = 0;
int cmp_by_total_score_desc(const void *a, const void *b) {
const Student *sa = (const Student *)a;
const Student *sb = (const Student *)b;
return sb->total_score - sa->total_score;
}
int cmp_by_total_score_asc(const void *a, const void *b) {
const Student *sa = (const Student *)a;
const Student *sb = (const Student *)b;
return sa->total_score - sb->total_score;
}
int cmp_by_id_asc(const void *a, const void *b) {
const Student *sa = (const Student *)a;
const Student *sb = (const Student *)b;
return strcmp(sa->id, sb->id);
}
int find_student_by_name(const char *name) {
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].name, name) == 0) {
return i;
}
}
return -1;
}
void add_student() {
if (student_count >= MAX_STUDENT_NUM) {
printf("Error: student list is full\n");
return;
}
printf("Input student id: ");
scanf("%s", students[student_count].id);
printf("Input student name: ");
scanf("%s", students[student_count].name);
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
printf("Input score of subject %d: ", i + 1);
scanf("%d", &students[student_count].score[i]);
students[student_count].total_score += students[student_count].score[i];
}
students[student_count].avg_score = (float)students[student_count].total_score / MAX_SUBJECT_NUM;
student_count++;
printf("Add student successfully\n");
}
void delete_student() {
char name[MAX_NAME_LEN];
printf("Input name of the student to be deleted: ");
scanf("%s", name);
int index = find_student_by_name(name);
if (index == -1) {
printf("Error: student not found\n");
return;
}
for (int i = index; i < student_count - 1; i++) {
students[i] = students[i + 1];
}
student_count--;
printf("Delete student successfully\n");
}
void search_student() {
char name[MAX_NAME_LEN];
printf("Input name of the student to be searched: ");
scanf("%s", name);
int index = find_student_by_name(name);
if (index == -1) {
printf("Error: student not found\n");
return;
}
printf("Student found:\n");
printf("ID: %s\n", students[index].id);
printf("Name: %s\n", students[index].name);
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
printf("Subject %d: %d\n", i + 1, students[index].score[i]);
}
printf("Total score: %d\n", students[index].total_score);
printf("Average score: %.2f\n", students[index].avg_score);
}
void sort_by_total_score() {
char order;
printf("Sort by total score in ascending or descending order? (a/d): ");
scanf(" %c", &order);
if (order == 'a') {
qsort(students, student_count, sizeof(Student), cmp_by_total_score_asc);
printf("Sort by total score in ascending order successfully\n");
}
else if (order == 'd') {
qsort(students, student_count, sizeof(Student), cmp_by_total_score_desc);
printf("Sort by total score in descending order successfully\n");
}
else {
printf("Error: invalid order\n");
}
}
void sort_by_id() {
qsort(students, student_count, sizeof(Student), cmp_by_id_asc);
printf("Sort by ID in ascending order successfully\n");
}
void print_students() {
printf("Students:\n");
for (int i = 0; i < student_count; i++) {
printf("ID: %s\n", students[i].id);
printf("Name: %s\n", students[i].name);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("Subject %d: %d\n", j + 1, students[i].score[j]);
}
printf("Total score: %d\n", students[i].total_score);
printf("Average score: %.2f\n", students[i].avg_score);
}
}
void print_menu() {
printf("\n");
printf("1. Add a student\n");
printf("2. Delete a student\n");
printf("3. Search a student\n");
printf("4. Sort by total score\n");
printf("5. Sort by ID\n");
printf("6. Print students\n");
printf("0. Exit\n");
}
int main() {
int choice;
while (1) {
print_menu();
printf("Input your choice: ");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("Goodbye!\n");
return 0;
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
search_student();
break;
case 4:
sort_by_total_score();
break;
case 5:
sort_by_id();
break;
case 6:
print_students();
break;
default:
printf("Error: invalid choice\n");
break;
}
}
}
```
注意,这只是一个简单的示例代码,实际的学生成绩管理系统可能需要更加完善和复杂的功能,也需要更加严谨和安全的数据处理和算法实现。如果你需要使用这个代码作为参考或者基础,请自行理解和修改,以符合你的具体需求和要求。
阅读全文