c程序中文学生管理系统
时间: 2023-10-16 21:06:35 浏览: 103
好的,我可以为您提供一个简单的C语言学生管理系统的示例代码。这个学生管理系统可以添加、删除、修改和显示学生信息。以下是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[50];
char id[20];
int age;
};
int num_students = 0;
struct student students[MAX_STUDENTS];
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: Maximum number of students reached.\n");
return;
}
struct student new_student;
printf("Enter name: ");
scanf("%s", new_student.name);
printf("Enter ID: ");
scanf("%s", new_student.id);
printf("Enter age: ");
scanf("%d", &new_student.age);
students[num_students] = new_student;
num_students++;
printf("Student added successfully.\n");
}
void remove_student() {
char id[20];
printf("Enter ID of student to remove: ");
scanf("%s", id);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
found = 1;
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j+1];
}
num_students--;
printf("Student removed successfully.\n");
break;
}
}
if (!found) {
printf("Error: Student not found.\n");
}
}
void update_student() {
char id[20];
printf("Enter ID of student to update: ");
scanf("%s", id);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
found = 1;
printf("Enter new name (or leave blank to keep current name): ");
char new_name[50];
scanf("%s", new_name);
if (strcmp(new_name, "") != 0) {
strcpy(students[i].name, new_name);
}
printf("Enter new age (or leave blank to keep current age): ");
char new_age_str[10];
scanf("%s", new_age_str);
if (strcmp(new_age_str, "") != 0) {
int new_age = atoi(new_age_str);
students[i].age = new_age;
}
printf("Student updated successfully.\n");
break;
}
}
if (!found) {
printf("Error: Student not found.\n");
}
}
void display_students() {
printf("Name\tID\tAge\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%s\t%d\n", students[i].name, students[i].id, students[i].age);
}
}
int main() {
int choice = 0;
while (choice != 5) {
printf("\n1. Add student\n");
printf("2. Remove student\n");
printf("3. Update student\n");
printf("4. Display students\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
remove_student();
break;
case 3:
update_student();
break;
case 4:
display_students();
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Error: Invalid choice.\n");
break;
}
}
return 0;
}
```
这个示例代码包含一个 student 结构体,用于存储学生信息。程序使用一个 students 数组来存储所有的学生信息,最多存储100个学生。程序提供了添加、删除、修改和显示学生信息的功能。可以根据需要进行修改和扩展。
阅读全文