请使用c语言分别将出生日期以及员工信息定义为结构体数据类型,定义结构体变量, 从键盘输入员工信息:姓名,性别和出生日期,计算员工 2023 年的年龄,并输 出所有信息。
时间: 2023-09-20 22:09:07 浏览: 51
以下是使用C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Date {
int year;
int month;
int day;
};
struct Employee {
char name[20];
char gender[10];
struct Date birthdate;
int age;
};
int main() {
struct Employee emp;
printf("请输入员工姓名:");
scanf("%s", emp.name);
printf("请输入员工性别:");
scanf("%s", emp.gender);
printf("请输入员工出生日期(格式为 yyyy mm dd):");
scanf("%d %d %d", &emp.birthdate.year, &emp.birthdate.month, &emp.birthdate.day);
emp.age = 2023 - emp.birthdate.year;
printf("姓名:%s\n", emp.name);
printf("性别:%s\n", emp.gender);
printf("出生日期:%d 年 %d 月 %d 日\n", emp.birthdate.year, emp.birthdate.month, emp.birthdate.day);
printf("2023 年年龄:%d 岁\n", emp.age);
return 0;
}
```
代码中定义了两个结构体类型,`Date` 用于表示日期,`Employee` 用于表示员工信息。在 `main` 函数中,首先定义了一个 `Employee` 结构体变量 `emp`。然后通过 `scanf` 函数从键盘输入员工的姓名、性别以及出生日期,并计算员工 2023 年的年龄。最后输出所有员工信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)
![](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)