C语言使用数组设计学生成绩管理系统,要求有输入、查询、显示、排序的功能
时间: 2023-07-10 08:23:03 浏览: 143
C语言利用结构体数组实现学生成绩管理系统
5星 · 资源好评率100%
好的,这是一个很典型的C语言数组应用的问题。下面是一个简单的学生成绩管理系统的源代码,它包含了输入、查询、显示和排序等功能,你可以参考一下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct student {
char name[20];
int score;
};
struct student students[MAX_STUDENT_NUM];
int student_num = 0;
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("too many students!\n");
return;
}
printf("please input student name and score: ");
scanf("%s %d", students[student_num].name, &students[student_num].score);
student_num++;
printf("add student success!\n");
}
void search_student() {
char name[20];
printf("please input name to search: ");
scanf("%s", name);
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("name: %s, score: %d\n", students[i].name, students[i].score);
return;
}
}
printf("student not found!\n");
}
void display_students() {
if (student_num == 0) {
printf("no students!\n");
return;
}
for (int i = 0; i < student_num; i++) {
printf("name: %s, score: %d\n", students[i].name, students[i].score);
}
}
void sort_students() {
for (int i = 0; i < student_num-1; i++) {
for (int j = i+1; j < student_num; j++) {
if (students[i].score < students[j].score) {
struct student tmp = students[i];
students[i] = students[j];
students[j] = tmp;
}
}
}
printf("sort students success!\n");
}
int main() {
while (1) {
printf("1. add student\n");
printf("2. search student\n");
printf("3. display students\n");
printf("4. sort students\n");
printf("5. exit\n");
printf("please select your operation: ");
int op;
scanf("%d", &op);
switch (op) {
case 1:
add_student();
break;
case 2:
search_student();
break;
case 3:
display_students();
break;
case 4:
sort_students();
break;
case 5:
printf("bye-bye!\n");
exit(0);
default:
printf("invalid operation!\n");
break;
}
}
return 0;
}
```
这个程序使用一个结构体数组 `students` 来存储学生的姓名和分数,最多可以存储 `MAX_STUDENT_NUM` 个学生。程序提供了四个功能:
1. `add_student()`:添加一个学生,输入学生的姓名和分数,存储到数组中;
2. `search_student()`:查询一个学生,输入学生的姓名,遍历数组找到对应的学生,输出其姓名和分数;
3. `display_students()`:显示所有学生的姓名和分数;
4. `sort_students()`:按照分数排序,从高到低输出所有学生的姓名和分数。
你可以根据需要进行修改和优化。
阅读全文