利用一个结构体定义一个员工,每个员工拥有一个名字,一个工号(唯一的),月工资。现有四个员工,其信息如下: {"zhao",1001,"35000"},{"qian",1003,"55000"},{"s
时间: 2023-12-31 15:24:55 浏览: 104
可以使用结构体来定义一个员工,每个员工拥有一个名字、一个工号和月工资。以下是一个示例代码:
```c
#include <stdio.h>
struct Employee {
char name[20];
int ID;
double salary;
};
int main() {
struct Employee emp1 = {"zhao", 1001, 35000};
struct Employee emp2 = {"qian", 1003, 55000};
struct Employee emp3 = {"sun", 1005, 45000};
struct Employee emp4 = {"li", 1007, 40000};
printf("Employee 1: Name - %s, ID - %d, Salary - %.2f\n", emp1.name, emp1.ID, emp1.salary);
printf("Employee 2: Name - %s, ID - %d, Salary - %.2f\n", emp2.name, emp2.ID, emp2.salary);
printf("Employee 3: Name - %s, ID - %d, Salary - %.2f\n", emp3.name, emp3.ID, emp3.salary);
printf("Employee 4: Name - %s, ID - %d, Salary - %.2f\n", emp4.name, emp4.ID, emp4.salary);
return 0;
}
```
这段代码定义了一个名为`Employee`的结构体,包含了名字、工号和月工资三个成员变量。然后,我们创建了四个员工对象,并为每个对象赋予相应的值。最后,使用`printf`函数打印出每个员工的信息。
阅读全文