第11章 結構化數據

使用結構
嘗試將horse結構用於一個簡單的例子中

#define _STDC_WANT_LIB_EXT1_ 1
#include <stdio.h>

typedef struct Horse Horse;

struct Horse
{
    int age;
    int height;
    char father[20];
    char mother[20];
    char name[20];
};
int main()
{
    Horse my_horse;

    printf("Enter the name of the horse: ");
    scanf("%s",my_horse.name,sizeof(my_horse.name));

    printf_s("How old is %s? ",my_horse.name);
    scanf("%d", &my_horse.age);

    printf("How high is %s (in hands)? ",my_horse.name);
    scanf("%d",&my_horse.height);

    printf("Who is %s's father? ",my_horse.name);
    scanf("%s",&my_horse.father,sizeof(my_horse.father));

    printf("Who is %s's mother? ",my_horse.name);
    scanf("%s",&my_horse.mother,sizeof(my_horse.mother));

    printf(" %s id %d years olds ,%d hands high,",my_horse.name,my_horse.age,my_horse.height);

    printf(" and has %s and %s as parents.\n",my_horse.father,my_horse.mother);

    return 0;
}

使用結構數組
擴展上一個例子,以處理多匹馬

#define _STDC_WANT_LIB_EXT1_ 1
#include <stdio.h>
#include <ctype.h>

typedef struct Horse Horse;

struct Horse
{
    int age;
    int height;
    char name[20];
    char father[20];
    char mother[20];
};

int main()
{
    Horse my_horses[50];
    int hcount = 0;
    char test = '\0';

    for (hcount = 0; hcount < sizeof(my_horses) / sizeof(Horse); ++hcount)
    {
        printf("Do you want to enter details of %s horse (Y or N)? ", hcount ? "nother" :"");

        scanf(" %c",&test,sizeof(test));
        if (tolower(test) == 'n')
            break;

        printf("Enter the name of the horse: ");
        scanf("%s",my_horses[hcount].name,sizeof(my_horses[hcount].name));

        printf("How old is %s? ",my_horses[hcount].name);
        scanf("%s",&my_horses[hcount].age,sizeof(my_horses[hcount].age));

        printf("How high is %s(in hands)? ",my_horses[hcount].name);
        scanf("%d", &my_horses[hcount].height, sizeof(my_horses[hcount].height));

        printf("Who is %s's father?", my_horses[hcount].name);
        scanf("%s",&my_horses[hcount].father,sizeof(my_horses[hcount].father));

        printf("Who is %s's mother?",my_horses[hcount].name);
        scanf("%s", &my_horses[hcount].mother, sizeof(my_horses[hcount].mother));
    }

    printf("\n");
    for (int i = 0; i < hcount; ++i)
    {
        printf("%s is %d years old,%d hands high,",my_horses[hcount].name,my_horses[hcount].age,my_horses[hcount].height);
        printf(" and has %s and %s as parents.\n",my_horses[hcount].father,my_horses[hcount].mother);
    }
    return 0;
}

使用結構指針
這個例子演示瞭如何爲結構動態分配內存

#define _STDC_WANT_LIB_EXT1_ 1
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Horse Horse;

struct Horse
{
    int age;
    int height;
    char name[20];
    char father[20];
    char mother[20];
};

int main(void)
{
    Horse *phorses[50];
    int hcount =0;
    char test = '\0';
    for (hcount = 0; hcount < sizeof(phorses) / sizeof(Horse*); ++hcount)
    {
        printf_s("Do you want to enter details of a%s horse(Y or N)? ", hcount ? "nother" : "");
        scanf("%c", &test, sizeof(test));

        if (tolower(test) == 'n')
            break;

        phorses[hcount] = (Horse*)malloc(sizeof(Horse));

        printf("Enter the name of the horse: ");
        scanf("%s",phorses[hcount]->name,sizeof(phorses[hcount]->name));

        printf("How high is %s (in hands)? ",phorses[hcount]->name);
        scanf("%s",&phorses[hcount]->height);

        printf("How old is %s? ",phorses[hcount]->name);
        scanf("%d",&phorses[hcount]->age);

        printf("Who is %s's father?",phorses[hcount]->name);
        scanf("%s",phorses[hcount]->father,sizeof(phorses[hcount]->father));

        printf("Who is %s's mother? ",phorses[hcount]->name);
        scanf("%s",phorses[hcount]->mother,sizeof(phorses[hcount]->mother));
    }

    printf("\n");
    for (int i = 0; i < hcount; ++i)
    {
        printf("%s is %d years old,%d hands high,",phorses[i]->name,phorses[i]->age,phorses[i]->height);
        printf(" and has %s and %d as parents.\n",phorses[i]->father,phorses[i]->mother);

        free(phorses[i]);
    }
    return 0;
}

