请你用c语言写一个学生信息管理系统包括查询,添加,删除,分数排序
时间: 2023-04-07 16:03:50 浏览: 87
用C语言写的 学生信息管理系统
好的,我可以回答这个问题。以下是一个简单的学生信息管理系统的C语言代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[50];
int id;
float score;
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
return;
}
struct student s;
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student score: ");
scanf("%f", &s.score);
students[num_students] = s;
num_students++;
}
void delete_student() {
int id;
printf("Enter student ID to delete: ");
scanf("%d", &id);
int i;
for (i = 0; i < num_students; i++) {
if (students[i].id == id) {
// shift all students after this one back by one
int j;
for (j = i; j < num_students - 1; j++) {
students[j] = students[j+1];
}
num_students--;
printf("Deleted student with ID %d\n", id);
return;
}
}
printf("Error: student with ID %d not found\n", id);
}
void print_students() {
int i;
for (i = 0; i < num_students; i++) {
printf("%s (ID %d): %.2f\n", students[i].name, students[i].id, students[i].score);
}
}
void sort_students() {
int i, j;
for (i = 0; i < num_students - 1; i++) {
for (j = i + 1; j < num_students; j++) {
if (students[i].score < students[j].score) {
// swap students i and j
struct student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
int main() {
int choice;
while (1) {
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Print students\n");
printf("4. Sort students by score\n");
printf("5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
print_students();
break;
case 4:
sort_students();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
阅读全文