让我来详细讲解“C语言实现学生信息管理程序”的攻略。
概述
学生信息管理程序是一个非常基础的程序,它主要实现如下功能:
- 添加学生信息
- 查询学生信息
- 修改学生信息
- 删除学生信息
开发学生信息管理程序可以加强我们对C语言基础知识的掌握,例如指针、结构体等等。
开发步骤
1. 创建学生信息结构体
首先,我们需要创建一个结构体来存储学生信息。结构体可以包含学生的姓名、学号、性别、出生日期、电话号码等信息。
struct Student {
char name[20];
int id;
char sex[10];
char birthday[20];
char phone[20];
};
2. 添加学生信息
我们可以使用数组存储学生信息,每次添加学生信息时,先判断数组是否已满,如果未满则添加学生信息。
#define MAX_STUDENT_NUM 100
struct Student students[MAX_STUDENT_NUM];
int currentStudentNum = 0;
void addStudent() {
if (currentStudentNum >= MAX_STUDENT_NUM) {
printf("Failed to add student, the number of students has reached the highest.\n");
return;
}
struct Student student;
printf("Please input the student name: ");
scanf("%s", student.name);
printf("Please input the student id: ");
scanf("%d", &student.id);
printf("Please input the student sex: ");
scanf("%s", student.sex);
printf("Please input the student birthday: ");
scanf("%s", student.birthday);
printf("Please input the student phone: ");
scanf("%s", student.phone);
students[currentStudentNum] = student;
currentStudentNum++;
printf("Successfully added student.\n");
}
3. 查询学生信息
我们可以通过输入学生的学号或姓名来查询具体某个学生的信息,也可以查询所有学生的信息。
void searchStudent() {
printf("Please select the search method:\n");
printf("1. Search by student id.\n");
printf("2. Search by student name.\n");
printf("3. Search all students.\n");
int option;
scanf("%d", &option);
switch (option) {
case 1: {
int id;
printf("Please input the student id to search: ");
scanf("%d", &id);
for (int i = 0; i < currentStudentNum; i++) {
if (students[i].id == id) {
printf("name: %s, id: %d, sex: %s, birthday: %s, phone: %s\n", students[i].name, students[i].id, students[i].sex, students[i].birthday, students[i].phone);
return;
}
}
printf("No student found with the id of %d.\n", id);
break;
}
case 2: {
char name[20];
printf("Please input the student name to search: ");
scanf("%s", name);
int foundCount = 0;
for (int i = 0; i < currentStudentNum; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("name: %s, id: %d, sex: %s, birthday: %s, phone: %s\n", students[i].name, students[i].id, students[i].sex, students[i].birthday, students[i].phone);
foundCount++;
}
}
if (foundCount == 0) {
printf("No student found with the name of %s.\n", name);
}
break;
}
case 3: {
if (currentStudentNum == 0) {
printf("No students added yet.\n");
} else {
for (int i = 0; i < currentStudentNum; i++) {
printf("name: %s, id: %d, sex: %s, birthday: %s, phone: %s\n", students[i].name, students[i].id, students[i].sex, students[i].birthday, students[i].phone);
}
}
break;
}
default: {
printf("Invalid input.\n");
break;
}
}
}
4. 修改学生信息
我们可以通过输入学生的学号或姓名来修改具体某个学生的信息。
void modifyStudent() {
printf("Please input the student id or name to modify: ");
char input[20];
scanf("%s", input);
int foundIndex = -1;
for (int i = 0; i < currentStudentNum; i++) {
if (strcmp(students[i].name, input) == 0 || students[i].id == atoi(input)) {
foundIndex = i;
break;
}
}
if (foundIndex == -1) {
printf("No student found with the name or id of %s.\n", input);
} else {
struct Student student;
printf("Please input the new student name (current: %s): ", students[foundIndex].name);
scanf("%s", student.name);
printf("Please input the new student id (current: %d): ", students[foundIndex].id);
scanf("%d", &student.id);
printf("Please input the new student sex (current: %s): ", students[foundIndex].sex);
scanf("%s", student.sex);
printf("Please input the new student birthday (current: %s): ", students[foundIndex].birthday);
scanf("%s", student.birthday);
printf("Please input the new student phone (current: %s): ", students[foundIndex].phone);
scanf("%s", student.phone);
students[foundIndex] = student;
}
}
5. 删除学生信息
我们可以通过输入学生的学号或姓名来删除具体某个学生的信息。
void deleteStudent() {
printf("Please input the student id or name to delete: ");
char input[20];
scanf("%s", input);
int foundIndex = -1;
for (int i = 0; i < currentStudentNum; i++) {
if (strcmp(students[i].name, input) == 0 || students[i].id == atoi(input)) {
foundIndex = i;
break;
}
}
if (foundIndex == -1) {
printf("No student found with the name or id of %s.\n", input);
} else {
for (int i = foundIndex; i < currentStudentNum - 1; i++) {
students[i] = students[i + 1];
}
currentStudentNum--;
printf("Successfully deleted student.\n");
}
}
示例说明
示例1:添加/student
在程序中输入1,然后输入学生的姓名、学号、性别、出生日期和电话号码,程序会将这个学生添加到列表中。
Please select the operation:
1. Add student.
2. Search student.
3. Modify student.
4. Delete student.
5. Exit.
1
Please input the student name: Tom
Please input the student id: 123
Please input the student sex: Male
Please input the student birthday: 1990/1/1
Please input the student phone: 18600000000
Successfully added student.
示例2:查询学生信息
在程序中输入2,然后根据提示选择查询方式,可以根据学号、姓名或者全部查询学生信息。
Please select the operation:
1. Add student.
2. Search student.
3. Modify student.
4. Delete student.
5. Exit.
2
Please select the search method:
1. Search by student id.
2. Search by student name.
3. Search all students.
3
name: Tom, id: 123, sex: Male, birthday: 1990/1/1, phone: 18600000000
这就是“C语言实现学生信息管理程序”的完整攻略,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言实现学生信息管理程序 - Python技术站