您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 辽宁石油化工大学《C语言程序设计》第七章 结构体与共用体
7StructureandUnionSummaryofthechapter77.1DefinitionandReferenceofStructure7.1.1DefinitionofStructure1——struct{11221n};7·151·structstu{intnum;charname[20];charsex;floatscore;};stu4numnamesexscorestu42.stu1structstu{intnum;charname[20];charsex;floatscore;};structstuboy1,boy2;boy1boy2stu#defineSTUstructstuSTU{intnum;charname[20];charsex;floatscore;};STUboy1,boy2;2structstu{intnum;charname[20];charsex;·152·7floatscore;}boy1,boy2;3struct{intnum;charname[20];charsex;floatscore;}boy1,boy2;structdate{intmonth;intday;intyear;};struct{intnum;charname[20];charsex;structdatebirthday;floatscore;}boy1,boy2;datemonthdayyearboy1boy2birthdaydata7.1.2ReferenceofStructureANSIC.boy1.numboy2.sex7·153·boy1.birthday.month7.1main(){structstu{intnum;char*name;charsex;floatscore;}boy1,boy2;boy1.num=102;boy1.name=Zhangping;printf(inputsexandscore\n);scanf(%c%f,&boy1.sex,&boy1.score);boy2=boy1;printf(Number=%d\nName=%s\n,boy2.num,boy2.name);printf(Sex=%c\nScore=%f\n,boy2.sex,boy2.score);}inputsexandscoref96.5Number=102Name=ZhangpingSex=fScore=96.5000007.2ArrayofStructurestructstu{intnum;char*name;charsex;·154·7floatscore;}boy[5];boy5boy[0]boy[4]structstustructstu{intnum;char*name;charsex;floatscore;}boy[5]={{101,Liping,M,45},{102,Zhangping,M,62.5},{103,Hefang,F,92.5},{104,Chengling,F,87},{105,Wangming,M,58};}7.2structstu{intnum;char*name;charsex;floatscore;}boy[5]={{101,Liping,'M',45},{102,Zhangping,'M',62.5},{103,Hefang,'F',92.5},{104,Chengling,'F',87},{105,Wangming,'M',58}};main(){inti,c=0;floatave,s=0;for(i=0;i5;i++){s+=boy[i].score;if(boy[i].score60)c+=1;}printf(s=%f\n,s);ave=s/5;printf(average=%f\ncount=%d\n,ave,c);}s=345.000000average=69.000000count=2boy5main7·155·forscoresscore60()c17.3#includestdio.h#defineNUM3structmem{charname[20];charphone[10];};main(){structmemman[NUM];inti;for(i=0;iNUM;i++){printf(inputname:\n);gets(man[i].name);printf(inputphone:\n);gets(man[i].phone);}printf(name\t\t\t\n\n);for(i=0;iNUM;i++)printf(%s\t\t\t%s\n,man[i].name,man[i].phone);}inputname:Zhangpinginputphone:inputname:Lihuainputphone:6625573inputname:Guoyaninputphone:5977204namephoneZhangping6626652Lihua6625573Guoyan5977204·156·7memnamephonemanmemforgetsforprintf7.3PointerofStructure7.3.1DefinitionofthePointerofStructurestruct*stustupstustructstu*pstu;boystupstu=&boypstu=&stu&stu7.3.2ReferenceofthePointerofStructure(*).-7·157·(*pstu).numpstu-num(*pstu).**pstu.num*(pstu.num)7.4structstu{intnum;char*name;charsex;floatscore;}boy1={102,Zhangping,'M',78.5},*pstu;main(){pstu=&boy1;printf(Number=%d\nName=%s\n,boy1.num,boy1.name);printf(Sex=%c\nScore=%f\n\n,boy1.sex,boy1.score);printf(Number=%d\nName=%s\n,(*pstu).num,(*pstu).name);printf(Sex=%c\nScore=%f\n\n,(*pstu).sex,(*pstu).score);printf(Number=%d\nName=%s\n,pstu-num,pstu-name);printf(Sex=%c\nScore=%f\n\n,pstu-sex,pstu-score);}Number=102Name=ZhangpingSex=MScore=78.500000Number=102Name=ZhangpingSex=MScore=78.500000Number=102Name=ZhangpingSex=MScore=78.500000stustuboy1stupstumainpstuboy1pstuboy1printfboy1·158·7.(*).-7.4PointerofStructureandLinkedList7.4.1WhatisLinkedList17.1headheadNULL7.1structstu{intnum;intscore;structstu*next;}nextstu27·159·intn;scanf(%d,&n);inta[n];1malloc(*)malloc(size)size(*)sizepc=(char*)malloc(100);100pc2calloc(*)calloc(n,size)n“size”(*)callocmallocnps=(struetstu*)calloc(2,sizeof(structstu));sizeof(structstu)stustu2stups3freefree(ptr);ptrptrmalloccalloc7.4.2CreateLinkedList75creat·160·7#defineNULL0#defineTYPEstructstu#defineLENsizeof(structstu)structstu{intnum;intage;structstu*next;};TYPE*creat(intn){structstu*head,*pf,*pb;inti;for(i=0;in;i++){pb=(TYPE*)malloc(LEN);printf(inputNumberandAge\n);scanf(%d%d,&pb-num,&pb-age);if(i==0)pf=head=pb;elsepf-next=pb;pb-next=NULL;pf=pb;}return(head);}#defineTYPEstructstuLENsizeof(structstu)stucreatnstucreatstuheadpfpb7.4.3OutputLinkedListheadppp76printvoidprint(structstu*head){structstu*p;printf(\nNow,These%drecordsare:\n,n);7·161·p=head;if(head!=NULL)do{printf(%d%d\n,p-num,p-age);p=p-next;}while(p!=NULL);}Ppp=p-nextpnextpp-nextpp7.4.4InsertLinkedListpi1head2headpi-next=pb;head=pi;3pi-next=pb;pf-next=pi4NULLpb-next=pi;pi-next=NULLTYPE*insert(TYPE*head,TYPE*pi){TYPE*pf,*pb;pb=head;if(head==NULL)/**/{head=pi;pi-next=NULL;}else{while((pi-numpb-num)&&(pb-next!=NULL)){pf=pb;pb=pb-next;}/**/if(pi-num=pb-num){if(head==pb)head=pi;/**/elsepf-next=pi;/**/pi-next=pb;·162·7}else{pb-next=pi;pi-next=NULL;}/**/}returnhead;}headpiheadwhileheadhead7.4.5DeleteLinkedList1.headhead=pb-next7.4headpb991019910399107NULL2.pf-next=pb-next7.5head99101pf99103pb99107NULLTYPE*delete(TYPE*head,intnum)7·163·{TYPE*pf,*pb;if(head==NULL)/**/{printf(\nemptylist!\n);gotoend;}pb=head;while(pb-num!=num&&pb-next!=NULL)/**/{pf=pb;pb=pb-next;}/*pfpb*/if(pb-num==num){if(pb==head)head=pb-next;/*headpf*/elsepf-next=pb-next;free(pb);printf(Thenodeisdeleted\n);}elseprintf(Thenodenotbeenfoud!\n);end:returnhead;}headnumpbwhilehead()(pf)()head7.5Union7.5.1DefinitionofUnion1·164·7union{1122…}uniondata{inti;charch;floatf;};icf2:uniondata{in
本文标题:辽宁石油化工大学《C语言程序设计》第七章 结构体与共用体
链接地址:https://www.777doc.com/doc-4024244 .html