用c++语言创建一个结构体ST,成员有num(学号),name(姓名),score(成绩),birthday(出生日期),出生日期是一个名为date的结构体类型,初始化第1个人员的数据信息并赋予变量st1,再把此人信息赋给另一个变量st2,输出两个变量st1及st2的值。
时间: 2023-06-11 13:08:18 浏览: 87
以下是代码实现:
```c
#include <stdio.h>
struct Date {
int year;
int month;
int day;
};
struct ST {
int num;
char name[20];
float score;
struct Date birthday;
};
int main() {
// 初始化第1个人员的数据信息并赋予变量st1
struct ST st1 = { 1001, "Tom", 90.5, { 2000, 1, 1 } };
// 把此人信息赋给另一个变量st2
struct ST st2 = st1;
// 输出两个变量st1及st2的值
printf("st1.num = %d\n", st1.num);
printf("st1.name = %s\n", st1.name);
printf("st1.score = %.2f\n", st1.score);
printf("st1.birthday = %d-%d-%d\n", st1.birthday.year, st1.birthday.month, st1.birthday.day);
printf("\n");
printf("st2.num = %d\n", st2.num);
printf("st2.name = %s\n", st2.name);
printf("st2.score = %.2f\n", st2.score);
printf("st2.birthday = %d-%d-%d\n", st2.birthday.year, st2.birthday.month, st2.birthday.day);
return 0;
}
```
输出结果:
```
st1.num = 1001
st1.name = Tom
st1.score = 90.50
st1.birthday = 2000-1-1
st2.num = 1001
st2.name = Tom
st2.score = 90.50
st2.birthday = 2000-1-1
```
阅读全文