《高级语言程序设计教学课件》第9章_第1页
《高级语言程序设计教学课件》第9章_第2页
《高级语言程序设计教学课件》第9章_第3页
《高级语言程序设计教学课件》第9章_第4页
《高级语言程序设计教学课件》第9章_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

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

文档简介

1,第九章结构体,2,学习目标,结构体的概念结构体类型和结构体变量的差别运算符“.”和“-”结构体数组、结构体指针,3,9.1结构体,问题:我们要处理一个学生的信息,包括:学号、姓名、性别、年龄、成绩、地址按照以往定义变量的方式,我们可以这样定义:,structstudentintnum;charname20;charsex;intage;floatscore;charaddr50;,结构体,4,结构类型,【例】平面上点(x,y)的表示,structpointfloatx;floaty;,关键字,结构体名,结构体成员,5,定义一个结构体的一般形式为:注意:1.结构体类型不分配空间,是一种数据类型;2.结构体成员的类型可以不同,struct结构名数据类型成员1;数据类型成员n;,6,structstudentintnum;charname20;charsex;intage;floatscore;charaddr50;,结构体类型定义描述结构的组织形式,不分配内存,7,【例】定义点结构变量并保存平面点(4,3),structpointfloatx;floaty;structpointpoint1;point1.x=4;point1.y=3;,8,定义结构体变量的三种形式,形式1:先定义类型,再定义变量,struct结构名数据类型成员1;数据类型成员n;struct结构名结构变量1,结构变量2;,structpointfloatx;floaty;structpointpoint1,point2;,9,形式2:在定义类型时,定义变量,struct结构名数据类型成员1;数据类型成员n;结构变量1,结构变量2;,structpointfloatx;floaty;point1,point2;,10,形式3:无类型名的变量定义,注意第三种定义形式省略了结构名,在此定义语句后面无法再定义这个类型的其它结构变量,struct数据类型成员1;数据类型成员n;结构变量1,结构变量2;,structfloatx;floaty;point1,point2;,结构名,11,说明结构体类型与结构体变量概念不同类型:不分配内存变量:分配内存类型:不能赋值、存取、运算变量:可以结构体可嵌套结构体成员名与程序中变量名可同,不会混淆,例structdateintmonth;intday;intyear;structstudentintnum;charname20;structdatebirthday;stu;,例structstudentintnum;charname20;structdateintmonth;intday;intyear;birthday;stu;,例main()structstudentintnum;charname20;stu;intnum;num=0;stu.num=1;,12,【例9-1】结构变量初始化,#includemain()structstudentlongintnum;charname20;charaddr20;a=89031,LiLin,123BeijingRoad;printf(No.:%ldnname:%snaddress:%sn,a.num,,a.addr);,初值表,13,初始化的一般形式,在定义结构变量的时候对其进行初始化;初始化表由括起;逗号隔开的数据对应赋值给结构变量的每个成员,14,结构体变量的引用,引用规则结构体变量不能整体进行输入、输出、赋值、运算引用方式:结构体变量名.成员名可以将一个结构体变量赋值给另一个结构体变量结构体嵌套时逐级引用,structstudentintnum;charname20;structdateintmonth;intday;intyear;birthday;stu1,stu2;stu1.birthday.month=12;,15,【例】成员变量引用,structstudentintnum;charname20;charsex;intage;floatscore;charaddr30;stu1,stu2;,stu1=101,“WanLin”,M,19,87.5,“DaLian”;printf(“%d,%s,%c,%d,%f,%sn”,stu1);if(stu1=stu2)=“WangLin”;,stu1.num=101;strcpy(,“WangLin”);stu1.sex=M;Stu1.age=19;stu1.score=87.5;strcpy(stu1.addr,“DaLian”;stu2=stu1;,16,【例】分别定义表示直线和矩形的结构体,structpointfloatx;floaty;structlinestructpointstartp;structpointendp;,structrectstructpointpt1;structpointpt2;structpointpt3;structpointpt4;,17,常见编程错误,错例1,structstuintno;charname10;floatscore;=1,“wangbin”,98;,错误分析:structstu是结构类型,不占内存空间,不能对其赋值。,18,错例2,structstuintno;charname10;floatscore;stus;,错误分析:1)结构体类型定义之后“”后必须加“;”2)结构体类型名为structstu,不是stu;申明变量应为:structstus;,19,错例3,structstuintno;charname10;floatscore;structstus;scanf(“%d%s%f”,s);,错误分析:不能整体读入结构体变量值,应为scanf(“%d%s%f”,20,错例4,structstuintno;charname10;floatscore;structstus,*p;scanf(“%d%s%f”,错误分析:1)p是结构指针变量,使用之前应赋初值;2)p访问成员的方式应为p-no,p-name,p-score;3)p-name为数组名,读入一个字符串时,前面不能加“floaty;floatGetDis(structpoint,structpoint);,23,main()structpointpt1,pt2;printf(Pleaseinputpt1:);scanf(%f%f,24,定义计算两点之间距离的函数,floatGetDis(structpointp1,structpointp2)returnsqrt(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y);,25,【例9-3】判断点是否在矩形内应包含以下功能:构造一个点;构造一个矩形;判断一个点是否在矩形内。每一个功能都用函数来实现,26,#include#includestructpointfloatx;floaty;structrectstructpointpt1;structpointpt2;structpointpt3;structpointpt4;structpointmakepoint(floatx,floaty);voidMakeRect(structpointpt1,structpointpt2,structpointpt3,structpointpt4,structrect*screen);intPtInRect(structpointp,structrectr);,27,main()structpointpt1=MakePoint(0,0);structpointpt2=MakePoint(50,0);structpointpt3=MakePoint(50,10);structpointpt4=MakePoint(0,10);structrectscreen;MakeRect(pt1,pt2,pt3,pt4,运行结果Themiddlepointofthescreenis(25.0,5.0)Themiddlepointisinscreen!Pressanykeytocontinue,28,structpointMakePoint(floatx,floaty)structpointtemp;temp.x=x;temp.y=y;returntemp;voidMakeRect(structpointpt1,structpointpt2,structpointpt3,structpointpt4,structrect*screen)(*screen).pt1=pt1;(*screen).pt2=pt2;(*screen).pt3=pt3;(*screen).pt4=pt4;,29,intPtInRect(structpointp,structrectr)returnp.x=r.pt1.x,30,9.3结构体数组,结构体数组的定义三种形式:,形式一:structstudentintnum;charname20;charsex;intage;structstudentstu2;,形式二:structstudentintnum;charname20;charsex;intage;stu2;,形式三:structintnum;charname20;charsex;intage;stu2;,31,结构体数组初始化顺序初始化分行初始化,结构体数组引用引用方式:结构体数组名下标.成员名,顺序初始化:structstudentintnum;charname20;charsex;intage;structstudentstu3=100,“WangLin”,M,20,101,“LiGang”,M,19,110,“LiuYan”,F,19;,32,【例9-7】计算学生的平均成绩和不及格的人数,#include#includestructstuintnum;char*name;charsex;floatscore;boy5=101,Liping,M,45,102,Zhangping,M,62.5,103,Hefang,F,92.5,104,Chengling,F,87,105,Wangming,M,58,;,33,main()inti,c=0;/循环控制变量、不及格人数floatave,s=0;/平均成绩、总成绩for(i=0;i5;i+)s+=boyi.score;if(boyi.score60)c+=1;printf(s=%fn,s);ave=s/5;printf(average=%fncount=%dn,ave,c);,运行结果:s=345.0average=69.0count=2Pressanykeytocontinue,34,【例9-5】编写候选人得票统计程序。设有3个候选人,10张选票,每次输入一个得票的候选人名字,统计每个候选人票数并输出,#include#includestructpersoncharname20;intcount;leader3=Li,0,Zhang,0,Wang,0;,35,main()charname20;printf(input10names:n);for(inti=1;i=10;i+)scanf(%s,name);for(intj=0;jname,p-sex,p-age);,42,9.5结构指针作为函数参数,【例9-10】通过调用函输出某个学生的信息。设学生信息结构体类型有三个成员:学号、姓名和3门课成绩,#include#includestructstudentintnum;charname20;floatscore3;voidPrin

温馨提示

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

评论

0/150

提交评论