c语言源代码具体要求: ①输入学生信息:输入学生的学号、姓名、班级②查找学生信息:能按学号和姓名分别查找学生的完整记录信息。 ③修改学生信息:能修改学生各项信息。 ④删除学生信息 ⑤插入学生信息
时间: 2024-03-18 12:41:43 浏览: 65
明白了,您想了解如何用 C 语言编写一个简单的学生信息管理系统,实现以上功能。以下是一个简单的示例代码,仅供参考:
```
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct Student {
char id[20];
char name[20];
char class[20];
};
struct Student students[MAX_STUDENT_NUM];
int student_count = 0;
void add_student() {
if (student_count >= MAX_STUDENT_NUM) {
printf("Error: Student database is full.\n");
return;
}
struct Student s;
printf("Enter student ID: ");
scanf("%s", s.id);
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student class: ");
scanf("%s", s.class);
students[student_count++] = s;
printf("Student added successfully.\n");
}
void search_student_by_id() {
char id[20];
printf("Enter student ID to search: ");
scanf("%s", id);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Student found:\nID: %s\nName: %s\nClass: %s\n", students[i].id, students[i].name, students[i].class);
found = 1;
break;
}
}
if (!found) {
printf("Error: Student not found.\n");
}
}
void search_student_by_name() {
char name[20];
printf("Enter student name to search: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Student found:\nID: %s\nName: %s\nClass: %s\n", students[i].id, students[i].name, students[i].class);
found = 1;
}
}
if (!found) {
printf("Error: Student not found.\n");
}
}
void update_student() {
char id[20];
printf("Enter student ID to update: ");
scanf("%s", id);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Enter new student name (or press enter to skip): ");
char name[20];
scanf("%s", name);
if (strlen(name) > 0) {
strcpy(students[i].name, name);
}
printf("Enter new student class (or press enter to skip): ");
char class[20];
scanf("%s", class);
if (strlen(class) > 0) {
strcpy(students[i].class, class);
}
printf("Student updated successfully.\n");
found = 1;
break;
}
}
if (!found) {
printf("Error: Student not found.\n");
}
}
void delete_student() {
char id[20];
printf("Enter student ID to delete: ");
scanf("%s", id);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < student_count - 1; j++) {
students[j] = students[j + 1];
}
student_count--;
printf("Student deleted successfully.\n");
found = 1;
break;
}
}
if (!found) {
printf("Error: Student not found.\n");
}
}
int main() {
while (1) {
printf("\n");
printf("1. Add student\n");
printf("2. Search student by ID\n");
printf("3. Search student by name\n");
printf("4. Update student\n");
printf("5. Delete student\n");
printf("6. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
search_student_by_id();
break;
case 3:
search_student_by_name();
break;
case 4:
update_student();
break;
case 5:
delete_student();
break;
case 6:
return 0;
default:
printf("Error: Invalid choice.\n");
}
}
}
```
这个示例代码定义了一个 `Student` 结构体,包含学生的 ID、姓名和班级。使用一个 `students` 数组来存储所有学生的信息。程序提供了以下功能:
1. 添加学生:输入学生的 ID、姓名和班级,将其添加到 `students` 数组中;
2. 按 ID 查找学生:输入学生的 ID,查找并输出该学生的完整信息;
3. 按姓名查找学生:输入学生的姓名,查找并输出所有与该姓名匹配的学生的完整信息;
4. 修改学生信息:输入要修改的学生的 ID,然后可以选择更新该学生的姓名和/或班级;
5. 删除学生信息:输入要删除的学生的 ID,将其从 `students` 数组中删除;
6. 退出程序。
请注意,这只是一个简单的示例,实际使用时应该根据具体需求进行适当的修改和扩展。
阅读全文