C语言程序设计(微课版)ch10-结构体与共用体_第1页
C语言程序设计(微课版)ch10-结构体与共用体_第2页
C语言程序设计(微课版)ch10-结构体与共用体_第3页
C语言程序设计(微课版)ch10-结构体与共用体_第4页
C语言程序设计(微课版)ch10-结构体与共用体_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第10章结构体与共用体第10章结构体与共用体10.1结构体类型的定义10.2结构体类型变量10.3

结构体数组10.4结构体和指针10.5结构体和函数10.6链表(略)10.7共用体10.8枚举(略)10.9用typedef定义类型10.1结构体类型的定义

numnamesexageScoreadrr041000LiPingM1878.1Beijing041001LiuYiF2088.5Jilin041002YaoLiM1990.8Beijing学生信息:学号、姓名、性别、年龄、成绩、地址注意:(1)定义了一种类型,不是变量。(2)最后的分号“;”不能省略。(3)成员可以是任意类型。structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];};一般形式:struct结构体名

{

结构体成员表};structdate{intyear;intmonth;intday;};structstu{longintnumber;charname[20];charsex;

structdatebirthday;floatscore[3];charaddress[30];};10.2结构体类型变量

如:structstudents1,s2;score[3]numname[20]sexbirthdayaddr[30]s1地址例如:structstudent{intnum;charname[20];floatscore;}s1,s2;例如:struct{intnum;charname[20];floatscore;}s1,s2;10.2.1结构体类型变量的定义3种定义方式1.先定义类型,再定义变量struct结构体名变量名表;2.在定义结构体类型的同时定义变量struct结构体名

{成员表}变量名表;3.直接定义结构体类型变量struct

{成员表}变量名表;10.2.2结构体变量的引用

例如:structdate{intyear;intmonth;intday;}structexample{intnum;charname[20];structdatebir;}s1,s2;各成员的引用:s1.num=101;s2.num=s1.num+1;strcpy(,"wang");s1.bir.year=1985;注意:结构体变量的初始化:结构体类型结构体变量名={初始值表};例如:structstudent{charname[10];floatscore[3];}stu={"wang_li",81,77,96};结构体变量名.成员名【例10.1】输入某学生的姓名、年龄和5门功课成绩,计算平均成绩并输出。程序运行情况:wang_li21↙8277916885↙wang_li2182.077.091.068.085.0average=80.6#include<stdio.h>intmain(){structstudent{charname[10];intage;floatscore[5],ave;}stu;inti;stu.ave=0;scanf("%s%d",,&stu.age)

for(i=0;i<5;i++){scanf("%f",&stu.score[i]);stu.ave+=stu.score[i]/5.0; }printf("%s%4d\n",,stu.age);for(i=0;i<5;i++)printf("%6.1f",stu.score[i]);printf("average=%6.1f\n",stu.ave);return0;}C语言中,定义结构体的保留字是().enumstructuniontypedefABCD提交单选题100分有以下的结构体变量定义语句:structstudent{intnum;charname[9];}stu;则下列叙述中错误的是()结构体名为student结构体类型名为stustruct是C的关键字num是结构体成员名ABCD提交单选题100分结构体数组的定义10.3结构体数组

structstudent{intnum;charname[20];charsex;intage;floatscore[3];};structstudentstu[10];10.3.1结构体数组的定义与初始化structstudent{intnum;charname[20];charsex;intage;floatscore[3];}

stu[10];结构体数组的初始化structstudent{intnum;charname[20];charsex;intage;floatscore[3];}stu[2]={

{1101,”wangli”,’M”,21,75,82,94},

{1102,”liping”,’F’,20,82,79,90}

};10.3.2结构体数组的引用

【例10.2】输入3个复数的实部和虚部放在一个结构体数组中,根据复数模由大到小顺序对数组进行排序并输出。(注:复数的模=sqrt(实部*实部+虚部*虚部))13运行结果:32↙11↙54↙5.00+4.00i3.00+2.00i1.00+1.00ifor(i=0;i<N-1;i++)

{k=i;for(j=i+1;j<N;j++)if(a[k].m<a[j].m)k=j;temp=a[i];a[i]=a[k];a[k]=temp;}for(i=0;i<N;i++)printf("%.2f+%.2fi\n",a[i].x,a[i].y);return0;}#defineN3#include<stdio.h>#include<math.h>intmain(){structcomplex {floatx;floaty;floatm;}a[N],temp;

inti,j,k;for(i=0;i<N;i++){scanf("%f%f",&a[i].x,&a[i].y);a[i].m=sqrt(a[i].x*a[i].x+a[i].y*a[i].y);

}若有以下定义structstudent{intnum;charname[9];}stu[2]={1,"zhangsan",2,"lisi"};以下能输出字符串"lisi"的语句是()

printf("%s",&stu[1].name[0]);printf("%s",&stu[1].name);printf("%s",stu[0].name);printf("%s",stu[1].name[0]);ABCD提交单选题100分10.4结构体和指针

1.结构体指针变量的定义例如:structstudent{intnum;charname[15];floatscore[3];}stu[10],x,*p;p=&x;

2.结构体指针变量的引用例如:引用结构体变量x的成员有以下3种方法:① x.成员名,如:x.num② (*p).成员名,如:(*p).num③ p->成员名,如:p->nump=stu或p=&stu[0]运行结果:32↙11↙54↙5.00+4.00i3.00+2.00i1.00+1.00ifor(p=a;p<a+N;p++){k=p;for(q=p+1;q<a+N;q++) if(k->m<q->m)k=q;temp=*p;*p=*k;*k=temp;}for(p=a;p<a+N;p++) printf("%.2f+%.2fi\n",p->x,p->y);return0;}#defineN3#include<stdio.h>#include<math.h>intmain(){structcomplex{floatx,y,m;}a[N],temp,*p,*q,*k;

