C语言结构体:建立一个同学通讯录。通讯录中包含:姓名、性别、出生日期、手机号这几项。 现需要根据通讯录统计 2000 年后(包括 2000 年)出生你的男生人数,请编程实现这个通讯并完成人数统计要求。
时间: 2024-03-05 08:48:13 浏览: 23
好的,根据您的要求,我来为您编写一个C语言程序来实现这个通讯录并完成人数统计。
首先,我们需要定义一个结构体来表示每一个同学的信息,包含姓名、性别、出生日期和手机号:
```c
struct student {
char name[20];
char gender;
int birth_year;
int birth_month;
int birth_day;
char phone[12];
};
```
接下来,我们可以定义一个数组来存储所有同学的信息:
```c
struct student classmates[100];
```
这里我们假设最多有100个同学。
然后,我们需要输入每一个同学的信息,这里可以使用一个循环来实现:
```c
int count = 0;
for (int i = 0; i < 100; i++) {
printf("请输入第%d个同学的姓名:", i+1);
scanf("%s", classmates[i].name);
printf("请输入第%d个同学的性别(M表示男性,F表示女性):", i+1);
scanf(" %c", &classmates[i].gender);
printf("请输入第%d个同学的出生日期(格式为YYYY/MM/DD):", i+1);
scanf("%d/%d/%d", &classmates[i].birth_year, &classmates[i].birth_month, &classmates[i].birth_day);
printf("请输入第%d个同学的手机号:", i+1);
scanf("%s", classmates[i].phone);
if (classmates[i].birth_year >= 2000 && classmates[i].gender == 'M') {
count++;
}
}
```
这里使用了一个计数器变量`count`来统计2000年后出生的男生人数。
最后,我们可以输出统计结果:
```c
printf("2000年后出生的男生人数为:%d\n", count);
```
完整的程序如下:
```c
#include <stdio.h>
struct student {
char name[20];
char gender;
int birth_year;
int birth_month;
int birth_day;
char phone[12];
};
int main() {
struct student classmates[100];
int count = 0;
for (int i = 0; i < 100; i++) {
printf("请输入第%d个同学的姓名:", i+1);
scanf("%s", classmates[i].name);
printf("请输入第%d个同学的性别(M表示男性,F表示女性):", i+1);
scanf(" %c", &classmates[i].gender);
printf("请输入第%d个同学的出生日期(格式为YYYY/MM/DD):", i+1);
scanf("%d/%d/%d", &classmates[i].birth_year, &classmates[i].birth_month, &classmates[i].birth_day);
printf("请输入第%d个同学的手机号:", i+1);
scanf("%s", classmates[i].phone);
if (classmates[i].birth_year >= 2000 && classmates[i].gender == 'M') {
count++;
}
}
printf("2000年后出生的男生人数为:%d\n", count);
return 0;
}
```
希望能够帮助到您。