" 在职工管理系统中,实现统计不同部门人数的功能,可以通过定义部门结构体和职工结构体,然后遍历职工列表来实现。以下是一个简单的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 部门结构体
typedef struct {
int id;
char name[20];
} Department;
// 职工结构体
typedef struct {
int id;
char name[20];
Department *department;
} Employee;
// 初始化部门
Department *initDepartment(int id, const char *name) {
Department *department = (Department *)malloc(sizeof(Department));
strcpy(department->name, name);
department->id = id;
return department;
}
// 添加职工到部门
void addEmployeeToDepartment(Department *department, Employee *employee) {
employee->department = department;
department->employees = (Employee *)realloc(department->employees, sizeof(Employee) * (department->employeeCount + 1));
department->employees[department->employeeCount++] = *employee;
}
// 初始化职工
Employee *initEmployee(int id, const char *name, Department *department) {
Employee *employee = (Employee *)malloc(sizeof(Employee));
strcpy(employee->name, name);
employee->id = id;
employee->department = department;
return employee;
}
// 统计部门人数
int countEmployeesInDepartment(Department *department) {
return department->employeeCount;
}
int main() {
Department *department1 = initDepartment(1, "研发部");
Department *department2 = initDepartment(2, "市场部");
Employee employee1 = {1, "张三", department1};
Employee employee2 = {2, "李四", department1};
Employee employee3 = {3, "王五", department2};
Employee employee4 = {4, "赵六", department2};
addEmployeeToDepartment(department1, &employee1);
addEmployeeToDepartment(department1, &employee2);
addEmployeeToDepartment(department2, &employee3);
addEmployeeToDepartment(department2, &employee4);
int department1Count = countEmployeesInDepartment(department1);
int department2Count = countEmployeesInDepartment(department2);
printf("研发部人数:%d\n", department1Count);
printf("市场部人数:%d\n", department2Count);
return 0;
}
```
这个代码示例创建了两个部门(研发部和市场部),并添加了四个职工。通过调用`countEmployeesInDepartment`函数,可以分别统计两个部门的人数。"