- 论坛徽章:
- 0
|
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdbool.h> // For bool, false, true
- #include <string.h> // For strcmp()
- struct Family * get_person(void);
- struct Famliy *pmember1;
- struct Family *pmember2;
- bool set_ancestry_fuck(struct Famliy *pmember1, struct Family *pmember2);
- struct Date
- {
- int day;
- int month;
- int year;
- };
- struct Family
- {
- 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 */
- };
- int main(void)
- {
- struct Family * first = NULL; /* Pointer to first person */
- struct Family * current = NULL; /* Pointer to current person */
- struct Family * last = NULL; /* pointer to last 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;
- printf("\n");// 为了美观
- current = get_person();
- if (first == NULL){
- first = current; /* Set pointer to first Family */
- last = current; /* Remember fot 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 */
- }
- }
- /** Now tell them what we know **/
- /** Ouput Family data in reverse order **/
- while (current != NULL){
- printf("\n");// 为了美观
- 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);
- last = current; /* Svae pointer to enable memory to be freed */
- current = current->previous; /* current points to previous list */
- free(last); /* Free memory for the Family we output */
- }
- printf("\n");// 为了美观
- return 0;
- }
- /** Function to input data on Family members **/
- struct Family * get_person(void)
- {
- struct Family * temp;
- /* Allocate memory for a structure */
- temp = (struct Family *)malloc(sizeof(struct Family));
- printf("\nEnter the name of the person: "); /* Read the Family's name */
- scanf("%s", temp -> 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 pointer to NULL
- printf("\n");//为了美观
- return temp;
- }
- bool set_ancestry(struct Famliy *pmember1, struct Family *pmember2)
- {
- if (strcmp(pmember1->father, pmember2->name) == 0){
- //pmember1->p_to_fa = pmember2;
- return true;
- }
- if (strcmp(pmember1->mather, pmember2->name) == 0){
- //pmember1->p_to_ma = pmember2;
- return true;
- }
- else
- return false;
- }
复制代码 |
|