【C Language| Data Structure】Structure

2 阅读1分钟

金石计划

Structure

定义结构体

输入 逐个输出

定义一个结构体,其中包括:职工号、职工名、年龄、工资、地址。按结构体类型定义一个结构体数组.从键盘输入每个结构体元素所需的数据.然后逐个输出这些元素(可设数组只有三个元素)。

`// 定义一个结构体,包括职工号、职工姓名、年龄、工资、地址`

`struct` `Employee {`

`    ``int` `empNo;`

`    ``char` `name[20];`

`    ``int` `age;`

`    ``float` `salary;`

`    ``char` `address[100];`

`};`

`int` `main() {`

`    ``// 声明结构体数组`

`    ``struct` `Employee employees[3];`

`    ``// 循环输入员工信息`

`    ``int` `i;`

`    ``for``(i = 0; i < 3; i++) {`

`        ``scanf``(``"%d %s %d %f %s"``, &employees[i].empNo,`

`              ``employees[i].name, &employees[i].age, &employees[i].salary,`

`              ``employees[i].address);`

`    ``}`

`    ``// 输出员工信息`

`    ``printf``(``"\n职工信息如下:\n"``);`

`    ``for``(i = 0; i < 3; i++) {`

`        ``printf``(``"职工号:%d\n"``, employees[i].empNo);`

`        ``printf``(``"职工姓名:%s\n"``,employees[i].name);`

`        ``printf``(``"年龄:%d\n"``, employees[i].age);`

`        ``printf``(``"工资:%.2f\n"``, employees[i].salary);`

`        ``printf``(``"地址:%s\n"``, employees[i].address);`

`        ``printf``(``"\n"``);`

`    ``}`

`    ``return` `0;`

`}`