for(p=a;p<a+N;p++){scanf("%f%f",&p->x,&p->y);p->m=sqrt(p->x*p->x+p->y*p->y);}(37)有以下程序

#include<stdio.h>

structord

{intx,y;}dt[2]={1,2,3,4};

main()

{structord*p=dt;

printf("%d,",++p->x);printf("%d\n",++p->y);

}程序的运行结果是().2,3

4,1

1,2

3,4ABCD提交单选题100分

10.5结构体和函数

1.将结构体变量作为函数的参数【例10.4】输入两个复数,比较这两个复数模是否相等。19#include<stdio.h>#include<math.h>structcomp{floatx,y; floatm;};floatcompare(structcompa,structcompb){a.m=sqrt(a.x*a.x+a.y*a.y);b.m=sqrt(b.x*b.x+b.y*b.y);return(a.m-b.m); }程序运行:12↙21↙Equalintmain(){structcompa,b;scanf("%f%f",&a.x,&a.y); scanf("%f%f",&b.x,&b.y); if(compare(a,b)==0)printf("Equal\n");elseprintf("Unequal\n");return0;}10.5.1结构体作函数参数2.结构体指针作函数参数【例10.5】编写按复数模从小到大排序的函数。#defineN5#include<stdio.h>#include<math.h>structcomp{floatx,y;floatm;};voidsort(structcomp*p,intn){inti,j,k;structcompt;for(i=0;i<n-1;i++){k=i;for(j=i+1;j<n;j++)if((p+k)->m>(p+j)->m)k=j;t=*(p+i);*(p+i)=*(p+k);*(p+k)=t;}}intmain(){structcompa[N];inti;printf("Inputcomplex:\n");for(i=0;i<N;i++){scanf("%f%f",&a[i].x,&a[i].y);a[i].m=sqrt(a[i].x*a[i].x+a[i].y*a[i].y);}sort(a,N);printf("Outputcomplex:\n");for(i=0;i<N;i++)printf("%.1f+%.1fi",a[i].x,a[i].y); printf("\n");return0;}10.5.2返回结构体的函数

1.返回结构体数据的函数函数可以带回一个结构体类型的数据给主调函数。【例10.6】输入一批复数,查找并输出模最大的复数。编写函数完成查找功能。#defineN5#include<stdio.h>#include<math.h>structcomp{floatx,y;floatm;};structcompfind(structcompp[],intn){inti,k=0;floatt=p[0].m;for(i=1;i<n;i++)if(t<p[i].m){t=p[i].m;k=i;}returnp[k];}intmain(){structcompa[N],max;inti;for(i=0;i<N;i++){scanf("%f%f",&a[i].x,&a[i].y);a[i].m=sqrt(a[i].x*a[i].x+a[i].y*a[i].y);}

max=find(a,N);printf("max=%.1f+%.1fi\n",max.x,max.y);return0;}程序运行:11↙13↙3-2↙5-3↙79↙max=7.0+9.0i2.返回结构体指针的函数#defineN5#include<stdio.h>#include<math.h>structcomp{floatx,y;floatm;

};structcomp*find(structcomp*p,intn){inti,k=0;floatt=p[0].m;for(i=1;i<n;i++)if(t<(p+i)->m) {t=(p+i)->m; k=i; }return(p+k); }intmain(){structcompa[N],*max; inti;for(i=0;i<N;i++){scanf("%f%f",&a[i].x,&a[i].y);a[i].m=sqrt(a[i].x*a[i].x+a[i].y*a[i].y);}

max=find(a,N);printf("max=%.1f+%.1fi\n",max->x,max->y);return0;}程序运行:11↙13↙3-2↙5-3↙79↙max=7.0+9.0i10.6链表(简介)1链表的数据结构head张三李四王五孙六NULL……structstudent{charname[10];structstudent*next;};10.6.2动态存储分配函数1.malloc函数函数原型:void*malloc(unsignedintsize);使用方法:例如:char*x;x=(char*)malloc(10);2.calloc函数函数原型:void*calloc(unsignedintn,unsignedintsize);使用方法:例如:float*x;x=(float*)calloc(10,4);3.free函数函数原型:voidfree(void*p);使用方式:例如:float*x;x=(float*)calloc(10,4);……free(x);10.6.3链表的基本操作

建立链表遍历链表将节点插入到链表删除链表例:有以下结构体说明和变量定义,如图所示,structnode{intdata;structnode*next;}*p,*q,*r;29指针p、q、r分别指向此链表中的三个连续结点。现要将q所指结点从链表中删除,同时要保持链表的连续,以下不能完成指定操作的语句是A)p->next=q->next;B)p->next=p->next->next;C)p->next=r;D)p=q->next;返回目录30313210.7共用体

1.共用体变量的定义union共用体名{共用体成员表};(1)先定义共用体类型,再定义共用体变量例如:uniondata{shortinti;charch;floatf;};uniondataa,b,c;(2)在定义共用体类型的同时定义变量例如:uniondata{shortinti;charch;floatf;}a,b,c;(3)不定义共用体类型名,直接定义变量例如:union{shortinti;charch;floatf;}a,b,c;2.共用体变量的引用共用体变量的引用方式与结构体变量相同,可以使用以下3种形式之一:(1)共用体变量名.成员名。(2)指针变量名->成员名。(3)(*指针变量名).成员名。例如,对于前面所定义的共用体变量a,用以下赋值语句:a.i=1;a.ch=’$’; a.f=1.5;10.8枚举

略36返回目录10.9使用typedef定义类型

格式:typedef已定义的类型名新的类型名;floata,b;REALa,b;typedefflo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论