学生成绩管理系统c语言
时间: 2023-06-15 10:04:23 浏览: 120
学生成绩管理系统是一个比较典型的计算机应用程序,可以帮助学校、教师和学生管理学生的成绩信息,包括课程、考试、分数、排名等等。下面是一个简单的学生成绩管理系统的C语言代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
int id;
char name[20];
double score;
} Student;
void add(Student *students, int *num) {
if (*num >= MAX_SIZE) {
printf("The student list is full.\n");
return;
}
Student student;
printf("Please enter the student ID: ");
scanf("%d", &student.id);
printf("Please enter the student name: ");
scanf("%s", student.name);
printf("Please enter the student score: ");
scanf("%lf", &student.score);
students[*num] = student;
(*num)++;
}
void search(Student *students, int num) {
int id;
printf("Please enter the student ID: ");
scanf("%d", &id);
for (int i = 0; i < num; i++) {
if (students[i].id == id) {
printf("ID: %d, Name: %s, Score: %.2lf\n", students[i].id, students[i].name, students[i].score);
return;
}
}
printf("The student is not found.\n");
}
void print(Student *students, int num) {
printf("ID\tName\tScore\n");
for (int i = 0; i < num; i++) {
printf("%d\t%s\t%.2lf\n", students[i].id, students[i].name, students[i].score);
}
}
void sort(Student *students, int num) {
for (int i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (students[i].score < students[j].score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
int main() {
Student students[MAX_SIZE];
int num = 0;
while (1) {
printf("Please select the operation:\n");
printf("1. Add student\n");
printf("2. Search student\n");
printf("3. Print all students\n");
printf("4. Sort by score\n");
printf("5. Exit\n");
printf("Your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add(students, &num);
break;
case 2:
search(students, num);
break;
case 3:
print(students, num);
break;
case 4:
sort(students, num);
break;
case 5:
exit(0);
default:
printf("Invalid choice.\n");
break;
}
printf("\n");
}
return 0;
}
```
这个示例程序实现了四个基本功能:添加学生、搜索学生、打印所有学生、按成绩排序。在程序中,我们使用了结构体来表示学生信息,使用了指针来动态修改学生列表的大小。当然,这个程序还有很多可以优化和改进的地方,比如增加文件读写功能、使用链表来存储学生列表等等。
阅读全文