学生信息管理系统c语言程序设计代码
时间: 2023-09-11 10:12:05 浏览: 163
好的,这里提供一份简单的学生信息管理系统C语言程序设计代码,希望能够对您有帮助:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 1000
typedef struct {
char name[20];
char id[10];
char className[10];
int score;
} Student;
void addStudent(Student *students, int *num);
void deleteStudent(Student *students, int *num);
void modifyStudent(Student *students, int num);
void queryStudents(Student *students, int num);
void sortStudents(Student *students, int num);
int main() {
Student students[MAX_STUDENT_NUM];
int num = 0;
int choice;
while (1) {
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student information\n");
printf("4. Query student information\n");
printf("5. Sort student by score\n");
printf("0. Quit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &num);
break;
case 2:
deleteStudent(students, &num);
break;
case 3:
modifyStudent(students, num);
break;
case 4:
queryStudents(students, num);
break;
case 5:
sortStudents(students, num);
break;
case 0:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
void addStudent(Student *students, int *num) {
if (*num >= MAX_STUDENT_NUM) {
printf("Exceed maximum number of students\n");
return;
}
printf("Please enter student name: ");
scanf("%s", students[*num].name);
printf("Please enter student ID: ");
scanf("%s", students[*num].id);
printf("Please enter student class name: ");
scanf("%s", students[*num].className);
printf("Please enter student score: ");
scanf("%d", &students[*num].score);
(*num)++;
printf("Successfully added a student\n");
}
void deleteStudent(Student *students, int *num) {
char id[10];
int i, j;
printf("Please enter the ID of the student to be deleted: ");
scanf("%s", id);
for (i = 0; i < *num; i++) {
if (strcmp(students[i].id, id) == 0) {
for (j = i; j < *num - 1; j++) {
students[j] = students[j + 1];
}
(*num)--;
printf("Successfully deleted the student\n");
return;
}
}
printf("Cannot find the student with the ID\n");
}
void modifyStudent(Student *students, int num) {
char id[10];
int i;
printf("Please enter the ID of the student to be modified: ");
scanf("%s", id);
for (i = 0; i < num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Please enter the new student name: ");
scanf("%s", students[i].name);
printf("Please enter the new student class name: ");
scanf("%s", students[i].className);
printf("Please enter the new student score: ");
scanf("%d", &students[i].score);
printf("Successfully modified the student information\n");
return;
}
}
printf("Cannot find the student with the ID\n");
}
void queryStudents(Student *students, int num) {
char id[10];
int i;
printf("Please enter the ID of the student to be queried (enter 0 to list all students): ");
scanf("%s", id);
if (strcmp(id, "0") == 0) {
printf("Name\t\tID\t\tClass\t\tScore\n");
for (i = 0; i < num; i++) {
printf("%-16s%-12s%-12s%d\n", students[i].name, students[i].id, students[i].className, students[i].score);
}
} else {
for (i = 0; i < num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Name\t\tID\t\tClass\t\tScore\n");
printf("%-16s%-12s%-12s%d\n", students[i].name, students[i].id, students[i].className, students[i].score);
return;
}
}
printf("Cannot find the student with the ID\n");
}
}
void sortStudents(Student *students, int num) {
int i, j;
Student temp;
for (i = 0; i < num - 1; i++) {
for (j = 0; j < num - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("Successfully sorted the students by score\n");
}
```
该程序实现了以下功能:
1. 添加学生信息
2. 删除学生信息
3. 修改学生信息
4. 查询学生信息
5. 按照成绩排序学生信息
注意事项:
1. 该程序只是一个简单的示例,实际情况下需要根据具体需求进行修改和完善。
2. 该程序中使用了结构体来存储学生信息,可以根据需要进行修改。
3. 该程序中使用了一个宏定义来限制最大学生数量,可以根据需要进行修改。
阅读全文