选取一个合适的数据结构存储数据,设计一学生成绩表,能对数据进行插入、 删除、统计总分、按总分从大到小排序、查找等。 参考程序如下:c语言
时间: 2023-12-20 19:06:54 浏览: 137
好的,以下是用C语言实现的学生成绩表,使用结构体来存储学生信息,包含姓名和各科成绩的数组。可以通过动态内存分配来实现插入和删除操作。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_SUBJECTS 5
typedef struct {
char name[MAX_NAME_LEN];
int scores[MAX_SUBJECTS];
} Student;
typedef struct {
Student *students;
int size;
int capacity;
} ScoreTable;
void init_score_table(ScoreTable *table) {
table->students = NULL;
table->size = 0;
table->capacity = 0;
}
void add_student(ScoreTable *table, char *name, int *scores) {
if (table->size == table->capacity) {
table->capacity += 10;
table->students = realloc(table->students, table->capacity * sizeof(Student));
}
strcpy(table->students[table->size].name, name);
memcpy(table->students[table->size].scores, scores, MAX_SUBJECTS * sizeof(int));
table->size++;
}
void remove_student(ScoreTable *table, char *name) {
int i, j;
for (i = 0; i < table->size; i++) {
if (strcmp(table->students[i].name, name) == 0) {
for (j = i; j < table->size - 1; j++) {
table->students[j] = table->students[j+1];
}
table->size--;
return;
}
}
}
int get_total_score(ScoreTable *table, char *name) {
int i;
for (i = 0; i < table->size; i++) {
if (strcmp(table->students[i].name, name) == 0) {
int total = 0;
int j;
for (j = 0; j < MAX_SUBJECTS; j++) {
total += table->students[i].scores[j];
}
return total;
}
}
return -1;
}
int cmp(const void *a, const void *b) {
int total_a = 0, total_b = 0;
int i;
for (i = 0; i < MAX_SUBJECTS; i++) {
total_a += ((Student*)a)->scores[i];
total_b += ((Student*)b)->scores[i];
}
return total_b - total_a;
}
void sort_by_total_score(ScoreTable *table) {
qsort(table->students, table->size, sizeof(Student), cmp);
}
int search_student(ScoreTable *table, char *name, int *scores) {
int i;
for (i = 0; i < table->size; i++) {
if (strcmp(table->students[i].name, name) == 0) {
memcpy(scores, table->students[i].scores, MAX_SUBJECTS * sizeof(int));
return 1;
}
}
return 0;
}
int main() {
ScoreTable table;
init_score_table(&table);
int scores1[MAX_SUBJECTS] = {90, 80, 85, 75, 95};
add_student(&table, "Tom", scores1);
int scores2[MAX_SUBJECTS] = {85, 75, 70, 80, 90};
add_student(&table, "Jack", scores2);
int scores3[MAX_SUBJECTS] = {95, 90, 80, 85, 95};
add_student(&table, "Lucy", scores3);
int scores4[MAX_SUBJECTS] = {80, 85, 90, 75, 80};
add_student(&table, "Lily", scores4);
printf("Before sorting:\n");
int i;
for (i = 0; i < table.size; i++) {
printf("%s: %d %d %d %d %d\n", table.students[i].name,
table.students[i].scores[0], table.students[i].scores[1],
table.students[i].scores[2], table.students[i].scores[3],
table.students[i].scores[4]);
}
sort_by_total_score(&table);
printf("After sorting:\n");
for (i = 0; i < table.size; i++) {
printf("%s: %d %d %d %d %d\n", table.students[i].name,
table.students[i].scores[0], table.students[i].scores[1],
table.students[i].scores[2], table.students[i].scores[3],
table.students[i].scores[4]);
}
printf("Total score of Lucy: %d\n", get_total_score(&table, "Lucy"));
int scores5[MAX_SUBJECTS];
if (search_student(&table, "Jack", scores5)) {
printf("Scores of Jack: %d %d %d %d %d\n", scores5[0], scores5[1], scores5[2], scores5[3], scores5[4]);
} else {
printf("Jack not found\n");
}
remove_student(&table, "Tom");
printf("After removing Tom:\n");
for (i = 0; i < table.size; i++) {
printf("%s: %d %d %d %d %d\n", table.students[i].name,
table.students[i].scores[0], table.students[i].scores[1],
table.students[i].scores[2], table.students[i].scores[3],
table.students[i].scores[4]);
}
free(table.students);
return 0;
}
```
这个程序实现了以下功能:
- `init_score_table()`:初始化成绩表。
- `add_student()`:向学生成绩表中添加一个学生的成绩。
- `remove_student()`:从学生成绩表中删除一个学生的成绩。
- `get_total_score()`:获取一个学生的总分。
- `sort_by_total_score()`:按学生总分从大到小排序。
- `search_student()`:查找一个学生的成绩。
您可以根据自己的需求在这个基础上进行修改和扩展。
阅读全文