C语言实现动态学生记录列表:完整代码及功能详解
It looks like you're in the process of defining a structure for a student record in C. However, the structure definition is incomplete. Let's complete it and add some basic functions to manage a dynamic array of these student records. Here's a more complete version of your code: ```c #include <stdio.h> #include <stdlib.h> // For malloc, realloc, free #include <string.h> // For string handling functions #define error 0 // Macro definition for error #define ok 1 // Macro definition for success #define over -2 // Macro definition for overflow #define listinitsize 20 // Initial size of the list #define listincrement 5 // Increment size for the list #define namelen 8 // Maximum length of the name #define majorlen 20 // Maximum length of the major #define init_n 3 // Initial number of students typedef int status; // Custom type for status typedef char statusc; typedef struct { statusc number[10]; // 学号 (Student ID) statusc name[namelen + 1]; // 姓名 (Name) statusc gender; // 性别 (Gender) statusc telephone[12]; // 电话 (Telephone) statusc major[majorlen + 1]; // 专业 (Major) } Student; typedef struct { Student *data; // Pointer to the array of students int length; // Current number of students int listsize; // Current allocated size of the list } StudentList; // Function to initialize the student list status InitStudentList(StudentList *L) { L->data = (Student *)malloc(listinitsize * sizeof(Student)); if (!L->data) return error; L->length = 0; L->listsize = listinitsize; return ok; } // Function to insert a student into the list status InsertStudent(StudentList *L, Student s, int i) { if (i < 1 || i > L->length + 1) return error; if (L->length >= L->listsize) { Student *newdata = (Student *)realloc(L->data, (L->listsize + listincrement) * sizeof(Student)); if (!newdata) return error; L->data = newdata; L->listsize += listincrement; } for (int j = L->length; j >= i; j--) { L->data[j] = L->data[j - 1]; } L->data[i - 1] = s; L->length++; return ok; } // Function to display the student list void DisplayStudentList(StudentList L) { for (int i = 0; i < L.length; i++) { printf("Student %d:\n", i + 1); printf("Number: %s\n", L.data[i].number); printf("Name: %s\n", L.data[i].name); printf("Gender: %c\n", L.data[i].gender); printf("Telephone: %s\n", L.data[i].telephone); printf("Major: %s\n", L.data[i].major); printf("\n"); } } // Function to destroy the student list void DestroyStudentList(StudentList *L) { free(L->data); L->data = NULL; L->length = 0; L->listsize = 0; } int main() { StudentList L; if (InitStudentList(&L) != ok) { printf("Failed to initialize student list.\n"); return error; } // Example of inserting students Student s1 = {"12345678", "Alice", 'F', "1234567890", "Computer Science"}; Student s2 = {"87654321", "Bob", 'M', "0987654321", "Mathematics"}; if (InsertStudent(&L, s1, 1) != ok) { printf("Failed to insert student 1.\n"); } if (InsertStudent(&L, s2, 2) != ok) { printf("Failed to insert student 2.\n"); } // Display the student list DisplayStudentList(L); // Clean up DestroyStudentList(&L); return 0; } ``` Explanation: 1. Structure Definition: The `Student` structure contains fields for the student's ID, name, gender, telephone, and major. 2. StudentList Structure: This structure manages an array of `Student` records, along with the current length and allocated size of the list. 3. Initialization: The `InitStudentList` function initializes the list with an initial size. 4. Insertion: The `InsertStudent` function inserts a new student into the list at a specified position, resizing the list if necessary. 5. Display: The `DisplayStudentList` function prints all the students in the list. 6. Destruction: The `DestroyStudentList` function frees the allocated memory. This code provides a basic framework for managing a dynamic list of student records in C. You can expand it with additional features as needed. ######[AI写代码神器 | 1142点数解答 | 2024-11-07 19:08:54]
- Python 实现简易密码设置系统:多种功能交互与安全要点揭秘(字节豆包 | 286点数解答 | 2025-03-28 10:43:21)355
- C语言实现多功能密码设置系统:含代码及安全注意要点(字节豆包 | 414点数解答 | 2025-03-28 10:43:55)343
- C 语言实现:超简单密码设置系统,含代码与注意要点!(字节豆包 | 519点数解答 | 2025-03-28 12:42:02)384
- C语言实现密码管理系统:含功能代码与安全注意事项(字节豆包 | 409点数解答 | 2025-04-05 18:09:56)333
- C语言实现多功能密码系统:初始化、确认、设置、显示、重置与退出(字节豆包 | 454点数解答 | 2025-04-05 18:48:42)332
- C语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 413点数解答 | 2025-04-06 15:37:16)279
- C 语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 403点数解答 | 2025-04-06 15:37:54)292
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- 卡牌游戏技能“教化”机制全解析:规则、代码与注意要点 (DeepSeek | 353点数解答 | 2025-07-07 15:40:28)169
- Python 模拟无名杀“教化”技能:出牌阶段规则与代码实现及优化建议(字节豆包 | 546点数解答 | 2025-07-07 15:41:02)174
- Java 实现“教化”技能:出牌阶段限两次的牌获取规则代码解析(字节豆包 | 1003点数解答 | 2025-07-07 15:41:54)144
- JavaScript 实现“教化”技能:两次出牌限制与牌型记录逻辑揭秘(字节豆包 | 846点数解答 | 2025-07-07 15:42:46)183