C语言设置一个学生系统,可以学生成绩录入,学生成绩信息删除,学生成绩修改,学生成绩信息查找,学生信息插入,统计学生人数
时间: 2024-04-02 12:36:09 浏览: 47
好的,这是一个比较复杂的问题,需要设计一个学生结构体来存储学生信息,并且需要用文件来保存学生信息,下面是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_STUDENT_NUM 100
struct student {
char name[MAX_NAME_LEN];
int id;
float grade;
};
void add_student(struct student *students, int *num_students);
void delete_student(struct student *students, int *num_students);
void modify_student(struct student *students, int *num_students);
void search_student(struct student *students, int num_students);
void insert_student(struct student *students, int *num_students);
void count_student(int num_students);
int main() {
struct student students[MAX_STUDENT_NUM];
int num_students = 0;
while (1) {
printf("What do you want to do?\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Search student\n");
printf("5. Insert student\n");
printf("6. Count student\n");
printf("7. Exit\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
delete_student(students, &num_students);
break;
case 3:
modify_student(students, &num_students);
break;
case 4:
search_student(students, num_students);
break;
case 5:
insert_student(students, &num_students);
break;
case 6:
count_student(num_students);
break;
case 7:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
void add_student(struct student *students, int *num_students) {
if (*num_students >= MAX_STUDENT_NUM) {
printf("Too many students.\n");
return;
}
struct student new_student;
printf("Enter name: ");
scanf("%s", new_student.name);
printf("Enter id: ");
scanf("%d", &new_student.id);
printf("Enter grade: ");
scanf("%f", &new_student.grade);
students[*num_students] = new_student;
(*num_students)++;
printf("Student added.\n");
}
void delete_student(struct student *students, int *num_students) {
int id;
printf("Enter student id to delete: ");
scanf("%d", &id);
for (int i = 0; i < *num_students; i++) {
if (students[i].id == id) {
for (int j = i; j < *num_students - 1; j++) {
students[j] = students[j+1];
}
(*num_students)--;
printf("Student deleted.\n");
return;
}
}
printf("Student not found.\n");
}
void modify_student(struct student *students, int *num_students) {
int id;
printf("Enter student id to modify: ");
scanf("%d", &id);
for (int i = 0; i < *num_students; i++) {
if (students[i].id == id) {
printf("Enter new name (or press enter to keep current name): ");
char new_name[MAX_NAME_LEN];
fgets(new_name, MAX_NAME_LEN, stdin);
new_name[strcspn(new_name, "\n")] = 0;
if (strlen(new_name) > 0) {
strcpy(students[i].name, new_name);
}
printf("Enter new grade (or press enter to keep current grade): ");
char new_grade_str[10];
fgets(new_grade_str, 10, stdin);
new_grade_str[strcspn(new_grade_str, "\n")] = 0;
if (strlen(new_grade_str) > 0) {
sscanf(new_grade_str, "%f", &students[i].grade);
}
printf("Student modified.\n");
return;
}
}
printf("Student not found.\n");
}
void search_student(struct student *students, int num_students) {
int id;
printf("Enter student id to search: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Name: %s\n", students[i].name);
printf("Grade: %.1f\n", students[i].grade);
return;
}
}
printf("Student not found.\n");
}
void insert_student(struct student *students, int *num_students) {
if (*num_students >= MAX_STUDENT_NUM) {
printf("Too many students.\n");
return;
}
int index;
printf("Enter the index to insert the student (starting from 0): ");
scanf("%d", &index);
if (index < 0 || index > *num_students) {
printf("Invalid index.\n");
return;
}
struct student new_student;
printf("Enter name: ");
scanf("%s", new_student.name);
printf("Enter id: ");
scanf("%d", &new_student.id);
printf("Enter grade: ");
scanf("%f", &new_student.grade);
for (int i = *num_students; i > index; i--) {
students[i] = students[i-1];
}
students[index] = new_student;
(*num_students)++;
printf("Student inserted.\n");
}
void count_student(int num_students) {
printf("Number of students: %d\n", num_students);
}
```
这个程序用了一个循环来提供菜单,其中每个菜单选项都对应一个函数,可以实现增加、删除、修改、查找和插入学生,以及统计学生人数。注意,这个程序只是一个简单的实现,实际情况中可能还需要考虑更多的细节和异常情况。
阅读全文