学生信息管理:定义学生信息结构体,包括学生姓名、学号、性别、年龄、班级等信息。可以使用链表或数组来存储学生信息。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生信息结构体
struct Student {
char name[20]; // 学生姓名
int id; // 学生学号
char gender; // 学生性别
int age; // 学生年龄
char class[10]; // 学生班级
struct Student *next; // 下一个学生节点
};
// 定义链表头节点
struct Student *head = NULL;
// 添加学生信息函数
void addStudent() {
struct Student *newStudent = (struct Student*) malloc(sizeof(struct Student));
printf("Please enter student name: ");
scanf("%s", newStudent->name);
printf("Please enter student ID: ");
scanf("%d", &(newStudent->id));
printf("Please enter student gender (M/F): ");
scanf(" %c", &(newStudent->gender));
printf("Please enter student age: ");
scanf("%d", &(newStudent->age));
printf("Please enter student class: ");
scanf("%s", newStudent->class);
newStudent->next = NULL;
if (head == NULL) {
head = newStudent;
} else {
struct Student *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newStudent;
}
}
// 输出学生信息函数
void printStudents() {
struct Student *temp = head;
printf("Student Information:\n");
while (temp != NULL) {
printf("Name: %s, ID: %d, Gender: %c, Age: %d, Class: %s\n", temp->name, temp->id, temp->gender, temp->age, temp->class);
temp = temp->next;
}
}
int main() {
// 调用添加学生信息函数,添加三个学生信息
addStudent();
addStudent();
addStudent();
// 调用输出学生信息函数,输出所有学生信息
printStudents();
return 0;
}
以上代码定义了一个学生信息结构体,包括学生姓名、学号、性别、年龄、班级等信息,并使用链表来存储学生信息。在主函数中,调用了添加学生信息函数addStudent(),添加了三个学生信息,并调用了输出学生信息函数printStudents(),输出了所有学生信息。
需要注意的是,以上代码只是一个简单的示例,实际应用中还需要根据需求进行修改和扩展。同时,链表的插入、删除等操作也需要进行相应的实现。