#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 员工结构体
struct Employee {
char name[100];
int age;
char position[50];
char contact[20];
struct Employee *child; // 指向第一个子节点
struct Employee *sibling; // 指向下一个同级节点
struct Employee *supervisor; // 指向上级节点
};
// 创建员工
struct Employee* createEmployee(const char *name, int age, const char *position, const char *contact)
{
struct Employee* newEmployee = (struct Employee*)malloc(sizeof(struct Employee));
if (newEmployee == NULL)
{
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
strcpy(newEmployee->name, name);
newEmployee->age = age;
strcpy(newEmployee->position, position);
strcpy(newEmployee->contact, contact);
newEmployee->child = NULL;
newEmployee->sibling = NULL;
return newEmployee;
}
// 添加员工,将员工添加为指定领导的下属
/*void addEmployee(struct Employee *leader, struct Employee *employee)
{
if (leader == NULL)
{
printf("Error: Leader not found\n");
return;
}
if (leader->child == NULL)
{
leader->child = employee;
}
else
{
struct Employee *sibling = leader->child;
while (sibling->sibling != NULL)
{
sibling = sibling->sibling;
}
sibling->sibling = employee;
}
}
*/
void addEmployee(struct Employee *parent, struct Employee *child)
{
if(child == NULL)
{
return ;
}
if (parent->child == NULL)
{
parent->child = child;
}
else
{
struct Employee *sibling = parent->child;
while (sibling->sibling != NULL)
{
sibling = sibling->sibling;
}
sibling->sibling = child;
}
child->supervisor = parent;
}
// 删除员工
void deleteEmployee(struct Employee *leader, const char *name)
{
if (leader == NULL || leader->child == NULL)
{
printf("Error: Employee not found\n");
return;
}
struct Employee *current = leader->child;
struct Employee *prev = NULL;
while (current != NULL && strcmp(current->name, name) != 0)
{
prev = current;
current = current->sibling;
}
if (current == NULL)
{
printf("Error: Employee not found\n");
return;
}
if (prev == NULL)
{
leader->child = current->sibling;
}
else
{
prev->sibling = current->sibling;
}
free(current);
}
// 修改员工信息
void modifyEmployee(struct Employee *employee, int age, const char *contact)
{
if (employee == NULL)
{
printf("Error: Employee not found\n");
return;
}
employee->age = age;
strcpy(employee->contact, contact);
}
// 查询员工
struct Employee* findstaff(struct Employee *root, const char *name)
{
if (root == NULL)
{
return NULL;
}
struct Employee *current = root->child;
while (current != NULL)
{
//根据职位查找同事
if (strcmp(current->name, name) == 0)
{
return current;
}
struct Employee *result = findstaff(current, name);
if (result != NULL)
{
return result;
}
current = current->sibling;
}
return NULL;
}
// 查询员工的领导
struct Employee* findLeader(struct Employee *root, const char *name)
{
struct Employee *staff = findstaff(root,name);
if(staff == NULL)
{
//printf("没有此员工\n");
return NULL;
}
if (root == NULL)
{
return NULL;
}
if (strcmp(root->name, name) == 0)
{
return NULL; // 根节点没有领导
}
struct Employee *current = root->child;
while (current != NULL)
{
if (strcmp(current->name, name) == 0)
{
return root;
}
struct Employee *result = findLeader(current, name);
if (result != NULL)
{
return result;
}
current = current->sibling;
}
return NULL;
}
struct Employee* findColleague(struct Employee *root, const char *name)
{
char position[50];
struct Employee *staff = findstaff(root,name);
if (root == NULL)
{
return NULL;
}
struct Employee *current = root->child;
while (current != NULL)
{
//根据职位查找同事
if (strcmp(current->position, staff->position) == 0)
{
return current;
}
struct Employee *result = findColleague(current, name);
if (result != NULL)
{
return result;
}
current = current->sibling;
}
return NULL;
}
// 查询员工的下属
struct Employee* findSubordinate(struct Employee *root, const char *name)
{
if (root == NULL)
{
return NULL;
}
struct Employee *current = root->child;
while (current != NULL)
{
if (strcmp(current->name, name) == 0)
{
return current->child;
}
struct Employee *result = findSubordinate(current, name);
if (result != NULL)
{
return result;
}
current = current->sibling;
}
return NULL;
}
// 输出员工信息
void printEmployee(struct Employee *employee)
{
if (employee != NULL) {
printf("姓名: %s\n", employee->name);
printf("年龄: %d\n", employee->age);
printf("职位: %s\n", employee->position);
printf("联系方式: %s\n", employee->contact);
printf("--------------------\n");
}
}
// 按职位高低输出所有员工,
void printEmployeesByPosition(struct Employee *root)
{
//递归的终止条件,确保不会对空节点
if (root != NULL)
{
printEmployee(root);//根
printEmployeesByPosition(root->child);//左孩
printEmployeesByPosition(root->sibling);//右孩
}
}
// 输出所有超过35岁的员工,递归查找,先序遍历
void printEmployeesOver35(struct Employee *root)
{
if (root != NULL)
{
//判断条件
if (root->age > 35)
{
printEmployee(root);
}
printEmployeesOver35(root->child);
printEmployeesOver35(root->sibling);
}
}
// 统计公司里面的所有员工数量
int countEmployees(struct Employee *root)
{
if (root == NULL)
{
return 0;
}
return 1 + countEmployees(root->child) + countEmployees(root->sibling);
}
//释放申请的内存
void freeEmployee(struct Employee *root)
{
if (root == NULL)
{
return;
}
freeEmployee(root->child);
freeEmployee(root->sibling);
free(root);
}
// 修改员工信息
void modifyEmployee(struct Employee *orgChart, const char *employeeName, const char *newContact)
{
struct Employee *current = orgChart->child;
while (current != NULL && strcmp(current->name, employeeName) != 0) {
current = current->sibling;
}
if (current != NULL) {
strcpy(current->contact, newContact);
}
}
void print_usage()
{
printf("公司组织架构管理\n");
printf("1.员工的添加\n");
printf("2.修改员工信息\n");
printf("3.查询员工的领导、同事和下属等信息\n");
printf("4.按职位高低输出所有员工\n");
printf("5.输出所有超过35岁的员工\n");
printf("6.统计公司里面的所有员工数量\n");
printf("7.删除员工\n");
return ;
}
int main()
{
//创建员工
struct Employee *Chairman = createEmployee("Zhao", 40, "Chairman", "123456789");
struct Employee *ceo = createEmployee("Qian", 38, "CEO", "987654321");
struct Employee *director1 = createEmployee("Sun", 36, "Director", "111222333");
struct Employee *director2= createEmployee("Sun_1", 36, "Director", "111222333");
struct Employee *manager = createEmployee("Li", 33, "Manager", "444555666");
struct Employee *staff1 = createEmployee("Zhou", 30, "Staff", "777888999");
struct Employee *staff2 = createEmployee("Wu", 28, "Staff", "666555444");
// 创建公司结构
addEmployee(Chairman, ceo);
addEmployee(ceo, director1);
addEmployee(ceo, director2);
addEmployee(director2, manager);
addEmployee(manager, staff1);
addEmployee(manager, staff2);
int cmdix;
print_usage();
while(1)
{
printf("请输入需要执行的操作\n");
scanf("%d",&cmdix);
switch (cmdix)
{
case 0:
print_usage();
break;
case 1:
{
char name[10];
int age;
char position[50];
char contack[50];
printf("输入需要添加的员工信息\n");
printf("姓名:");
scanf("%s",name);
printf("年龄:");
scanf("%d",&age);
printf("职位");
scanf("%s",position);
printf("联系方式:");
scanf("%s",contack);
struct Employee *staff3 = createEmployee(name, age, position, contack);
addEmployee(manager, staff3);
//添加员工默认添加在主管下面
break;
}
case 2:
{
char name[50];
int age;
char contack[50];
printf("修改员工信息\n");
printf("输入需要修改员工姓名:\n");
scanf("%s",name);
struct Employee *tmp=findstaff(Chairman, name);
printEmployee(tmp);
printf("输入需要修改的年龄和联系方式,不需要修改年龄保持一致即可\n");
printf("输入年龄\n");
scanf("%d",&age);
printf("输入联系方式\n");
scanf("%s",contack);
modifyEmployee(tmp, age,contack);
//printf("\n%s's Modified Information:\n", staff2->name);
printEmployee(tmp);
break;
}
case 3:
{
char staff_name[24];
printf("查询员工的领导、同事和下属等信息\n");
printf("输入员工姓名:");
scanf("%s",staff_name);
//struct Employee *employee = staff1;
if(findstaff(Chairman, staff_name)==NULL)
{
printf("没有此员工,退出\n");
return -1;
}
struct Employee *leader = findLeader(Chairman, staff_name);
if (leader != NULL)
{
printf("%s's Leader: %s\n", staff_name, leader->name);
}
else
{
printf("%s has no leader\n", staff_name);
}
// 查询员工同事
struct Employee *colleague = findColleague(Chairman, staff_name);
if (colleague != NULL)
{
printf("%s's Colleague: %s\n", staff_name, colleague->name);
}
else
{
printf("%s has no colleague\n", staff_name);
}
// 查询员工下属
struct Employee *subordinate = findSubordinate(Chairman, staff_name);
if (subordinate != NULL)
{
printf("%s's Subordinates:\n", staff_name);
printEmployeesByPosition(subordinate);
}
else
{
printf("%s has no subordinates\n", staff_name);
}
break;
}
case 4:
{
printf("按职位高低输出所有员工\n");
printEmployeesByPosition(Chairman);
printf("\n");
break;
}
case 5:
{
printf("输出所有超过35岁的员工\n");
printEmployeesOver35(Chairman);
break;
}
case 6:
{
printf("统计公司里面的所有员工数量\n");
int totalEmployees = countEmployees(Chairman);
printf("所有员工: %d\n", totalEmployees);
break;
}
case 7:
{
printf("删除员工\n");
char name[50];
printf("输入入需要删除的员工姓名:");
scanf("%s",name);
deleteEmployee(manager, name);
break;
}
default:
print_usage();
break;
}
}
/*
// 构建公司结构
addEmployee(ceo, cxo);
addEmployee(cxo, director);
addEmployee(director, manager);
addEmployee(manager, staff1);
addEmployee(manager, staff2);
// 输出所有员工信息
printf("All Employees:\n");
printEmployeesByPosition(ceo);
printf("\n");
// 输出所有超过35岁的员工
printf("Employees Over 35:\n");
printEmployeesOver35(ceo);
printf("\n");
// 统计公司员工数量
int totalEmployees = countEmployees(ceo);
printf("Total Employees: %d\n", totalEmployees);
// 示例查询员工领导、同事和下属信息
printf("\nExample Queries:\n");
// 查询员工领导
struct Employee *employee = staff1;
struct Employee *leader = findLeader(ceo, employee->name);
if (leader != NULL) {
printf("%s's Leader: %s\n", employee->name, leader->name);
} else {
printf("%s has no leader\n", employee->name);
}
// 查询员工同事
struct Employee *colleague = findColleague(ceo, employee->name);
if (colleague != NULL) {
printf("%s's Colleague: %s\n", employee->name, colleague->name);
} else {
printf("%s has no colleague\n", employee->name);
}
// 查询员工下属
struct Employee *subordinate = findSubordinate(ceo, employee->name);
if (subordinate != NULL) {
printf("%s's Subordinates:\n", employee->name);
printEmployeesByPosition(subordinate);
} else {
printf("%s has no subordinates\n", employee->name);
}
// 修改员工信息
modifyEmployee(staff2, 29, "newcontact@company.com");
printf("\n%s's Modified Information:\n", staff2->name);
printEmployee(staff2);
// 删除员工
deleteEmployee(manager, staff1->name);
//printf("\nAfter Deleting %s:\n", staff1->name);
printEmployeesByPosition(manager);
// 释放内存
// ... (在实际应用中,应该递归释放所有员工节点的内存)
*/
freeEmployee(ceo);
return 0;
}
更多资料在:3528019497