060.记录个人资料

举报
C语言与CPP编程 发表于 2022/04/30 23:58:39 2022/04/30
【摘要】 #include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <string.h> struct Family *get_person(void); /* Prototype for input function */...

  
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. struct Family *get_person(void); /* Prototype for input function */
  6. char related(struct Family *pmember1, struct Family *pmember2);
  7. char set_ancestry(struct Family *pmember1, struct Family *pmember2);
  8. struct Date
  9. {
  10. int day;
  11. int month;
  12. int year;
  13. };
  14. struct Family /* Family structure declaration */
  15. {
  16. struct Date dob;
  17. char name[20];
  18. char father[20];
  19. char mother[20];
  20. struct Family *next; /* Pointer to next structure */
  21. struct Family *previous; /* Pointer to previous structure */
  22. struct Family *p_to_pa; /* Pointer to father structure */
  23. struct Family *p_to_ma; /* Pointer to mother structure */
  24. };
  25. void main()
  26. {
  27. struct Family *first = NULL; /* Pointer to first person */
  28. struct Family *current = NULL; /* Pointer to current person */
  29. struct Family *last = NULL; /* Pointer to previous person */
  30. char more = '\0'; /* Test value for ending input */
  31. for( ; ; )
  32. {
  33. printf("\nDo you want to enter details of a%s person (Y or N)? ",
  34. first != NULL?"nother " : "" );
  35. scanf(" %c", &more);
  36. if(tolower(more) == 'n')
  37. break;
  38. current = get_person();
  39. if(first == NULL)
  40. {
  41. first = current; /* Set pointer to first Family */
  42. last = current; /* Remember for next iteration */
  43. }
  44. else
  45. {
  46. last->next = current; /* Set next address for previous Family */
  47. current->previous = last; /* Set previous address for current */
  48. last = current; /* Remember for next iteration */
  49. }
  50. }
  51. current = first;
  52. while(current->next != NULL) /* Check for relation for each person in */
  53. { /* the list up to second to last */
  54. int parents = 0; /* Declare parent count local to this block */
  55. last = current->next; /* Get the pointer to the next */
  56. while(last != NULL) /* This loop tests current person */
  57. { /* against all the remainder in the list */
  58. if(related(current, last)) /* Found a parent ? */
  59. if(++parents == 2) /* Yes, update count and check it */
  60. break; /* Exit inner loop if both parents found */
  61. last = last->next; /* Get the address of the next */
  62. }
  63. current = current->next; /* Next in the list to check */
  64. }
  65. /* Now tell them what we know */
  66. /* Output Family data in correct order */
  67. current = first;
  68. while (current != NULL) /* Output Family data in correct order */
  69. {
  70. printf("\n%s was born %d/%d/%d, and has %s and %s as parents.",
  71. current->name, current->dob.day, current->dob.month,
  72. current->dob. year, current->father, current->mother);
  73. if(current->p_to_pa != NULL )
  74. printf("\n\t%s's birth date is %d/%d/%d ",
  75. current->father, current->p_to_pa->dob.day,
  76. current->p_to_pa->dob.month,
  77. current->p_to_pa->dob.year);
  78. if(current->p_to_ma != NULL)
  79. printf("and %s's birth date is %d/%d/%d.\n ",
  80. current->mother, current->p_to_ma->dob.day,
  81. current->p_to_ma->dob.month,
  82. current->p_to_ma->dob.year);
  83. current = current->next; /* current points to next in list */
  84. }
  85. /* Now free the memory */
  86. current = first;
  87. while(current->next != NULL)
  88. {
  89. last = current; /* Save pointer to enable memory to be freed */
  90. current = current->next; /* current points to next in list */
  91. free(last); /* Free memory for last */
  92. }
  93. }
  94. /* Function to input data on Family members */
  95. struct Family *get_person(void)
  96. {
  97. struct Family *temp; /* Define temporary structure pointer */
  98. /* Allocate memory for a structure */
  99. temp = (struct Family*) malloc(sizeof(struct Family));
  100. printf("\nEnter the name of the person: ");
  101. scanf("%s", temp -> name ); /* Read the Family's name */
  102. printf("\nEnter %s's date of birth (day month year); ", temp->name);
  103. scanf("%d %d %d", &temp->dob.day, &temp->dob.month, &temp->dob.year);
  104. printf("\nWho is %s's father? ", temp->name );
  105. scanf("%s", temp->father ); /* Get the father's name */
  106. printf("\nWho is %s's mother? ", temp -> name );
  107. scanf("%s", temp -> mother ); /* Get the mother's name */
  108. temp->next = temp->previous = NULL; /* Set pointers to NULL */
  109. temp->p_to_pa = temp->p_to_ma = NULL; /* Set pointers to NULL */
  110. return temp; /* Return address of Family structure */
  111. }
  112. char set_ancestry(struct Family *pmember1, struct Family *pmember2)
  113. {
  114. if(strcmp(pmember1->father, pmember2->name) == 0)
  115. {
  116. pmember1->p_to_pa = pmember2;
  117. return 1;
  118. }
  119. if( strcmp(pmember1->mother, pmember2->name) == 0)
  120. {
  121. pmember1->p_to_ma = pmember2;
  122. return 1;
  123. }
  124. else
  125. return 0;
  126. }
  127. /* Fill in pointers for mother or father relationships */
  128. char related (struct Family *pmember1, struct Family *pmember2)
  129. {
  130. return set_ancestry(pmember1, pmember2) ||
  131. set_ancestry(pmember2, pmember1);
  132. }

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124495500

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。