c
复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Company
{
char *name;
struct Company **children;
size_t child_count;
void (*add)(struct Company *, struct Company *);
void (*remove)(struct Company *);
void (*display)(struct Company *, int);
void (*lineOfDuty)(struct Company *);
} Company;
typedef struct
{
Company company;
void (*lineOfDuty)(Company *);
} LeafCompany;
void company_add(Company *company, Company *child);
void company_display(Company *company, int depth);
void company_lineOfDuty(Company *company);
void company_init(Company *company, const char *name)
{
company->name = strdup(name);
company->children = NULL;
company->child_count = 0;
company->add = &company_add;
company->remove = NULL; // 实现时需要具体逻辑
company->display = &company_display;
company->lineOfDuty = &company_lineOfDuty;
}
void company_free(Company *company)
{
free(company->name);
size_t i;
for (i = 0; i < company->child_count; ++i)
{
company_free(company->children[i]);
}
free(company->children);
free(company);
}
void company_add(Company *company, Company *child)
{
company->children = realloc(company->children, sizeof(Company *) * (company->child_count + 1));
company->children[company->child_count++] = child;
}
void company_display(Company *company, int depth)
{
char s[depth + 1];
memset(s, '-', depth);
s[depth] = '\0';
printf("%s%s\n", s, company->name);
size_t i;
for (i = 0; i < company->child_count; ++i)
{
company->children[i]->display(company->children[i], depth + 2);
}
}
void company_lineOfDuty(Company *company)
{
size_t i;
for (i = 0; i < company->child_count; ++i)
{
company->children[i]->lineOfDuty(company->children[i]);
}
}
void leaf_company_init(LeafCompany *leaf, const char *name, void (*duty)(Company *))
{
leaf->company.name = strdup(name);
leaf->company.add = NULL;
leaf->company.remove = NULL;
leaf->company.display = &company_display;
leaf->company.lineOfDuty = duty;
leaf->lineOfDuty = duty;
}
void hr_department_lineOfDuty(Company *company)
{
printf("%s 员工招聘培训管理\n", company->name);
}
void finance_department_lineOfDuty(Company *company)
{
printf("%s 公司财务收支管理\n", company->name);
}
int main()
{
Company *root = malloc(sizeof(Company));
company_init(root, "北京总公司");
LeafCompany *hr_root = malloc(sizeof(LeafCompany));
leaf_company_init(hr_root, "总公司人力资源部", hr_department_lineOfDuty);
root->add(root, (Company *)hr_root);
LeafCompany *finance_root = malloc(sizeof(LeafCompany));
leaf_company_init(finance_root, "总公司财务部", finance_department_lineOfDuty);
root->add(root, (Company *)finance_root);
Company *shanghai_comp = malloc(sizeof(Company));
company_init(shanghai_comp, "上海华东分公司");
root->add(root, shanghai_comp);
LeafCompany *hr_shanghai = malloc(sizeof(LeafCompany));
leaf_company_init(hr_shanghai, "华东分公司人力资源部", hr_department_lineOfDuty);
shanghai_comp->add(shanghai_comp, (Company *)hr_shanghai);
LeafCompany *finance_shanghai = malloc(sizeof(LeafCompany));
leaf_company_init(finance_shanghai, "华东分公司财务部", finance_department_lineOfDuty);
shanghai_comp->add(shanghai_comp, (Company *)finance_shanghai);
Company *nanjing_office = malloc(sizeof(Company));
company_init(nanjing_office, "南京办事处");
shanghai_comp->add(shanghai_comp, nanjing_office);
LeafCompany *hr_nanjing = malloc(sizeof(LeafCompany));
leaf_company_init(hr_nanjing, "南京办事处人力资源部", hr_department_lineOfDuty);
nanjing_office->add(nanjing_office, (Company *)hr_nanjing);
LeafCompany *finance_nanjing = malloc(sizeof(LeafCompany));
leaf_company_init(finance_nanjing, "南京办事处财务部", finance_department_lineOfDuty);
nanjing_office->add(nanjing_office, (Company *)finance_nanjing);
printf("结构图:\n");
root->display(root, 1);
printf("\n职责图:\n");
root->lineOfDuty(root);
company_free(root); // 释放根节点,递归释放所有子节点
return 0;
}