c语音建立员工结构体WORKER包含5个员工数据。成员包括:姓名,编号,年龄,工资。
时间: 2024-05-16 09:14:44 浏览: 130
可以使用以下代码创建一个包含5个员工数据的员工结构体:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 50
struct worker {
char name[MAX_NAME_LEN];
int id;
int age;
float salary;
};
int main() {
struct worker employees[5];
// Add employee data
strcpy(employees[0].name, "John");
employees[0].id = 1001;
employees[0].age = 25;
employees[0].salary = 5000.0;
strcpy(employees[1].name, "Mary");
employees[1].id = 1002;
employees[1].age = 30;
employees[1].salary = 6000.0;
strcpy(employees[2].name, "Bob");
employees[2].id = 1003;
employees[2].age = 35;
employees[2].salary = 7000.0;
strcpy(employees[3].name, "Alice");
employees[3].id = 1004;
employees[3].age = 40;
employees[3].salary = 8000.0;
strcpy(employees[4].name, "Tom");
employees[4].id = 1005;
employees[4].age = 45;
employees[4].salary = 9000.0;
// Print employee data
for (int i = 0; i < 5; i++) {
printf("Name: %s\n", employees[i].name);
printf("ID: %d\n", employees[i].id);
printf("Age: %d\n", employees[i].age);
printf("Salary: %.2f\n\n", employees[i].salary);
}
return 0;
}
```
在此示例中,我们定义了一个名为 `worker` 的结构体,其中包含了员工的姓名、编号、年龄和工资。我们使用 `strcpy` 函数将员工数据添加到 `employees` 数组中,然后使用循环遍历数组并打印出每个员工的数据。
阅读全文