【以下回答由 GPT 生成】
首先,我们需要定义一个链表结构体LinkedList来表示选手信息的链表。链表结构体包含一个指向链表头节点的指针head。
struct LinkedList {
struct Node* head;
};
然后,我们需要实现一系列基本的链表操作函数,包括创建链表、插入节点、删除节点等。
- 创建链表函数
createLinkedList用于创建一个空链表。
struct LinkedList* createLinkedList() {
struct LinkedList* list = (struct LinkedList*) malloc(sizeof(struct LinkedList));
list->head = NULL;
return list;
}
- 插入节点函数
insertNode,通过在链表头部插入新节点来记录选手信息。
void insertNode(struct LinkedList* list, struct Athlete data) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = list->head;
list->head = newNode;
}
- 删除节点函数
deleteNode,通过姓名或身份证号在链表中删除对应的节点。
void deleteNode(struct LinkedList* list, char* nameOrId) {
struct Node* current = list->head;
struct Node* prev = NULL;
while (current != NULL) {
if (strcmp(current->data.name, nameOrId) == 0 || strcmp(current->data.id, nameOrId) == 0) {
if (prev == NULL) {
list->head = current->next;
} else {
prev->next = current->next;
}
free(current);
return;
}
prev = current;
current = current->next;
}
}
接下来,我们需要实现程序的主要功能。
- 数据录入功能,使用
insertNode函数将选手信息插入链表中。
void dataEntry(struct LinkedList* list, int numPlayers) {
for (int i = 0; i < numPlayers; i++) {
struct Athlete athlete;
printf("Enter name: ");
scanf("%s", athlete.name);
printf("Enter ID: ");
scanf("%s", athlete.id);
athlete.attend = false; // 默认未参加检录
athlete.score = 0; // 默认成绩为0
insertNode(list, athlete);
}
}
- 数据删除功能,使用
deleteNode函数在链表中删除选手信息。
void dataDelete(struct LinkedList* list, char* deleteNameOrId) {
deleteNode(list, deleteNameOrId);
}
- 抽签管理,根据每组人数,随机将选手划分到不同的组,并记录检录时间。
void luckyDraw(struct LinkedList* list, int groupSize) {
struct Node* current = list->head;
int group = 1; // 组别,默认从1开始
int track = 1; // 赛道,默认从1开始
while (current != NULL) {
current->data.attend = true; // 参加检录
current->data.score = 0; // 默认成绩为0
printf("Group: %d, Track: %d, Name: %s, ID: %s\n", group, track, current->data.name, current->data.id);
track++;
if (track > groupSize) {
track = 1;
group++;
}
current = current->next;
}
}
- 查找选手信息,根据输入的姓名或身份证号在链表中查找并打印选手成绩。
void searchAthlete(struct LinkedList* list, char* nameOrId) {
struct Node* current = list->head;
while (current != NULL) {
if (strcmp(current->data.name, nameOrId) == 0 || strcmp(current->data.id, nameOrId) == 0) {
printf("Name: %s, ID: %s, Score: %.2f\n", current->data.name, current->data.id, current->data.score);
return;
}
current = current->next;
}
printf("Athlete not found.\n");
}
- 输出晋级名单,根据选手成绩,确定每组的前三名选手晋级,并打印晋级名单。
void printQualifiers(struct LinkedList* list, int groupSize) {
struct Node* current = list->head;
struct Athlete qualifiers[groupSize * 3];
int count = 0;
while (current != NULL) {
qualifiers[count] = current->data;
count++;
if (count % groupSize == 0) {
// 每组选手排序
for (int i = 0; i < groupSize - 1; i++) {
for (int j = 0; j < groupSize - i - 1; j++) {
if (qualifiers[count - j - 2].score < qualifiers[count - j - 1].score) {
struct Athlete temp = qualifiers[count - j - 2];
qualifiers[count - j - 2] = qualifiers[count - j - 1];
qualifiers[count - j - 1] = temp;
}
}
}
// 打印每组晋级名单
printf("Qualifiers for Group %d:\n", count / groupSize);
for (int i = 0; i < 3; i++) {
printf("Name: %s, ID: %s, Score: %.2f\n", qualifiers[count - groupSize + i].name, qualifiers[count - groupSize + i].id, qualifiers[count - groupSize + i].score);
}
}
current = current->next;
}
}
最后,我们需要编写一个主程序来操作以上功能。
int main() {
struct LinkedList* list = createLinkedList();
// 模拟数据初始化过程
struct Athlete athletes[24] = {
{"Athlete1", "ID1", false, 0},
{"Athlete2", "ID2", false, 0},
//...
{"Athlete24", "ID24", false, 0}
};
for (int i = 0; i < 24; i++) {
insertNode(list, athletes[i]);
}
// 主程序循环
while (1) {
printf("\n1. Data Entry\n");
printf("2. Data Delete\n");
printf("3. Lucky Draw\n");
printf("4. Search Athlete\n");
printf("5. Print Qualifiers\n");
printf("6. Exit\n");
int choice;
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// 数据录入
dataEntry(list, 24);
break;
case 2: {
// 数据删除
char deleteNameOrId[50];
printf("Enter name or ID to delete: ");
scanf("%s", deleteNameOrId);
dataDelete(list, deleteNameOrId);
break;
}
case 3: {
// 抽签管理
int groupSize;
printf("Enter group size: ");
scanf("%d", &groupSize);
luckyDraw(list, groupSize);
break;
}
case 4: {
// 查找选手信息
char nameOrId[50];
printf("Enter name or ID to search: ");
scanf("%s", nameOrId);
searchAthlete(list, nameOrId);
break;
}
case 5: {
// 输出晋级名单
int groupSize;
printf("Enter group size: ");
scanf("%d", &groupSize);
printQualifiers(list, groupSize);
break;
}
case 6:
// 退出程序
return 0;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
这个程序实现了基于链式存储结构的全运会径赛管理程序的所有功能要求,你可以通过菜单选择要执行的操作。如果你还有其他问题,请随时提问。
【相关推荐】
如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^