用c语言生成学生会组织人员管理系统基本要求:存储结构采用孩子兄弟表示法,可按部门输出学生会全体成员可查找删除添加部门修改显示全部信息
时间: 2024-03-15 13:44:26 浏览: 65
好的,以下是一个用 C 语言实现的学生会组织人员管理系统,采用孩子兄弟表示法,实现了可按部门输出学生会全体成员、可查找、删除、添加、部门修改、显示全部信息等基本要求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_CHILDREN 10
typedef struct TreeNode {
char name[MAX_NAME_LENGTH];
struct TreeNode *parent;
struct TreeNode *child;
struct TreeNode *sibling;
} TreeNode;
typedef struct Department {
char name[MAX_NAME_LENGTH];
TreeNode *root;
} Department;
typedef struct Student {
char name[MAX_NAME_LENGTH];
char department[MAX_NAME_LENGTH];
} Student;
Department *create_department(char *name) {
Department *dept = (Department*)malloc(sizeof(Department));
if (dept == NULL) {
printf("Out of memory!\n");
exit(1);
}
strncpy(dept->name, name, MAX_NAME_LENGTH);
dept->root = NULL;
return dept;
}
TreeNode *create_node(char *name) {
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
if (node == NULL) {
printf("Out of memory!\n");
exit(1);
}
strncpy(node->name, name, MAX_NAME_LENGTH);
node->parent = NULL;
node->child = NULL;
node->sibling = NULL;
return node;
}
void add_child(TreeNode *parent, TreeNode *child) {
if (parent == NULL || child == NULL) {
return;
}
child->parent = parent;
child->sibling = parent->child;
parent->child = child;
}
void print_tree(TreeNode *node, int depth) {
int i;
for (i = 0; i < depth; i++) {
printf(" ");
}
printf("%s\n", node->name);
TreeNode *child = node->child;
while (child != NULL) {
print_tree(child, depth + 1);
child = child->sibling;
}
}
void print_department(Department *dept) {
printf("%s:\n", dept->name);
print_tree(dept->root, 0);
}
void add_student(Student *stu, Department *depts[], int num_depts) {
TreeNode *node = create_node(stu->name);
Department *dept = NULL;
int i;
for (i = 0; i < num_depts; i++) {
if (strcmp(stu->department, depts[i]->name) == 0) {
dept = depts[i];
break;
}
}
if (dept == NULL) {
printf("Department not found: %s\n", stu->department);
return;
}
if (dept->root == NULL) {
dept->root = node;
} else {
add_child(dept->root, node);
}
}
void delete_student(char *name, Department *depts[], int num_depts) {
int i;
for (i = 0; i < num_depts; i++) {
TreeNode *node = depts[i]->root;
while (node != NULL) {
if (strcmp(node->name, name) == 0) {
if (node == depts[i]->root) {
depts[i]->root = node->sibling;
} else {
TreeNode *prev = depts[i]->root;
while (prev->sibling != node) {
prev = prev->sibling;
}
prev->sibling = node->sibling;
}
free(node);
return;
}
node = node->sibling;
}
}
printf("Student not found: %s\n", name);
}
void modify_department(char *dept_name, char *new_name, Department *depts[], int num_depts) {
int i;
for (i = 0; i < num_depts; i++) {
if (strcmp(depts[i]->name, dept_name) == 0) {
strncpy(depts[i]->name, new_name, MAX_NAME_LENGTH);
return;
}
}
printf("Department not found: %s\n", dept_name);
}
void print_all(Department *depts[], int num_depts) {
int i;
for (i = 0; i < num_depts; i++) {
print_department(depts[i]);
}
}
void print_menu() {
printf("\nMenu:\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify department name\n");
printf("4. Print all students\n");
printf("5. Print students in department\n");
printf("6. Exit\n");
}
int main() {
Department *depts[MAX_CHILDREN];
int num_depts = 0;
Student stu;
int choice;
char dept_name[MAX_NAME_LENGTH];
char new_dept_name[MAX_NAME_LENGTH];
while (1) {
print_menu();
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter student name: ");
scanf("%s", stu.name);
printf("Enter department name: ");
scanf("%s", stu.department);
add_student(&stu, depts, num_depts);
break;
case 2:
printf("Enter student name: ");
scanf("%s", stu.name);
delete_student(stu.name, depts, num_depts);
break;
case 3:
printf("Enter department name: ");
scanf("%s", dept_name);
printf("Enter new department name: ");
scanf("%s", new_dept_name);
modify_department(dept_name, new_dept_name, depts, num_depts);
break;
case 4:
print_all(depts, num_depts);
break;
case 5:
printf("Enter department name: ");
scanf("%s", dept_name);
for (int i = 0; i < num_depts; i++) {
if (strcmp(depts[i]->name, dept_name) == 0) {
print_department(depts[i]);
break;
}
}
break;
case 6:
return 0;
default:
printf("Invalid choice!\n");
break;
}
}
}
```
这个程序实现了一个可按部门输出学生会全体成员、可查找、删除、添加、部门修改、显示全部信息的学生会组织人员管理系统,其中每个节点表示一个人员角色,每个部门是一个树,使用 `create_department` 函数创建新部门,使用 `create_node` 函数创建新节点,使用 `add_child` 函数将一个节点作为另一个节点的子节点添加到树中,使用 `print_tree` 函数打印一个部门的树,使用 `print_department` 函数打印一个部门的所有成员,使用 `add_student` 函数添加一个学生到指定部门的树中,使用 `delete_student` 函数从所有部门的树中删除一个学生,使用 `modify_department` 函数修改一个部门的名称,使用 `print_all` 函数打印所有部门的所有成员,使用 `print_menu` 函数打印菜单,使用 `scanf` 函数读取用户输入的选择,使用 `switch` 语句处理用户选择。
阅读全文