用c语言编写学生会组织人员管理系统 ,基本要求:以数信学院为例(根据数信学院实际情况自己设计树);存储结构采用孩子兄弟表示法: 可按部门输出学生会全体成员。 (1)部门信息(2)成员信息(3)树的构造(含学生会组织人员的增、删、改、查)
时间: 2024-01-16 19:02:22 浏览: 65
好的,这是一个比较复杂的程序,需要设计一些数据结构和算法,下面是一个C语言代码示例,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_DEPT_LEN 10
#define MAX_CHILDREN 10
// 学生会成员结构体
typedef struct member {
char name[MAX_NAME_LEN];
int age;
char dept[MAX_DEPT_LEN];
struct member *children[MAX_CHILDREN];
} Member;
// 学生会部门结构体
typedef struct dept {
char name[MAX_DEPT_LEN];
Member *head;
struct dept *next;
} Dept;
// 创建成员
Member *create_member(char name[], int age, char dept[]) {
Member *m = (Member *) malloc(sizeof(Member));
strcpy(m->name, name);
m->age = age;
strcpy(m->dept, dept);
for (int i = 0; i < MAX_CHILDREN; i++) {
m->children[i] = NULL;
}
return m;
}
// 添加成员
void add_member(Member *parent, Member *child) {
for (int i = 0; i < MAX_CHILDREN; i++) {
if (parent->children[i] == NULL) {
parent->children[i] = child;
return;
}
}
printf("Error: parent has too many children.\n");
}
// 创建部门
Dept *create_dept(char name[]) {
Dept *d = (Dept *) malloc(sizeof(Dept));
strcpy(d->name, name);
d->head = NULL;
d->next = NULL;
return d;
}
// 添加部门
void add_dept(Dept *head, Dept *d) {
while (head->next != NULL) {
head = head->next;
}
head->next = d;
}
// 查找部门
Dept *find_dept(Dept *head, char name[]) {
while (head != NULL) {
if (strcmp(head->name, name) == 0) {
return head;
}
head = head->next;
}
return NULL;
}
// 输出成员
void print_member(Member *m) {
printf("Name: %s, Age: %d, Dept: %s\n", m->name, m->age, m->dept);
}
// 输出部门
void print_dept(Dept *d) {
printf("Department: %s\n", d->name);
Member *m = d->head;
while (m != NULL) {
print_member(m);
for (int i = 0; i < MAX_CHILDREN; i++) {
if (m->children[i] != NULL) {
print_member(m->children[i]);
}
}
m = m->children[0];
}
}
// 增加成员
void add_member_handler(Dept *head) {
char dept_name[MAX_DEPT_LEN];
printf("Please enter the department name: ");
scanf("%s", dept_name);
Dept *d = find_dept(head, dept_name);
if (d == NULL) {
printf("Error: department not found.\n");
return;
}
char name[MAX_NAME_LEN];
int age;
printf("Please enter the member name: ");
scanf("%s", name);
printf("Please enter the member age: ");
scanf("%d", &age);
Member *m = create_member(name, age, dept_name);
add_member(d->head, m);
}
// 删除成员
void delete_member_handler(Dept *head) {
char dept_name[MAX_DEPT_LEN];
printf("Please enter the department name: ");
scanf("%s", dept_name);
Dept *d = find_dept(head, dept_name);
if (d == NULL) {
printf("Error: department not found.\n");
return;
}
char name[MAX_NAME_LEN];
printf("Please enter the member name: ");
scanf("%s", name);
Member *m = d->head;
while (m != NULL) {
if (strcmp(m->name, name) == 0) {
// 删除该成员及其所有子孙节点
free(m);
return;
}
for (int i = 0; i < MAX_CHILDREN; i++) {
if (m->children[i] != NULL && strcmp(m->children[i]->name, name) == 0) {
// 删除该成员及其所有子孙节点
free(m->children[i]);
m->children[i] = NULL;
return;
}
}
m = m->children[0];
}
printf("Error: member not found.\n");
}
// 修改成员
void modify_member_handler(Dept *head) {
char dept_name[MAX_DEPT_LEN];
printf("Please enter the department name: ");
scanf("%s", dept_name);
Dept *d = find_dept(head, dept_name);
if (d == NULL) {
printf("Error: department not found.\n");
return;
}
char name[MAX_NAME_LEN];
printf("Please enter the member name: ");
scanf("%s", name);
Member *m = d->head;
while (m != NULL) {
if (strcmp(m->name, name) == 0) {
// 修改成员信息
printf("Please enter the new member age: ");
scanf("%d", &(m->age));
return;
}
for (int i = 0; i < MAX_CHILDREN; i++) {
if (m->children[i] != NULL && strcmp(m->children[i]->name, name) == 0) {
// 修改成员信息
printf("Please enter the new member age: ");
scanf("%d", &(m->children[i]->age));
return;
}
}
m = m->children[0];
}
printf("Error: member not found.\n");
}
// 查找成员
void search_member_handler(Dept *head) {
char dept_name[MAX_DEPT_LEN];
printf("Please enter the department name: ");
scanf("%s", dept_name);
Dept *d = find_dept(head, dept_name);
if (d == NULL) {
printf("Error: department not found.\n");
return;
}
char name[MAX_NAME_LEN];
printf("Please enter the member name: ");
scanf("%s", name);
Member *m = d->head;
while (m != NULL) {
if (strcmp(m->name, name) == 0) {
print_member(m);
return;
}
for (int i = 0; i < MAX_CHILDREN; i++) {
if (m->children[i] != NULL && strcmp(m->children[i]->name, name) == 0) {
print_member(m->children[i]);
return;
}
}
m = m->children[0];
}
printf("Error: member not found.\n");
}
int main() {
// 创建数信学院
Dept *sx = create_dept("数信学院");
// 添加部门
Dept *bm = create_dept("部门1");
add_dept(sx, bm);
// 创建成员
Member *m1 = create_member("成员1", 20, "部门1");
Member *m2 = create_member("成员2", 21, "部门1");
Member *m3 = create_member("成员3", 22, "部门1");
// 添加成员
add_member(m1, m2);
add_member(m1, m3);
bm->head = m1;
// 输出数信学院
print_dept(sx);
// 执行操作
int choice = 0;
while (1) {
printf("\nPlease select an operation:\n");
printf("1. Add member\n");
printf("2. Delete member\n");
printf("3. Modify member\n");
printf("4. Search member\n");
printf("5. Print members by department\n");
printf("6. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_member_handler(sx);
break;
case 2:
delete_member_handler(sx);
break;
case 3:
modify_member_handler(sx);
break;
case 4:
search_member_handler(sx);
break;
case 5:
print_dept(sx);
break;
case 6:
return 0;
default:
printf("Error: invalid choice.\n");
break;
}
}
// 释放内存
free(m3);
free(m2);
free(m1);
free(bm);
free(sx);
return 0;
}
```
这个程序使用孩子兄弟表示法来存储学生会组织的树形结构,每个部门是一个链表,每个学生是一个节点,每个节点有一个指向它的子节点的指针数组。可以通过调用相应的函数来构造树形结构,添加、删除、修改、查找成员和部门,输出组织成员信息等。程序中加入了一个循环,可以根据用户输入执行相应的操作。
阅读全文