將結構指針用做結構成員
讓結構含有指向同類型結構的指針

#define _STDC_WANT_LIB_EXT1_1
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Horse Horse;

struct Horse
{
    int age;
    int height;
    char name[20];
    char father[20];
    char mother[20];
    Horse *next;
};

int main()
{
    Horse *first = NULL;
    Horse *current = NULL;
    Horse *previous = NULL;

    char test = '\0';

    for (; ;)
    {
        printf("Do you want to enter details of a%s horse(Y or N)? ",first != NULL? "nother" : "");
        scanf(" %c",&test,sizeof(test));

        if (tolower(test) == 0)
            break;

        current = (Horse*)malloc(sizeof(Horse));
        if (first == NULL)
            first = current;

        if (previous != NULL)
            previous->next = current;

        printf("Enter the name of the horse: ");
        scanf("%s", current->name, sizeof(current->name));

        printf("How old is %s? ",current->name);
        scanf("%d",&current->age);

        printf("How high is %s? ",current->name);
        scanf("%d",&current->height);

        printf("Who is %s's father? ", current->name);
        scanf("%s",current->father,sizeof(current->father));

        printf("Who is %s's mother? ", current->name);
        scanf("%s",current->mother,sizeof(current->mother));

        current->next = NULL;
        previous = current;
    }

    printf("\n");
    current = first;
    while (current != NULL)
    {
        printf("%s is %d year old,%d hands high,",current->name,current->age,current->height);
        printf(" and has %s and %s as parents.\n",current->father,current->mother);
        previous = current;
        current = current->next;
        free(previous);
        previous = NULL;
    }
    first = NULL;
    return 0;
}

雙向鏈表

#define _STDC_WANT_LIB_EXT1_ 1
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Horse Horse;

struct Horse
{
    int age;
    int height;
    char name[20];
    char father[20];
    char mother[20];
    Horse *next;
    Horse *previous;
};

int main()
{
    Horse *first = NULL;
    Horse *current = NULL;
    Horse *last = NULL;

    char test = '\0';

    for (; ;)
    {
        printf("Do you want to enter details of a%s horse(Y or N)? ", first != NULL? "nother" : "");

        scanf(" %c",&test,sizeof(test));

        if (tolower(test) == 'n')
            break;

        current = (Horse*)malloc(sizeof(Horse));
        if (first == NULL)
        {
            first = current;
            current-> previous = NULL;
        }
        else
        {
            last->next = current;
            current->previous = last;
        }
        printf("Enter the name of the horse: ");
        scanf("%s",current->name,sizeof(current->name));

        printf("How old is %s? ",current->name);
        scanf("%d",&current->age);

        printf("How high is %s? ",current->name);
        scanf("%d",&current->height);

        printf("Who is %s's mother? ",current->name);
        scanf("%s",current->mother,sizeof(current->mother));

        printf("Who is %s's father? ",current->name);
        scanf("%s",current->father,sizeof(current->father));

        current->next = NULL;
        last = current;

    }
    printf("\n");
    while (current != NULL)
    {
        printf("%s is %d years old,%d hands high, ",current->name,current->age,current->height);
        printf(" and has %s and %s as parents.\n", current->father, current->mother);
        last = current;
        current = current -> previous;
        free(last);
        last = NULL;

    }

    first = NULL;
    return 0;
}
發佈了26 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章