第11章 结构化数据

来源:互联网 发布:知乎年度300问 编辑:程序博客网 时间:2024/05/17 23:03

使用结构
尝试将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;}
0 0
原创粉丝点击