void get_all_person(struct person p[], int n) { int i; for(i=0;i<n;i++){ struct birth b=get_birth(p[i].ID); if(checkID(p[i].ID)){ p[i].birthday=b; p[i].flag=1; } else{ p[i].flag=0; } } }解释
时间: 2023-06-24 10:08:25 浏览: 98
这是一个定义的函数,函数名为 get_all_person。该函数接受两个参数,一个是一个结构体数组 p,另一个是一个整数 n,表示这个结构体数组中有多少个元素。
函数的作用是遍历这个结构体数组 p 中的每一个元素,获取每个人的出生日期,并将其保存在一个名为 birthday 的结构体变量中。函数调用了一个名为 checkID 的函数来检查每个人的身份证号是否合法,如果身份证号合法,则将出生日期保存在结构体变量 birthday 中,并将该人的 flag 标志位设置为 1,表示已经成功获取了该人的出生日期。如果身份证号不合法,则将该人的 flag 标志位设置为 0,表示无法获取该人的出生日期。
需要注意的是,该函数中调用了一个名为 get_birth 的函数来获取每个人的出生日期,但是该函数的具体实现并没有被展示出来。可能是在其他地方已经定义好了该函数。
相关问题
用c编写程序开始时,自动从文件读入信息到结构体数组,并提取每个公民的出生年月放入结构体中,同时进行身份证校验位是否正确,判断信息也放入结构体中。补全主函数,#include <stdio.h> #include <string.h> #define N 100 struct birth { int year; int month; int day; }; struct person { char ID[19]; char name[20]; struct birth birthday; int flag;//校验位是否正确, 1代表正确,0代表错误 }; int read(struct person p[]); int checkID(char *ID); struct birth get_birth(char *ID); void get_all_person(struct person p[], int n); void display_person(struct person p[], int n, int flag); int birth_cmp(struct birth birth1, struct birth birth2); void birth_sort(struct person p[], int n); int search(struct person p[], int n, char *name); void save(struct person p[], int n); int prompt(void); int main(void) { struct person p[N]; int cmd, n; while (1) { cmd = prompt(); } return 0; }
补全的代码如下:
```
#include <stdio.h>
#include <string.h>
#define N 100
struct birth {
int year;
int month;
int day;
};
struct person {
char ID[19];
char name[20];
struct birth birthday;
int flag; // 校验位是否正确, 1代表正确,0代表错误
};
int read(struct person p[]);
int checkID(char *ID);
struct birth get_birth(char *ID);
void get_all_person(struct person p[], int n);
void display_person(struct person p[], int n, int flag);
int birth_cmp(struct birth birth1, struct birth birth2);
void birth_sort(struct person p[], int n);
int search(struct person p[], int n, char *name);
void save(struct person p[], int n);
int prompt(void);
int main(void) {
struct person p[N];
int cmd, n;
while (1) {
cmd = prompt();
switch (cmd) {
case 1:
n = read(p);
break;
case 2:
get_all_person(p, n);
break;
case 3:
birth_sort(p, n);
break;
case 4:
display_person(p, n, 1);
break;
case 5:
display_person(p, n, 0);
break;
case 6:
save(p, n);
break;
case 7:
return 0;
default:
printf("Invalid command!\n");
break;
}
}
return 0;
}
int read(struct person p[]) {
FILE *fp;
char filename[20];
int i = 0;
printf("Enter the file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (!fp) {
printf("Failed to open file!\n");
return 0;
}
while (fscanf(fp, "%s %s", p[i].ID, p[i].name) != EOF) {
p[i].birthday = get_birth(p[i].ID);
p[i].flag = checkID(p[i].ID);
i++;
}
fclose(fp);
printf("Read file successfully!\n");
return i;
}
int checkID(char *ID) {
int i, sum = 0, weight;
int weight_list[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
char check_list[] = "10X98765432";
for (i = 0; i < 17; i++) {
sum += (ID[i] - '0') * weight_list[i];
}
weight = sum % 11;
if (ID[17] == check_list[weight]) {
return 1;
} else {
return 0;
}
}
struct birth get_birth(char *ID) {
struct birth birthday;
birthday.year = (ID[6] - '0') * 1000 + (ID[7] - '0') * 100 + (ID[8] - '0') * 10 + (ID[9] - '0');
birthday.month = (ID[10] - '0') * 10 + (ID[11] - '0');
birthday.day = (ID[12] - '0') * 10 + (ID[13] - '0');
return birthday;
}
void get_all_person(struct person p[], int n) {
int i;
printf("All persons:\n");
for (i = 0; i < n; i++) {
printf("%s %s %d-%02d-%02d %s\n", p[i].ID, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day, p[i].flag ? "correct" : "incorrect");
}
}
void display_person(struct person p[], int n, int flag) {
int i;
printf("Persons with %s ID:\n", flag ? "correct" : "incorrect");
for (i = 0; i < n; i++) {
if (p[i].flag == flag) {
printf("%s %s %d-%02d-%02d\n", p[i].ID, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day);
}
}
}
int birth_cmp(struct birth birth1, struct birth birth2) {
if (birth1.year != birth2.year) {
return birth1.year - birth2.year;
} else if (birth1.month != birth2.month) {
return birth1.month - birth2.month;
} else {
return birth1.day - birth2.day;
}
}
void birth_sort(struct person p[], int n) {
int i, j;
struct person temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (birth_cmp(p[i].birthday, p[j].birthday) > 0) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
printf("Sort by birthday successfully!\n");
}
int search(struct person p[], int n, char *name) {
int i;
for (i = 0; i < n; i++) {
if (strcmp(p[i].name, name) == 0) {
return i;
}
}
return -1;
}
void save(struct person p[], int n) {
FILE *fp;
char filename[20];
int i;
printf("Enter the file name: ");
scanf("%s", filename);
fp = fopen(filename, "w");
if (!fp) {
printf("Failed to create file!\n");
return;
}
for (i = 0; i < n; i++) {
fprintf(fp, "%s %s %d-%02d-%02d %s\n", p[i].ID, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day, p[i].flag ? "correct" : "incorrect");
}
fclose(fp);
printf("Save file successfully!\n");
}
int prompt(void) {
int cmd;
printf("\nMenu:\n");
printf("1. Read from file\n");
printf("2. Display all persons\n");
printf("3. Sort by birthday\n");
printf("4. Display persons with correct ID\n");
printf("5. Display persons with incorrect ID\n");
printf("6. Save to file\n");
printf("7. Quit\n");
printf("Enter a command: ");
scanf("%d", &cmd);
return cmd;
}
```
在上述条件下给出void get_all_person (struct person p[],int n); 获取所有公民的出生年月和身份证校验位是否正确的函数。其中,结构体数组 p 中已经保存 n 个公民身份证和姓名,函数将从每个身份证号码中获得出生年、月、日,判定身份证校验位是否正确,并保存至结构体中。
可以使用以下代码获取所有公民的出生年月和身份证校验位是否正确的函数:
```c++
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct birth { int year, month, day; };
struct person {char ID[19], name[20]; struct birth birthday; int flag; };
int checkID(char id[]) {
int i, sum = 0;
int weight[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char check_code[] = "10X98765432";
for (i = 0; i < 17; i++) {
sum += (id[i] - '0') * weight[i];
}
int mod = sum % 11;
char check = check_code[mod];
if (check == id[17]) {
return 1; // 校验位正确
} else {
return 0; // 校验位错误
}
}
struct birth get_birthday_from_ID(char *ID) {
struct birth birthday;
char year_str[5], month_str[3], day_str[3];
memset(&birthday, 0, sizeof(struct birth));
memset(year_str, 0, sizeof(year_str));
memset(month_str, 0, sizeof(month_str));
memset(day_str, 0, sizeof(day_str));
strncpy(year_str, ID+6, 4);
strncpy(month_str, ID+10, 2);
strncpy(day_str, ID+12, 2);
birthday.year = atoi(year_str);
birthday.month = atoi(month_str);
birthday.day = atoi(day_str);
return birthday;
}
void get_all_person(struct person p[], int n) {
int i;
for (i = 0; i < n; i++) {
p[i].birthday = get_birthday_from_ID(p[i].ID);
p[i].flag = checkID(p[i].ID);
}
}
int main() {
struct person p[2];
strcpy(p[0].ID, "123456197001010101");
strcpy(p[0].name, "张三");
strcpy(p[1].ID, "123456197001010102");
strcpy(p[1].name, "李四");
get_all_person(p, 2);
int i;
for (i = 0; i < 2; i++) {
printf("name: %s, birthday: %d-%d-%d, flag: %d\n", p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day, p[i].flag);
}
return 0;
}
```
函数get_all_person接受一个person结构体数组p和整型n,将从每个身份证号中获得出生年、月、日,判定身份证校验位是否正确,并保存至结构体中。在函数内部,使用for循环遍历结构体数组p中的每个元素,调用get_birthday_from_ID函数获取出生年月,并调用checkID函数判断身份证校验位是否正确,最后将结果保存至结构体中。
阅读全文