使用结构数组存放下表中的员工的工资数据,然后用结构指针输出每个员工的姓名及应发工资数。 应发工资=基本工资+岗位津贴-扣款 姓名 基本工资 岗位津贴 扣款 liqun dongfang majing liudong 870.00 1250.00 1520.00 2050.00 500.00 800.00 1000.00 1500.00 85.00 63.00 72.00 120.00
时间: 2024-03-18 15:40:47 浏览: 42
好的,这是一个比较简单的结构体数组操作,可以按照以下步骤进行:
1. 定义结构体,包含姓名、基本工资、岗位津贴、扣款和应发工资字段。
```
struct Employee {
char name[20];
float basic_salary;
float position_allowance;
float deduction;
float gross_salary;
};
```
2. 定义结构体数组,初始化每个员工的数据。
```
struct Employee employees[] = {
{"liqun", 870.00, 500.00, 85.00},
{"dongfang", 1250.00, 800.00, 63.00},
{"majing", 1520.00, 1000.00, 72.00},
{"liudong", 2050.00, 1500.00, 120.00}
};
```
3. 计算每个员工的应发工资,并保存到结构体数组中。
```
for (int i = 0; i < 4; i++) {
float gross_salary = employees[i].basic_salary + employees[i].position_allowance - employees[i].deduction;
employees[i].gross_salary = gross_salary;
}
```
4. 定义结构指针,遍历结构体数组,输出每个员工的姓名和应发工资。
```
struct Employee *p;
for (p = employees; p < employees + 4; p++) {
printf("%s\t%.2f\n", p->name, p->gross_salary);
}
```
完整代码如下:
```
#include <stdio.h>
struct Employee {
char name[20];
float basic_salary;
float position_allowance;
float deduction;
float gross_salary;
};
int main() {
struct Employee employees[] = {
{"liqun", 870.00, 500.00, 85.00},
{"dongfang", 1250.00, 800.00, 63.00},
{"majing", 1520.00, 1000.00, 72.00},
{"liudong", 2050.00, 1500.00, 120.00}
};
for (int i = 0; i < 4; i++) {
float gross_salary = employees[i].basic_salary + employees[i].position_allowance - employees[i].deduction;
employees[i].gross_salary = gross_salary;
}
struct Employee *p;
for (p = employees; p < employees + 4; p++) {
printf("%s\t%.2f\n", p->name, p->gross_salary);
}
return 0;
}
```
阅读全文