好的,这是一个比较复杂的系统设计,需要考虑到各种情况,下面是我的建议:
(1)数据逻辑结构分析:
考虑到学生成绩查询统计系统需要记录学生的个人信息以及三科成绩信息,我们可以设计如下的逻辑结构:
- 学生信息:包括学号、姓名、性别、出生日期、班级等。
- 成绩信息:包括学号、姓名、语文成绩、数学成绩、英语成绩、总分、排名等。
(2)数据存储结构设计:
我们可以使用结构体数组来存储学生信息和成绩信息,例如:
```c
struct student {
char id[10];
char name[20];
char gender[10];
char birthday[20];
char class[20];
};
struct score {
char id[10];
char name[20];
int chinese;
int math;
int english;
int total;
int rank;
};
```
然后,将这些结构体数组存储在文件中,可以使用文本文件或二进制文件来存储。
(3)根据学生姓名进行字典排序:
可以使用字符串比较函数 `strcmp()` 来进行排序,例如:
```c
int cmp_by_name(const void *a, const void *b) {
return strcmp(((struct student *)a)->name, ((struct student *)b)->name);
}
```
(4)根据成绩进行由高到低排序:
可以使用快速排序算法来进行排序,例如:
```c
int cmp_by_total(const void *a, const void *b) {
return ((struct score *)b)->total - ((struct score *)a)->total;
}
void quick_sort(struct score *scores, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
struct score pivot = scores[left];
while (i < j) {
while (i < j && scores[j].total <= pivot.total) {
j--;
}
scores[i] = scores[j];
while (i < j && scores[i].total >= pivot.total) {
i++;
}
scores[j] = scores[i];
}
scores[i] = pivot;
quick_sort(scores, left, i - 1);
quick_sort(scores, i + 1, right);
}
```
(5)根据学号或姓名查找学生成绩:
可以使用线性查找算法来进行查找,例如:
```c
void search_by_id(struct score *scores, int n, const char *id) {
for (int i = 0; i < n; i++) {
if (strcmp(scores[i].id, id) == 0) {
printf("学号:%s,姓名:%s,语文:%d,数学:%d,英语:%d,总分:%d,排名:%d\n",
scores[i].id, scores[i].name, scores[i].chinese, scores[i].math, scores[i].english, scores[i].total, scores[i].rank);
return;
}
}
printf("未找到学号为 %s 的学生\n", id);
}
void search_by_name(struct score *scores, int n, const char *name) {
for (int i = 0; i < n; i++) {
if (strcmp(scores[i].name, name) == 0) {
printf("学号:%s,姓名:%s,语文:%d,数学:%d,英语:%d,总分:%d,排名:%d\n",
scores[i].id, scores[i].name, scores[i].chinese, scores[i].math, scores[i].english, scores[i].total, scores[i].rank);
return;
}
}
printf("未找到姓名为 %s 的学生\n", name);
}
```
(6)输出统计及格、中、良、优秀和不及格学生人数及其所占比例:
可以先按照总分排序,然后根据总分计算各个等级的学生人数和所占比例,例如:
```c
void print_statistics(struct score *scores, int n) {
quick_sort(scores, 0, n - 1);
int pass = 0, mid = 0, good = 0, excellent = 0, fail = 0;
for (int i = 0; i < n; i++) {
if (scores[i].total < 180) {
fail++;
} else if (scores[i].total < 240) {
pass++;
} else if (scores[i].total < 300) {
mid++;
} else if (scores[i].total < 360) {
good++;
} else {
excellent++;
}
}
printf("及格人数:%d,占比:%.2f%%\n", pass, (float)pass / n * 100);
printf("中等人数:%d,占比:%.2f%%\n", mid, (float)mid / n * 100);
printf("良好人数:%d,占比:%.2f%%\n", good, (float)good / n * 100);
printf("优秀人数:%d,占比:%.2f%%\n", excellent, (float)excellent / n * 100);
printf("不及格人数:%d,占比:%.2f%%\n", fail, (float)fail / n * 100);
for (int i = 0; i < n; i++) {
printf("学号:%s,姓名:%s,语文:%d,数学:%d,英语:%d,总分:%d,排名:%d\n",
scores[i].id, scores[i].name, scores[i].chinese, scores[i].math, scores[i].english, scores[i].total, scores[i].rank);
}
}
```
(7)主要C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char id[10];
char name[20];
char gender[10];
char birthday[20];
char class[20];
};
struct score {
char id[10];
char name[20];
int chinese;
int math;
int english;
int total;
int rank;
};
int cmp_by_name(const void *a, const void *b) {
return strcmp(((struct student *)a)->name, ((struct student *)b)->name);
}
int cmp_by_total(const void *a, const void *b) {
return ((struct score *)b)->total - ((struct score *)a)->total;
}
void quick_sort(struct score *scores, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
struct score pivot = scores[left];
while (i < j) {
while (i < j && scores[j].total <= pivot.total) {
j--;
}
scores[i] = scores[j];
while (i < j && scores[i].total >= pivot.total) {
i++;
}
scores[j] = scores[i];
}
scores[i] = pivot;
quick_sort(scores, left, i - 1);
quick_sort(scores, i + 1, right);
}
void search_by_id(struct score *scores, int n, const char *id) {
for (int i = 0; i < n; i++) {
if (strcmp(scores[i].id, id) == 0) {
printf("学号:%s,姓名:%s,语文:%d,数学:%d,英语:%d,总分:%d,排名:%d\n",
scores[i].id, scores[i].name, scores[i].chinese, scores[i].math, scores[i].english, scores[i].total, scores[i].rank);
return;
}
}
printf("未找到学号为 %s 的学生\n", id);
}
void search_by_name(struct score *scores, int n, const char *name) {
for (int i = 0; i < n; i++) {
if (strcmp(scores[i].name, name) == 0) {
printf("学号:%s,姓名:%s,语文:%d,数学:%d,英语:%d,总分:%d,排名:%d\n",
scores[i].id, scores[i].name, scores[i].chinese, scores[i].math, scores[i].english, scores[i].total, scores[i].rank);
return;
}
}
printf("未找到姓名为 %s 的学生\n", name);
}
void print_statistics(struct score *scores, int n) {
quick_sort(scores, 0, n - 1);
int pass = 0, mid = 0, good = 0, excellent = 0, fail = 0;
for (int i = 0; i < n; i++) {
if (scores[i].total < 180) {
fail++;
} else if (scores[i].total < 240) {
pass++;
} else if (scores[i].total < 300) {
mid++;
} else if (scores[i].total < 360) {
good++;
} else {
excellent++;
}
}
printf("及格人数:%d,占比:%.2f%%\n", pass, (float)pass / n * 100);
printf("中等人数:%d,占比:%.2f%%\n", mid, (float)mid / n * 100);
printf("良好人数:%d,占比:%.2f%%\n", good, (float)good / n * 100);
printf("优秀人数:%d,占比:%.2f%%\n", excellent, (float)excellent / n * 100);
printf("不及格人数:%d,占比:%.2f%%\n", fail, (float)fail / n * 100);
for (int i = 0; i < n; i++) {
printf("学号:%s,姓名:%s,语文:%d,数学:%d,英语:%d,总分:%d,排名:%d\n",
scores[i].id, scores[i].name, scores[i].chinese, scores[i].math, scores[i].english, scores[i].total, scores[i].rank);
}
}
int main() {
struct student students[1000];
struct score scores[1000];
int n = 0;
FILE *fp = fopen("data.txt", "r");
while (fscanf(fp, "%s%s%s%s%s%d%d%d",
students[n].id, students[n].name, students[n].gender, students[n].birthday, students[n].class,
&scores[n].chinese, &scores[n].math, &scores[n].english) != EOF) {
strcpy(scores[n].id, students[n].id);
strcpy(scores[n].name, students[n].name);
scores[n].total = scores[n].chinese + scores[n].math + scores[n].english;
n++;
}
fclose(fp);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(students[i].name, students[j].name) > 0) {
struct student temp_student = students[i];
students[i] = students[j];
students[j] = temp_student;
struct score temp_score = scores[i];
scores[i] = scores[j];
scores[j] = temp_score;
}
}
}
int rank = 1;
for (int i = 0; i < n; i++) {
if (i > 0 && scores[i].total != scores[i - 1].total) {
rank = i + 1;
}
scores[i].rank = rank;
}
int choice;
do {
printf("1.查询学生信息\n");
printf("2.按姓名排序\n");
printf("3.按总分排序\n");
printf("4.统计成绩信息\n");
printf("5.退出\n");
printf("请选择要进行的操作:");
scanf("%d", &choice);
switch (choice) {
case 1: {
printf("请选择查询方式(1.按学号查询,2.按姓名查询):");
int search_choice;
scanf("%d", &search_choice);
if (search_choice == 1) {
char id[10];
printf("请输入要查询的学生学号:");
scanf("%s", id);
search_by_id(scores, n, id);
} else if (search_choice == 2) {
char name[20];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
search_by_name(scores, n, name);
} else {
printf("输入有误,请重新选择\n");
}
break;
}
case 2: {
qsort(students, n, sizeof(struct student), cmp_by_name);
for (int i = 0; i < n; i++) {
printf("学号:%s,姓名:%s,性别:%s,出生日期:%s,班级:%s\n",
students[i].id, students[i].name, students[i].gender, students[i].birthday, students[i].class);
}
break;
}
case 3: {
quick_sort(scores, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("学号:%s,姓名:%s,语文:%d,数学:%d,英语:%d,总分:%d,排名:%d\n",
scores[i].id, scores[i].name, scores[i].chinese, scores[i].math, scores[i].english, scores[i].total, scores[i].rank);
}
break;
}
case 4: {
print_statistics(scores, n);
break;
}
case 5: {
printf("程序退出!\n");
break;
}
default: {
printf("输入有误,请重新选择\n");
break;
}
}
} while (choice != 5);
return 0;
}
```
(8)录入学生成绩:
可以在程序中添加录入学生成绩的功能,例如:
```c
void input_score(struct student *students, struct score *scores, int *n) {
printf("请输入学生学号、姓名、性别、出生日期、班级和三科成绩(以空格分隔):\n");
scanf("%s%s%s%s%s%d%d%d",
students[*n].id, students[*n].name, students[*n].gender, students[*n].birthday, students[*n].class,
&scores[*n].chinese, &scores[*n].math, &scores[*n].english);
strcpy(scores[*n].id, students[*n].id);
strcpy(scores[*n].name, students[*n].name);
scores[*n].total = scores[*n].chinese + scores[*n].math + scores[*n].english;
(*n)++;
printf("学生信息录入成功!\n");
}
```
(9)各个功能通过目录选择要进行的功能:
可以使用 `do-while` 循环和 `switch-case` 语句来实现菜单功能,例如:
```c
int choice;
do {
printf("1.录入学生成绩\n");
printf("2.查询学生信息\n");
printf("3.按姓名排序\n");
printf("4.按总分排序\n");
printf("5.统计成绩信息\n");
printf("6.退出\n");
printf("请选择要进行的操作:");
scanf("%d", &choice);
switch (choice) {
case 1: {
input_score(students, scores, &n);
break;
}
case 2: {
printf("请选择查询方式(1.按学号查询,2.按姓名查询):");
int search_choice;
scanf("%