以下是一个使用 C 语言实现的程序,可以满足你的需求:
#include <stdio.h>
#include <stdlib.h>
struct Student {
int num;
char name[20];
float score[3];
struct Student *next;
};
struct Student *create_student(int num, char *name, float score[3]) {
struct Student *student = (struct Student*)malloc(sizeof(struct Student));
student->num = num;
for (int i = 0; i < 20; i++) {
student->name[i] = name[i];
}
for (int i = 0; i < 3; i++) {
student->score[i] = score[i];
}
student->next = NULL;
return student;
}
void insert_student(struct Student *head, struct Student *new_student) {
struct Student *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_student;
}
void print_students(struct Student *head) {
struct Student *p = head;
while (p != NULL) {
printf("%d, %s, %.2f, %.2f, %.2f,\n", p->num, p->name, p->score[0], p->score[1], p->score[2]);
p = p->next;
}
}
int main() {
struct Student *head = NULL;
int num;
char name[20];
float score[3];
printf("请输入学生数据:\n");
while (1) {
scanf("%d%s%f%f%f", &num, name, &score[0], &score[1], &score[2]);
if (num == 0) {
break;
}
struct Student *student = create_student(num, name, score);
if (head == NULL) {
head = student;
} else {
insert_student(head, student);
}
}
printf("程序输出:\n");
print_students(head);
return 0;
}
上面的程序定义了一个结构体 Student
,用于表示学生的信息。然后定义了三个函数:
create_student
函数用于创建一个学生信息的结构体。insert_student
函数用于将一个学生信息的结构体插