060.记录个人资料

简介: 060.记录个人资料
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct Family *get_person(void);    /* Prototype for input function */
char related(struct Family *pmember1, struct Family *pmember2);
char set_ancestry(struct Family *pmember1, struct Family *pmember2);
struct Date   
{
  int day;
  int month;
  int year;
};
struct Family                      /* Family structure declaration   */
{
  struct Date dob;
  char name[20];
  char father[20];
  char mother[20];
  struct Family *next;            /* Pointer to next structure      */
  struct Family *previous;        /* Pointer to previous structure  */
  struct Family *p_to_pa;         /* Pointer to father structure   */
  struct Family *p_to_ma;         /* Pointer to mother structure   */
};
void main()
{
  struct Family *first = NULL;    /* Pointer to first person        */
  struct Family *current = NULL;  /* Pointer to current person      */
  struct Family *last = NULL;     /* Pointer to previous person     */
  char more = '\0';               /* Test value for ending input    */
  for( ; ; )
  {
    printf("\nDo you want to enter details of a%s person (Y or N)? ", 
      first != NULL?"nother " : "" );
    scanf(" %c", &more);
    if(tolower(more) == 'n') 
      break;
    current = get_person();
    if(first == NULL)
    {
      first = current;            /* Set pointer to first Family    */
      last = current;             /* Remember for next iteration    */
    }
    else
    {
      last->next = current;  /* Set next address for previous Family */  
      current->previous = last; /* Set previous address for current */
      last = current;           /* Remember for next iteration */             
    }
  }
  current = first;
  while(current->next != NULL)  /* Check for relation for each person in    */
  {                       /* the list up to second to last            */
    int parents = 0;      /* Declare parent count local to this block */
    last = current->next; /* Get the pointer to the next              */
    while(last != NULL)   /* This loop tests current person           */
    {                     /* against all the remainder in the list    */
      if(related(current, last))         /* Found a parent ?          */
        if(++parents == 2)   /* Yes, update count and check it        */
          break;             /* Exit inner loop if both parents found */
        last = last->next;     /* Get the address of the next           */
    } 
    current = current->next;   /* Next in the list to check             */
  }
  /* Now tell them what we know */
  /* Output Family data in correct order */
  current = first;
  while (current != NULL)  /* Output Family data in correct order  */
  {
    printf("\n%s was born %d/%d/%d, and has %s and %s as parents.",
      current->name, current->dob.day, current->dob.month,
      current->dob. year, current->father,  current->mother);
    if(current->p_to_pa != NULL )
      printf("\n\t%s's birth date is %d/%d/%d  ",
      current->father, current->p_to_pa->dob.day,
      current->p_to_pa->dob.month, 
      current->p_to_pa->dob.year);
    if(current->p_to_ma != NULL)
      printf("and %s's birth date is %d/%d/%d.\n  ",
      current->mother, current->p_to_ma->dob.day,
      current->p_to_ma->dob.month, 
      current->p_to_ma->dob.year);
    current = current->next;  /* current points to next in list       */
  }
  /* Now free the memory */  
  current = first;
  while(current->next != NULL)
  {
    last = current;     /* Save pointer to enable memory to be freed */
    current = current->next; /* current points to next in list       */
    free(last);         /* Free memory for last                      */
  }
}
/*   Function to input data on Family members   */
struct Family *get_person(void)
{
  struct Family *temp;         /* Define temporary structure pointer */
  /* Allocate memory for a structure */
  temp = (struct Family*) malloc(sizeof(struct Family));
  printf("\nEnter the name of the person: ");
  scanf("%s", temp -> name );         /* Read the Family's name */
  printf("\nEnter %s's date of birth (day month year); ", temp->name);
  scanf("%d %d %d", &temp->dob.day, &temp->dob.month, &temp->dob.year);
  printf("\nWho is %s's father? ", temp->name );
  scanf("%s", temp->father );        /* Get the father's name */
  printf("\nWho is %s's mother? ", temp -> name );
  scanf("%s", temp -> mother );      /* Get the mother's name */
  temp->next = temp->previous = NULL; /* Set pointers to NULL */
  temp->p_to_pa = temp->p_to_ma = NULL;    /* Set pointers to NULL  */
  return temp;          /* Return address of Family structure */
}
char set_ancestry(struct Family *pmember1, struct Family *pmember2)
{
  if(strcmp(pmember1->father, pmember2->name) == 0)
  {
    pmember1->p_to_pa = pmember2;
    return 1;
  }
  if( strcmp(pmember1->mother, pmember2->name) == 0)
  {
    pmember1->p_to_ma = pmember2;
    return 1;
  }
  else
    return 0;
}
/* Fill in pointers for mother or father relationships */
char related (struct Family *pmember1, struct Family *pmember2)
{
  return set_ancestry(pmember1, pmember2) ||
    set_ancestry(pmember2, pmember1);
}
相关文章
|
消息中间件 存储 安全
01为什么需要MQ及其好处
01为什么需要MQ及其好处
233 0
|
IDE Java 编译器
lombok编译遇到“找不到符号的问题”
【9月更文挑战第18天】当使用 Lombok 遇到 “找不到符号” 的问题时,可能是由于 Lombok 未正确安装、编译器不支持、IDE 配置不当或项目构建工具配置错误。解决方法包括确认 Lombok 安装、编译器支持,配置 IDE 和检查构建工具配置。通过这些步骤通常可解决问题,若问题仍存在,建议检查项目配置和依赖,或查看日志获取更多信息。
5436 2
【每日一题Day371】LC2586统计范围内的元音字符串数 | 模拟
【每日一题Day371】LC2586统计范围内的元音字符串数 | 模拟
119 1
|
存储 缓存 小程序
小程序获取不到用户头像和昵称返回微信用户问题解决,即小程序授权获取用户头像规则调整的最新解决方案
小程序获取不到用户头像和昵称返回微信用户问题解决,即小程序授权获取用户头像规则调整的最新解决方案
549 0
|
SQL Java 数据库连接
|
存储 算法 开发工具
日常工作中常用Git命令小结
正确理解Git的四个工作区域 Workspace:工作区,即个人克隆项目到本地后,项目所在的文件夹目录。 Index / Stage:暂存区,用于储存工作区中的变更(增删改等改动)的文件的地方。操
|
SQL 数据库
肝通宵写了三万字把SQL数据库的所有命令,函数,运算符讲得明明白白讲解,内容实在丰富,建议收藏+三连好评!(三)
肝通宵写了三万字把SQL数据库的所有命令,函数,运算符讲得明明白白讲解,内容实在丰富,建议收藏+三连好评!
189 0
肝通宵写了三万字把SQL数据库的所有命令,函数,运算符讲得明明白白讲解,内容实在丰富,建议收藏+三连好评!(三)
|
算法
《数字视频和高清:算法和接口》一第3章 线性光和感知均匀性
本节书摘来华章计算机《数字视频和高清:算法和接口》一书中的第3章 , [加]查尔斯·波因顿(Charles Poynton)著 刘开华 褚晶辉 马永涛 吕卫 宫霄霖 等译 译更多章节内容可以访问云栖社区“华章计算机”公众号查看。
2369 0