




已阅读5页,还剩46页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.,chapter10STRUCTURESANDUNIONS,Astructureisacollectionofoneormorevariables,possiblyofdifferenttypes,whicharetreatedasaunitinsteadofasseparateentities.10.1structuredefinition10.2structurevariablescitingandinitialization10.3structurearray10.4structurepointer10.5linkdispoing-applicationofstructurepointer10.6unionandenumeration10.7typedef,.,10.1STRUCTURETYPEANDDECLARATIONOFSTRUCTUREVARIABLES,THESTRUCTURETYPE,INCLANGUAGE,isthecounterpartas“record”inotheradvancedlanguage.一、questionemergingSTUDENTSCORESMANAGETABLECANOTDEALWITH2DARRAYS,DIFFERENTTYEPES!,.,二、structuretypedeclarationCommonusage:structstructuretypenamedatatypemember1;datatypemember2;datatypemembern;,structstudentintnum;charname20;charsex;intage;floatscore1;floatscore2;,Typename,structstudent,.,demonstration:1、therulestoname“structuretypename”and“membername”,aresamewiththevariables.2、thedefinitionsofvariablescanbeseperatedorinasamelineliketheinstancebeloweg.structdateintyear,month,day;3、thedefinitionsofvariablescouldbebasictypesoranotherstructuretypewhichhasbeendeclarated.eg.thetype”birthday”intheeg.Ofstructuretypestd_infoisadefinitedstructuretype-date,.,三、STRUCTUREVARIABLESDECLARATIONStructuretype(userdefinition)Standarttype(systemdefinition)1、indirectdeclarationfirstdeclarethestructuretype,thenthestructurevariables,Thesameusage,structstudentintnum;charname20;charsex;intage;floatscore1;floatscore2;,structstudentstu1,stu2;,numnamesexagescore1score2,stu1,.,2、directdeclaration(1)definitethestructurevariable,whendefinitingthestructuretype;(2)definitethestructurevariabledirectly;,(1)structstudentintnum;charname20;floatscore2;stu1,stu2;,(2)structintnum;charname20;floatscore2;stu1,stu2;,3、demonstration(1)differstructuretypeandvariable,whicharecompletelydifferentconception,justastypeintandvariableofint(2)membernamesinstructure,maybesameasothervariablesinprogram,standfordifferentobjects,.,eg10.1creatastructurerefferingtobasicinformationofastudenttostorethedetailsofastudentstructdate/*datastructuretypemadeofyear,monthandday*/intyear;intmonth;intday;structstd_info/*studentinformationstructuretype*/intnum;/*madeofname,sex,birthday,etc*/charname12;charsex;structdatebirthday;floatscore1,score2;,.,10.2CITINGANDINITIALIZATIONSOFSTUCTUREVARIABLES,例10.2makeuseoftheformerstucturetypestructstd_info,todefiniteastructuretypestutostoreandprintthestudentdetails#include“struct.h”structstd_infostu=1002,”ZhangSan”,M,1980,9,20,98.2,86.5;main()printf(No:%dn,stu.num);printf(Name:%sn,);printf(Sex:%cn,stu.sex);printf(Birthday:%d-%d-%dn,stu.birthday.year,stu.birthday.month,stu.birthday.day);,.,result:No:1002Name:ZhangSanSex:MBirthday:1980-9-20,.,1、INITIALIZATIONSOFSTUCTUREvariablestructtypestruct_variable=initialvaluelisteg,structstd_infostu=“000102”,”ZhangSan”,M,1980,9,20;note:datatypeofinitialvalueshouldbeconsistentwiththemembersinstructurevariable,ortherewillbeerror.2、citingrulesofstructurevariablememberoperator“.”whilecitingthememberinstructure,useoperator”.”;citingformat:structurevariable.membereg,stu.num;,etc。,.,NOTE:ifamemberitselfisastructuretype,amultilevelmemberoperationisneededtocitethelowestmember.Expandingformat:structurevariable.member.child-member.LowestmemberEg.:stu.birthday.yearstu.birthday.monthstu.birthday.daybetakingthelowestmemberisequaltocommonvariableofthesametype.,.,10.3STRUCTUREARRAYS,Structurearrays:anarraymadebystructuretypedatawhichisaselementinstructure.Everyarrayelement:includeallmembersofstructure例10.3usestructurearraytostoreandprintthestudentscoresmanagetablebelow.,stu0stu1stu2stu3,.,structstudent/*laythedefinitionofstructuretypeoutsidefunction*/intnum;charname12;charsex;intage;floatscore1;floatscore2;structstdentstu4=1001,”ZhangXin”,M,20,87,91,1002,”WangLi”,F,20,98,96,1003,”ChenFong”,M,21,86,90,1004,”LiXiaopen”,M,20,77,86;,Storeinfile“struct.h”,.,#includestruct.h”main()inti;/*printlisthead,“standsforonebackspace*/printf(No.NameSexAgesoc1sco2n);/*outputbasicdetailsof4students*/for(i=0;i4;i+)printf(%-5d,stui.num);printf(%-12s,);printf(%-4c,stui.sex);printf(%-4d,stui.age);printf(%-5.1f,stui.score1);printf(%-5.1fn,stui.score2);,.,Runningresult:No.NameSexAgesoc1sco21001ZhangXinM2087911002WangLiF2098961003ChenFongM2186901004LiXiaopenM207786,.,eg10.4usearraysofstructureasfuntionparameters#includestruct.hvoidprint_stulist(structstudentstu)inti;printf(No.NameSexAgesoc1sco2n);for(i=0;i4;i+)printf(%-5d,stui.num);printf(%-12s,);printf(%-4c,stui.sex);printf(%-4d,stui.age);printf(%-5.1f,stui.score1);printf(%-5.1fn,stui.score2);,UseArrayofstructureasformalparameter,.,voidsort(structstudentst)inti,j;structstudenttemp;for(i=0;i4-1;i+)for(j=i+1;j4;j+)if(sti.score1stj.score1)temp=sti;sti=stj;stj=temp;,main()clrscr();print_stulist(stu);sort(stu);printf(listaftersorted:n);print_stulist1(stu);getch();,Structurearrayasformalparameter,.,Runningresult:No.NameSexAgesoc1sco21001ZhangXinM2087911002WangLiF2098961003ChenFongM2186901004LiXiaopenM207786listaftersorted:No.NameSexAgesoc1sco21002WangLiF2098961001ZhangXinM2087911003ChenFongM2186901004LiXiaopenM207786,.,10.4pointerpointedtodatatypeofstructure,Pointerofstructurevariable:structurevariableexistsinthestartingpositionofthestorage.voidprint_stulist(structstudentstu)inti;printf(No.NameSexAgesoc1sco2n);for(i=0;i4;i+)printf(%-5d,stui.num);printf(%-12s,);printf(%-4c,stui.sex);printf(%-4d,stui.age);printf(%-5.1f,stui.score1);printf(%-5.1fn,stui.score2);,modify!Usepointerwhichpointtostructureasformalparameter,.,eg10.5usepointertocallmemberofthestructurevariablevoidprint_stulist1(structstudent*p)inti;printf(No.NameSexAgesoc1sco2n);for(i=0;i4;i+)printf(%-5d,(*(p+i).num);printf(%-12s,(*(p+i).name);printf(%-4c,(*(p+i).sex);printf(%-4d,(*(p+i).age);printf(%-5.1f,(*(p+i).score1);printf(%-5.1fn,(*(p+i).score2);,Ifusefor(i=0;inext=p;,/*4.lastmovebackwards*/last=p;,Repeat24,whenx0,/*setpointerareaNULLwhichwaspointedbytailnodepointer,headpointermovebackwards(deletingheadnode)*/last-next=NULL;head=head-next;,.,main()structnode*head,*p,*last;intx;head=(structnode*)malloc(LEN);last=head;while(1)scanf(%d,#defineNULL0#defineLENsizeof(structnode)structnodeintnum;structnode*next;,p=head;head=head-next;free(p);,.,/*outputlinklist*/printf(“dataofthelinklistare:”)p=head;while(p!=NULL)printf(%5d,p-num);p=p-next;printf(n);getch();Runningresult:13250dataofthelinklistare:1325,Q:make2functionsforcreatinglinklistandoutputlinklist:,1.structnode*creat(void)2.voidprint(structnode*head),.,structnode*creat(void)/*createlinklistfunction*/structnode*head,*p,*last;intx;head=(structnode*)malloc(LEN);last=head;while(1)scanf(%d,2、outputlinklist1、installlinklist四、realizingbyfunction:,.,voidprint(structnode*head)/*outputlinklistfunction*/structnode*p;p=head;while(p!=NULL)/*traversingthelinklist*/printf(%5d,p-num);p=p-next;printf(n);getch();main()structnode*head;printf(inputdatatocreatlinklist(endbythenumberofnode)disposingmanner:(1)error(2)inserttothetail,.,structnode*insert(structnode*head,inti,intx)structnode*s,*p;s=(structnode*)malloc(LEN);s-num=x;if(head=NULL)head=s;s-next=NULL;/*1、inserttothenulllinklist*/elseif(i=0)/*non-nulllinklist*/s-next=head;head=s;/*2、newnodeasnewheadnodeoflinklist*/elsep=head;/*lookuptheistnode(pointedbyp)*/for(;p!=NULL,.,main()structnode*head;inti,x;printf(inputdatatocreatlinklist(endbynext;/*lookupthenodewhosevalueisx*/for(;p-num!=x,.,espescial:1、NULLlisthead=NULLerrorinformation2、deletetheheadnodepre-deleted:after-deleted:3、nosuchnodeerrorinformation,.,structnode*delete_list(structnode*head,intx)structnode*p,*q;if(head=NULL)/*1、blanklist*/printf(Error!Thelistisempty.n);elseif(head-num=x)/*2、deletethefirstnode*/head=head-next;elseq=head;p=q-next;for(;p-num!=x,.,10.5.5colligatedexample:menuprogramdesigning,/*=sortedlink_listoperater=*/*DesignedbySunQiaoping*/#includestdio.h;#defineNULL0#defineLENsizeof(structnode)structnodeintnum;structnode*next;charmenu(void);voidprint(structnode*head);structnode*insert_sortedlist(structnode*head,intx);structnode*creat_sortedlist(void);structnode*delete_list(structnode*head,intx);,.,main()structnode*head=NULL;inti,x,flag=1;charch;doch=menu();clrscr();switch(ch)case1:head=creat_sortedlist();break;case2:printf(inputanumbertoinsert.x=);scanf(%d,.,/*=menu=*/charmenu(void)charch;clrscr();printf(MENUn);/*puts()*/printf(=n);printf(1.creatn);printf(2.insertn);printf(3.deleten);printf(4.printn);printf(0.exitn);printf(=n);printf(Inputyourchoice(0,1,2,3,4):);ch=getchar();return(ch);,.,/*=insert=*/structnode*insert_sortedlist(structnode*head,intx)structnode*s,*p,*q;s=(structnode*)malloc(LEN);s-num=x;if(head=NULL)/*inserttoablanklist*/head=s;s-next=NULL;elseif(xnum)/*inserttoheadoflist*/s-next=head;head=s;elseq=head;p=q-
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年物资储备仓库安全员招聘考试重点解析
- 甲状腺肿课件
- 脑干损伤护理查房
- 黑龙江省哈尔滨市联考2024-2025学年高二下学期7月期末教学质量检测物理试题(含答案)
- 中班动画城教学课件
- 用橡皮筋作动力课件
- 急性肾功能衰竭钙磷紊乱护理查房
- 急性脊髓炎高位截瘫护理查房
- 生活常识应急知识培训课件
- 癫痫持续状态护理查房记录
- 公安擒拿教学课件
- 产后耻骨护理
- JG/T 197-2018预应力混凝土空心方桩
- 影响购房者决定的心理因素研究
- 楼板加固施工协议书
- 《室内绿植布置》课件
- 手术医师人员档案
- 回收黄金免责合同协议
- 广东省广州市2025届普通高中毕业班综合测试(二)英语试题(含答案)
- 湖南省张家界市永定区2024-2025学年九年级下学期毕业学业水平考试模拟(一)语文试题(含答案)
- 开利30HXY-HXC螺杆冷水机组开机、运行维护手册
评论
0/150
提交评论