数据结构day2(2023.7.15)

news/2024/12/22 20:36:55/

一、Xmind整理:

 

二、课上练习:

练习1:定义车的信息:品牌,单价,颜色,车牌号

struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}a;struct Car//结构体自定义类型struct Car a;struct Car arr[3]struct Car *p;

练习2:间接定义变量按顺序初始化

 struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car car={"大众",123456,"白色"};car.name

练习3: 间接定义变量不按顺序初始化

struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car car={.price=123456,.color="白色",.id="沪A1111"};

练习4: 间接定义变量,单个赋值

 struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car car;strcpy(car.name,"大众");car.price=123456;strcpy(car.color,"白色");strcpy(car.id,"沪A88888");

练习5: 间接定义变量,输入赋值

 struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car car;scanf("%s",car.name);scanf("%d",&car.price);scanf("%s",car.color);scanf("%s",car.id);

练习6:直接定义变量按顺序初始化

 struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}car={"大众",123456,"白色","沪A1111"};

练习7:直接定义变量不按顺序初始化 

   struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}car={.price=123456,.color="白色",.id="沪A1111",.name="大众"};

练习8:直接定义变量,单个赋值 

struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}car;strcpy(car.name,"大众");car.price=123456;strcpy(car.color,"白色");strcpy(car.id,"沪A88888");

练习9:直接定义变量,输入赋值 

        struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}car;scanf("%s",car.name);scanf("%d",&car.price);scanf("%s",car.color);scanf("%s",car.id);

练习10:只有直接定义可以省略结构体名,无名结构体 

 struct {char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}car;

练习11:间接定义变量按顺序初始化 

  struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car car[3]={"大众",123456,"白色","沪A1111","大众",123456,"白色","沪A2222","大众",123456,"白色","沪A33333"};

练习12:间接定义变量不按顺序初始化 

 struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car car[3]={[0]={.price=123456,.color="白色",.id="沪A1111",.name="大众"},[2]={.price=123456,.color="白色",.id="沪A2222",.name="大众"},[1]={.price=123456,.color="白色",.id="沪A3333",.name="大众"}};

练习13: 间接定义变量,单个赋值

    struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car car[3];car[0]:第一辆车的所有信息car[0].name:第一辆车的品牌car[2].idstrcpy(car[0].name,"大众");car[0].price=123456;strcpy(car[0].color,"白色");strcpy(car[0].id,"沪A88888");strcpy(car[1].name,"大众");car[1].price=123456;strcpy(car[1].color,"白色");strcpy(car[1].id,"沪A88888");strcpy(car[2].name,"大众");car[2].price=123456;strcpy(car[2].color,"白色");strcpy(car[2].id,"沪A88888");

练习14: 间接定义变量,输入赋值

struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car car[3];for(int i=0;i<3;i++){scanf("%s",car[i].name);scanf("%d",&car[i].price);scanf("%s",car[i].color);scanf("%s",car[i].id);}

练习15:直接定义变量按顺序初始化

  struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号} car[3]={"大众",123456,"白色","沪A1111","大众",123456,"白色","沪A2222","大众",123456,"白色","沪A33333"};

练习16:直接定义变量不按顺序初始化

  struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}car[3]={[0]={.price=123456,.color="白色",.id="沪A1111",.name="大众"},[2]={.price=123456,.color="白色",.id="沪A2222",.name="大众"},[1]={.price=123456,.color="白色",.id="沪A3333",.name="大众"}};

练习17:直接定义变量,单个赋值

    struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}car[3];car[0]:第一辆车的所有信息car[0].name:第一辆车的品牌car[2].idstrcpy(car[0].name,"大众");car[0].price=123456;strcpy(car[0].color,"白色");strcpy(car[0].id,"沪A88888");strcpy(car[1].name,"大众");car[1].price=123456;strcpy(car[1].color,"白色");strcpy(car[1].id,"沪A88888");strcpy(car[2].name,"大众");car[2].price=123456;strcpy(car[2].color,"白色");strcpy(car[2].id,"沪A88888");

练习18: 直接定义变量,输入赋值

  struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号}car[3];for(int i=0;i<3;i++){scanf("%s",car[i].name);scanf("%d",&car[i].price);scanf("%s",car[i].color);scanf("%s",car[i].id);}

练习19:结构体指针指向普通结构体变量的地址 

   struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car c={.price=123456,.color="白色",.id="沪A1111",.name="大众"};struct Car *p=&c;

练习20: 结构体指针指向结构体数组的地址

