c语言结构体信息管理系统源代码
时间: 2023-07-24 08:31:45 浏览: 84
好的,下面是一个简单的C语言结构体信息管理系统的源代码,可以供您参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_STUDENT_NUM 100
typedef struct {
char name[MAX_NAME_LEN];
int age;
int score;
} Student;
int menu();
void add_student(Student students[], int *num);
void delete_student(Student students[], int *num);
void modify_student(Student students[], int num);
void search_student(Student students[], int num);
void print_all_students(Student students[], int num);
int main() {
int choice, student_num = 0;
Student students[MAX_STUDENT_NUM];
while (1) {
choice = menu();
switch (choice) {
case 1:
add_student(students, &student_num);
break;
case 2:
delete_student(students, &student_num);
break;
case 3:
modify_student(students, student_num);
break;
case 4:
search_student(students, student_num);
break;
case 5:
print_all_students(students, student_num);
break;
case 6:
printf("Bye!\n");
exit(0);
default:
printf("Invalid choice, please try again.\n");
break;
}
}
return 0;
}
int menu() {
int choice;
printf("\n1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Search student\n");
printf("5. Print all students\n");
printf("6. Quit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
void add_student(Student students[], int *num) {
if (*num == MAX_STUDENT_NUM) {
printf("The number of students has reached the upper limit.\n");
return;
}
printf("\nPlease enter the student's name: ");
scanf("%s", students[*num].name);
printf("Please enter the student's age: ");
scanf("%d", &students[*num].age);
printf("Please enter the student's score: ");
scanf("%d", &students[*num].score);
printf("Add student successfully!\n");
(*num)++;
}
void delete_student(Student students[], int *num) {
char name[MAX_NAME_LEN];
int i, j;
printf("\nPlease enter the name of the student to be deleted: ");
scanf("%s", name);
for (i = 0; i < *num; i++) {
if (strcmp(students[i].name, name) == 0) {
for (j = i; j < *num - 1; j++) {
students[j] = students[j + 1];
}
(*num)--;
printf("Delete student successfully!\n");
return;
}
}
printf("The student with this name does not exist.\n");
}
void modify_student(Student students[], int num) {
char name[MAX_NAME_LEN];
int i;
printf("\nPlease enter the name of the student to be modified: ");
scanf("%s", name);
for (i = 0; i < num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Please enter the student's age: ");
scanf("%d", &students[i].age);
printf("Please enter the student's score: ");
scanf("%d", &students[i].score);
printf("Modify student successfully!\n");
return;
}
}
printf("The student with this name does not exist.\n");
}
void search_student(Student students[], int num) {
char name[MAX_NAME_LEN];
int i;
printf("\nPlease enter the name of the student to be searched: ");
scanf("%s", name);
for (i = 0; i < num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Name: %s, Age: %d, Score: %d\n", students[i].name, students[i].age, students[i].score);
return;
}
}
printf("The student with this name does not exist.\n");
}
void print_all_students(Student students[], int num) {
int i;
printf("\nAll students:\n");
for (i = 0; i < num; i++) {
printf("Name: %s, Age: %d, Score: %d\n", students[i].name, students[i].age, students[i].score);
}
}
```
这个程序实现了添加、删除、修改、查找和打印所有学生的功能。您可以根据需要对其进行修改和完善。
阅读全文