第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",¤t->age); printf("How high is %s? ",current->name); scanf("%d",¤t->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",¤t->age); printf("How high is %s? ",current->name); scanf("%d",¤t->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
- 第11章 结构化数据
- 第5章 字典和结构化数据
- 第11章 使用结构化命令
- 第3章 shell结构化命令------------(文件数据的循环与控制循环)
- 第12章 使用结构化命令
- 数据算法与结构:第一周:文摘
- 结构化数据,非结构化数据
- 第6周 处理非结构化数据的利器Mongodb
- Spark 实战,第 3 部分: 使用 Spark SQL 对结构化数据进行统计分析
- Spark 实战,第 3 部分: 使用 Spark SQL 对结构化数据进行统计分析
- C primer plus第14章(结构和其他数据形式)
- C primer plus第14章(结构和其他数据形式)(练习)
- Lua篇(第04章):来点高难度的,获取Lua表结构数据
- c primer plus第14章总结:结构和其他数据形式
- 第10章 结构型、共用型、枚举型及用户自定义型数据
- 非结构化数据
- 半结构化数据
- 第3章内核结构
- SetWindowPos讲解
- Java之IO概述
- mysql datetime vs timestamp
- Struts的优点/缺点
- 十年·杭研大咖说 | 邱似峰:从应届生到网易视频云CTO的蜕变
- 第11章 结构化数据
- 【数学】奇异值分解(SVD)原理详解及推导
- 母函数与指数型母函数模板
- MySQL存储引擎与数据类型
- 2016 macbook pro 开关机
- 深度学习在图像取证中的进展与趋势
- 北大OJ编程基础之循环控制数字反转
- BZOJ4705棋盘游戏
- vim java自动补全插件 javacomplete