用C语言写一个含链表的职工工作量统计系统的代码
时间: 2024-01-22 08:18:17 浏览: 29
以下是一个简单的职工工作量统计系统的代码,使用了链表来存储职工信息:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 职工结构体
typedef struct Employee {
char name[20]; // 姓名
int work_hours; // 工作时长
int salary; // 工资
struct Employee *next; // 指向下一个职工的指针
} Employee;
// 添加职工函数
void add_employee(Employee **head) {
Employee *new_employee = (Employee *)malloc(sizeof(Employee));
printf("请输入职工姓名:");
scanf("%s", new_employee->name);
printf("请输入职工工作时长:");
scanf("%d", &new_employee->work_hours);
new_employee->salary = new_employee->work_hours * 100; // 简单计算工资
new_employee->next = *head; // 新职工指向原头节点
*head = new_employee; // 新职工成为新的头节点
}
// 显示所有职工信息函数
void show_employees(Employee *head) {
printf("所有职工信息如下:\n");
Employee *current_employee = head;
while (current_employee != NULL) {
printf("姓名:%s\t工作时长:%d小时\t工资:%d元\n",
current_employee->name, current_employee->work_hours, current_employee->salary);
current_employee = current_employee->next; // 移动到下一个职工节点
}
}
// 统计工资总额函数
int calculate_total_salary(Employee *head) {
int total_salary = 0;
Employee *current_employee = head;
while (current_employee != NULL) {
total_salary += current_employee->salary;
current_employee = current_employee->next; // 移动到下一个职工节点
}
return total_salary;
}
int main() {
Employee *head = NULL; // 头节点初始化为 NULL
int choice;
do {
printf("\n请选择操作:\n");
printf("1. 添加职工信息\n");
printf("2. 显示所有职工信息\n");
printf("3. 统计工资总额\n");
printf("4. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_employee(&head);
break;
case 2:
show_employees(head);
break;
case 3:
printf("工资总额为:%d元\n", calculate_total_salary(head));
break;
case 4:
printf("程序已退出。\n");
break;
default:
printf("无效的选择,请重新选择。\n");
}
} while (choice != 4);
// 释放内存
Employee *current_employee = head;
while (current_employee != NULL) {
Employee *next_employee = current_employee->next;
free(current_employee);
current_employee = next_employee;
}
return 0;
}
```
这个程序使用了一个 `Employee` 结构体来存储每个职工的信息,包括姓名、工作时长和工资。链表的头节点 `head` 初始化为 `NULL`,每次添加新职工时,新节点会成为新的头节点,而原来的头节点则成为新节点的下一个节点。在显示所有职工信息、统计工资总额等操作中,程序会遍历整个链表来访问每个职工节点的信息。当程序退出时,需要释放链表中所有的节点内存。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)