用结构体计算人工工资
时间: 2023-12-03 18:44:23 浏览: 21
好的,假设我们需要计算一名员工的工资,可以使用以下结构体来表示员工的信息:
```c
typedef struct Employee {
char name[20]; // 姓名
int age; // 年龄
float salary; // 基本工资
float bonus; // 奖金
float tax; // 税金
} Employee;
```
现在我们可以编写一个函数来计算员工的工资:
```c
void calculate_salary(Employee e) {
float net_income = e.salary + e.bonus - e.tax;
printf("姓名:%s\n", e.name);
printf("年龄:%d\n", e.age);
printf("基本工资:%.2f\n", e.salary);
printf("奖金:%.2f\n", e.bonus);
printf("税金:%.2f\n", e.tax);
printf("净收入:%.2f\n", net_income);
}
```
在主函数中,我们可以创建一个 Employee 结构体变量并为其赋值,然后将该变量作为参数传递给 calculate_salary 函数:
```c
int main() {
Employee e = {"张三", 25, 5000.0, 1000.0, 800.0};
calculate_salary(e);
return 0;
}
```
该程序将输出以下结果:
```
姓名:张三
年龄:25
基本工资:5000.00
奖金:1000.00
税金:800.00
净收入:5200.00
```