struct Car{char name[20];    //品牌int price;        //单价char color[10]    //颜色char id[20]        //车牌号};struct Car  car[3]={"大众",123456,"白色","沪A1111","大众",123456,"白色","沪A2222","大众",123456,"白色","沪A33333"};struct Car *p=car;

练习21:结构体指针指向堆区空间的首地址

#include <stdio.h>
#include <string.h>
#include <stdlib.h>struct Car{char name[20];    //品牌int price;        //单价char color[10];    //颜色char id[20]    ;    //车牌号};
//输出对去空间内存
void Output(struct Car *p,int len)
{for(int i=0;i<len;i++){//    printf("%s\t%d\t%s\t%s\n",(p+i)->name,(p+i)->price,(p+i)->color,(p+i)->id);printf("%s\t%d\t%s\t%s\n",(*(p+i)).name,(*(p+i)).price,(*(p+i)).color,(*(p+i)).id);}
}
//在堆区空间申请
struct Car * create()
{struct Car *p=(struct Car *)malloc(sizeof(struct Car)*3);//if(NULL == p){return NULL;}return p;}
//在堆区实现循环输入
void Input(struct Car *p ,int n)
{for(int i=0;i<3;i++){scanf("%s",(p+i)->name);scanf("%d",&(p+i)->price);//(p+i)->price===>pricescanf("%s",(p+i)->color);scanf("%s",(p+i)->id);}}
//释放堆区空间
struct Car * free_space(struct Car *p)
{if(NULL==p){return NULL;}free(p);
p=NULL;return p;
}
int main(int argc, const char *argv[])
{/*struct Car  car[3]={"大众",123456,"白色","沪A1111","大众",123456,"白色","沪A2222","大众",123456,"白色","沪A33333"};struct Car *p=car;Output(p,3);*/struct Car *p=create();Input(p,3);Output(p,3);p=free_space(p);return 0;
}

练习22:结构体的引用 

  struct Car{char name[20];    //品牌int price;        //单价char color[10];    //颜色char id[20]    ;    //车牌号};struct Car a={"大众",123456,"白色","沪A1111"};struct Car b;//把a赋值给bstrcpy(b.name,a.name);b.price=a.price;strcpy(b.color,a.color);strcpy(b.id,a.id);a:表示所有信息b=a;

练习23:typedef和结构体结合 

//typedef int a,arr[],p;//typedef int arr[];typedef struct  {char name[20];    //品牌int price;        //单价char color[10];    //颜色char id[20]    ;    //车牌号}car_t,car_arr[3],*car_p ;//a==》car_p 
car_t:不是变量名,是普通结构体类型别名
car_arr:不是结构体数组名,是结构体数组的别名
car_p:不是结构体指针变量名,是结构体指针的别名
当typedef和结构体结合时,结构体名可以省略不写

练习24:结构体嵌套普通结构       

               定义一个学生:姓名、性别、出生日期【年月日】 

    typedef struct{int year,month,day;}Birthday;typedef struct{char name[10];char sex;Birthday bir;}Stu;Stu student={"张三",'M',2000,7,14};printf("%s %c %d年%d月%d日\n",student.name,studnet.sex,student.bir.year,student.bir.month,student.bir.day);

练习25:结构体嵌套结构体数组

               定义一个人的信息:姓名,性别,年龄,车辆多辆【品牌,单价】 

 typedef struct{char name[10];int price;    }Car_t;typedef struct{char name[10];char sex;int age;Car_t arr[2]; }person;person per={"张三",'M',18,"奔驰",555555,"宝马",666666};printf("",per.name,per.sex,per.age,per.arr[0].name,per.arr[0].price,per.arr[1].name,per.arr[1].price);

练习26:结构体字节计算 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{typedef struct{int a;float b;}A_t;A_t var;printf("&var=%p\t&var.a=%p\t&var.b=%p\n",&var,&var.a,&var.b);printf("sizeof(A_t)=%ld\n",sizeof(A_t));//8typedef struct{char a;double b;}B_t;
B_t var1;printf("&var1=%p\t&var1.a=%p\t&var1.b=%p\n",&var1,&var1.a,&var1.b);printf("sizeof(B_t)=%ld\n",sizeof(B_t));//16typedef struct{int a;char b;float c;char *p;char d;}C_t;
C_t c;printf("c.a=%p\n",&c.a);printf("c.b=%p\n",&c.b);printf("c.c=%p\n",&c.c);printf("c.p=%p\n",&c.p);printf("c.d=%p\n",&c.d);printf("sizeof(C_t)=%ld\n",sizeof(C_t));//32typedef char a_str[5];    typedef struct{a_str a;int b;}D_t;printf("sizeof(D_t)=%ld\n",sizeof(D_t));//12typedef struct{char a;double b;}E_t;typedef struct {char a;short b;E_t c;}F_t;printf("sizeof(F_t)=%ld\n",sizeof(F_t));//24return 0;
}

 练习27:共用体:默认为第一个元素赋值

typedef union {int a;char b;double c;}A_t;
A_t var={97};//默认为第一个成员赋值

练习28:指定某一个成员赋值

typedef union {int a;char b;double c;}A_t;
A_t var1={.c=55.66};//指定为某一成员赋值

练习29:共用体和结构体结合【共用体在结构体外】

    typedef union{int a;double b;    }A_t;typedef struct{int a_v;A_t var;   }B_t;B_t a={100,20};//默认为共用体第一个成员a赋值B_t b={100,.var.b=55.66};//指定共用体成员b赋值a.a_v=100a.var.a=20b.a_v=100b.var.b=55.66

练习30: 共用体和结构体结合 【共用体在结构体内】不建议写共用体名

typedef struct{int a_v;union {int a;double b;    };}B_t;B_t b={100,.b=55.66};

http://www.ppmy.cn/news/896754.html

相关文章

iMac 配置Jmeter

iMac 配置Jmeter 下载jdk https://www.oracle.com/java/technologies/javase-downloads.html 下载jmeter http://jmeter.apache.org/download_jmeter.cgi 【配置java环境】 打开终端输入 java -version #检查jdk版本 /usr/libexec/java_home -V #列出所有jdk版本的JAVA_HOME…

imac卸载_如何从较旧的2007-2009 iMac上卸下光盘驱动器

imac卸载 Whether you need to replace your optical drive or swap it out with a solid-state drive, here’s how to remove the DVD drive from an older iMac. 无论您是需要更换光盘驱动器还是将其换成固态驱动器&#xff0c;这都是从较旧的iMac上卸下DVD驱动器的方法。 A…

iMac2021 在重新安装mac os系统后,电脑账户创建失败

环境&#xff1a; iMAC 24寸 2021 Mac OS Big sur V.11.7.1 Apple M1 问题描述&#xff1a; iMac2021 在重新安装mac os系统后&#xff0c;电脑账户创建失败 解决方案&#xff1a; 1.启动至恢复分区 先将设备强制关机&#xff0c;开机并继续按住电源按钮&#xff0c;直至…

imac 升级 ssd_如何在较旧的2007-2009 iMac中安装SSD

imac 升级 ssd If you’re still rocking an older iMac but want to breathe some new life into it, replacing the traditional hard drive with a solid-state drive is a great way to do that. 如果您仍在摇摆旧版iMac,但又想给它注入新的活力,那么用固态硬盘代替传统硬…

全新iMac开发中!外观、屏幕、性能全面升级

除了全新14英寸和16 英寸MacBook Pro外&#xff0c;根据彭博社消息&#xff0c;苹果正在开发全新 iMac。全新 iMac 采用全新设计&#xff0c;窄边框屏幕。底部的金属下巴被移除&#xff0c;整体外观类似于 Pro Display XDR。 苹果将会发布两款尺寸的全新 iMac&#xff0c;21.5英…

iMac搭建网络服务器

说明&#xff1a;iMac自带PHP和Apache服务器&#xff0c;仅需配置即可。 1.进入/etc/apache2&#xff0c;编辑httpd.conf 启动php环境。将下面这句前的#去掉。 # LoadModule php5_module libexec/apache2/libphp5.so修改权限。 <Directory /> …

闲置iMac这样处理更适合

iMac电脑跟手机差不多&#xff0c;每一年我国都会出现很多的闲置电脑&#xff0c;iMac便是其中之一的一部分&#xff0c;因为人口数量太过于非常庞大&#xff0c;即便是5000人当中才还有一个拥有着一部iMac&#xff0c;可是14亿的这样的基数也是让我国的iMac消费者产生最大的一…

2011 imac 固态_Apple 篇一:iMac 2011升级内存固态实录

Apple 篇一:iMac 2011升级内存固态实录 2020-03-23 22:48:07 25点赞 83收藏 39评论 小编注:此篇文章来自即可瓜分10万金币,周边好礼达标就有,邀新任务奖励无上限,点击查看活动详情 创作立场声明:什么是玩家?自己动手改造挖掘硬件的潜力,我认为这才是玩家! 兄弟这台201…