有3个职工的信息(包括工号、姓名、性别、工资),放在结构体数组中,要求输出全部职工的信息
时间: 2023-12-19 11:15:08 浏览: 38
好的,这是一个简单的结构体数组遍历问题。以下是一个示例代码:
```c
#include <stdio.h>
struct Employee {
int empNo;
char name[20];
char gender;
float salary;
};
int main() {
// 定义结构体数组
struct Employee employees[3] = {
{1001, "Tom", 'M', 5000.0},
{1002, "Mary", 'F', 6000.0},
{1003, "John", 'M', 5500.0}
};
// 遍历结构体数组并输出
for (int i = 0; i < 3; i++) {
printf("工号: %d, 姓名: %s, 性别: %c, 工资: %.2f\n",
employees[i].empNo, employees[i].name, employees[i].gender, employees[i].salary);
}
return 0;
}
```
输出结果:
```
工号: 1001, 姓名: Tom, 性别: M, 工资: 5000.00
工号: 1002, 姓名: Mary, 性别: F, 工资: 6000.00
工号: 1003, 姓名: John, 性别: M, 工资: 5500.00
```
相关问题
有三个职工的信息,包括工号,姓名,性别,工资,放在结构体数组中,输出职工全部信息
好的,以下是输出职工全部信息的代码示例:
```c
#include <stdio.h>
// 定义职工结构体
struct Employee {
int id; // 工号
char name[20]; // 姓名
char sex; // 性别
float salary; // 工资
};
int main() {
// 初始化三个职工信息
struct Employee employees[3] = {
{1001, "张三", 'M', 5000.0},
{1002, "李四", 'F', 6000.0},
{1003, "王五", 'M', 7000.0}
};
// 输出职工全部信息
for (int i = 0; i < 3; i++) {
printf("工号:%d\n", employees[i].id);
printf("姓名:%s\n", employees[i].name);
printf("性别:%c\n", employees[i].sex);
printf("工资:%.2f\n", employees[i].salary);
}
return 0;
}
```
输出结果如下:
```
工号:1001
姓名:张三
性别:M
工资:5000.00
工号:1002
姓名:李四
性别:F
工资:6000.00
工号:1003
姓名:王五
性别:M
工资:7000.00
```
有3个职工的信息(包括工号、姓名、性别、工资),放在结构体数组中,要求输出全部职工的信息。
好的,这个问题很简单。以下是一个示例代码,其中定义了一个名为 `Employee` 的结构体,包含了工号、姓名、性别、工资等信息,然后定义了一个结构体数组 `employees`,将三个职工的信息存储在其中,最后通过遍历结构体数组输出所有职工的信息。
```c
#include <stdio.h>
// 定义结构体 Employee
struct Employee {
int id;
char name[20];
char gender;
float salary;
};
int main() {
// 定义结构体数组 employees
struct Employee employees[3] = {
{1001, "张三", 'M', 5000.0},
{1002, "李四", 'F', 6000.0},
{1003, "王五", 'M', 7000.0}
};
// 输出所有职工的信息
for (int i = 0; i < 3; i++) {
printf("工号:%d,姓名:%s,性别:%c,工资:%.2f\n", employees[i].id, employees[i].name, employees[i].gender, employees[i].salary);
}
return 0;
}
```
输出结果如下:
```
工号:1001,姓名:张三,性别:M,工资:5000.00
工号:1002,姓名:李四,性别:F,工资:6000.00
工号:1003,姓名:王五,性别:M,工资:7000.00
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)