学生信息管理系统 学生信息包括:学号,姓名,年龄,性别,出生年月,地址,电话,E-mail等。设计一个学生信息管理系统,提供以下功能: 系统以菜单方式工作。 输入记录:学生信息录入功能。 查询记录:用户可以按学号或者姓名进行查询。 更新记录:对学生信息进行修改、删除、插入操作。 统计记录:对学生不同性别的人数进行统计。 排序记录:按学号或年龄排序。 输出记录:实现对记录的存盘操作或者将存储记录以表格的形式在屏幕上打印出来。
时间: 2024-03-14 13:47:33 浏览: 90
好的,这个问题需要编写一个完整的程序来实现。以下是一个简单的C语言程序,可以实现这些功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 100
struct Student {
char id[20];
char name[20];
int age;
char gender;
char birth[20];
char address[50];
char phone[20];
char email[50];
};
struct Student students[MAX_STUDENT];
int student_count = 0;
void add_student() {
if (student_count >= MAX_STUDENT) {
printf("Error: too many students.\n");
return;
}
struct Student student;
printf("Enter student info:\n");
printf("ID: ");
scanf("%s", student.id);
printf("Name: ");
scanf("%s", student.name);
printf("Age: ");
scanf("%d", &student.age);
printf("Gender (M/F): ");
scanf(" %c", &student.gender);
printf("Birth: ");
scanf("%s", student.birth);
printf("Address: ");
scanf("%s", student.address);
printf("Phone: ");
scanf("%s", student.phone);
printf("Email: ");
scanf("%s", student.email);
students[student_count++] = student;
printf("Student added.\n");
}
void print_student(struct Student student) {
printf("%s\t%s\t%d\t%c\t%s\t%s\t%s\t%s\n",
student.id, student.name, student.age, student.gender,
student.birth, student.address, student.phone, student.email);
}
void print_students() {
printf("ID\tName\tAge\tGender\tBirth\tAddress\tPhone\tEmail\n");
for (int i = 0; i < student_count; i++) {
print_student(students[i]);
}
}
void search_student() {
char keyword[20];
printf("Enter ID or name: ");
scanf("%s", keyword);
for (int i = 0; i < student_count; i++) {
if (strcmp(keyword, students[i].id) == 0 || strcmp(keyword, students[i].name) == 0) {
print_student(students[i]);
}
}
}
void update_student() {
char keyword[20];
printf("Enter ID or name: ");
scanf("%s", keyword);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(keyword, students[i].id) == 0 || strcmp(keyword, students[i].name) == 0) {
found = 1;
printf("Enter new info (press Enter to skip):\n");
printf("ID (%s): ", students[i].id);
scanf("%s", students[i].id);
printf("Name (%s): ", students[i].name);
scanf("%s", students[i].name);
printf("Age (%d): ", students[i].age);
scanf("%d", &students[i].age);
printf("Gender (%c): ", students[i].gender);
scanf(" %c", &students[i].gender);
printf("Birth (%s): ", students[i].birth);
scanf("%s", students[i].birth);
printf("Address (%s): ", students[i].address);
scanf("%s", students[i].address);
printf("Phone (%s): ", students[i].phone);
scanf("%s", students[i].phone);
printf("Email (%s): ", students[i].email);
scanf("%s", students[i].email);
printf("Student updated.\n");
break;
}
}
if (!found) {
printf("Error: student not found.\n");
}
}
void delete_student() {
char keyword[20];
printf("Enter ID or name: ");
scanf("%s", keyword);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(keyword, students[i].id) == 0 || strcmp(keyword, students[i].name) == 0) {
found = 1;
for (int j = i; j < student_count - 1; j++) {
students[j] = students[j + 1];
}
student_count--;
printf("Student deleted.\n");
break;
}
}
if (!found) {
printf("Error: student not found.\n");
}
}
void count_students() {
int male_count = 0;
int female_count = 0;
for (int i = 0; i < student_count; i++) {
if (students[i].gender == 'M') {
male_count++;
} else if (students[i].gender == 'F') {
female_count++;
}
}
printf("Male: %d, Female: %d\n", male_count, female_count);
}
void sort_students() {
printf("Sort by (ID/Age)? ");
char sort_by[10];
scanf("%s", sort_by);
if (strcmp(sort_by, "ID") == 0) {
for (int i = 0; i < student_count - 1; i++) {
for (int j = i + 1; j < student_count; j++) {
if (strcmp(students[i].id, students[j].id) > 0) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Students sorted by ID.\n");
} else if (strcmp(sort_by, "Age") == 0) {
for (int i = 0; i < student_count - 1; i++) {
for (int j = i + 1; j < student_count; j++) {
if (students[i].age > students[j].age) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Students sorted by age.\n");
} else {
printf("Error: invalid option.\n");
}
}
void save_students() {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("Error: cannot open file.\n");
return;
}
fprintf(file, "ID\tName\tAge\tGender\tBirth\tAddress\tPhone\tEmail\n");
for (int i = 0; i < student_count; i++) {
fprintf(file, "%s\t%s\t%d\t%c\t%s\t%s\t%s\t%s\n",
students[i].id, students[i].name, students[i].age, students[i].gender,
students[i].birth, students[i].address, students[i].phone, students[i].email);
}
fclose(file);
printf("Students saved.\n");
}
void load_students() {
FILE *file = fopen("students.txt", "r");
if (file == NULL) {
printf("Error: cannot open file.\n");
return;
}
student_count = 0;
char line[200];
fgets(line, 200, file); // skip header line
while (fgets(line, 200, file) != NULL && student_count < MAX_STUDENT) {
sscanf(line, "%s%s%d%c%s%s%s%s", students[student_count].id,
students[student_count].name, &students[student_count].age,
&students[student_count].gender, students[student_count].birth,
students[student_count].address, students[student_count].phone,
students[student_count].email);
student_count++;
}
fclose(file);
printf("Students loaded.\n");
}
int main() {
while (1) {
printf("\n");
printf("1. Add student\n");
printf("2. Search student\n");
printf("3. Update student\n");
printf("4. Delete student\n");
printf("5. Count students\n");
printf("6. Sort students\n");
printf("7. Print students\n");
printf("8. Save students\n");
printf("9. Load students\n");
printf("0. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
search_student();
break;
case 3:
update_student();
break;
case 4:
delete_student();
break;
case 5:
count_students();
break;
case 6:
sort_students();
break;
case 7:
print_students();
break;
case 8:
save_students();
break;
case 9:
load_students();
break;
case 0:
printf("Goodbye!\n");
exit(0);
default:
printf("Error: invalid option.\n");
}
}
return 0;
}
```
这个程序使用结构体存储学生信息,在主函数中提供了菜单,可以让用户选择不同的操作。其中包括添加、查询、更新、删除、统计、排序、输出和存储等功能。你可以根据自己的需要进行修改和扩展。
阅读全文