版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
StructureXueQing2009.4StructureXueQing1
Structure
WhatisStructureDefinitionInitializationAccessthemembersofstructureStructureandpointerStructureandfunction
StructureandarrayStructureWhatisStru2number,name,score1,score2,score3,score4WhatisStructureInordertodealwiththespecialdatawhichhasseveralmemberwithdifferentdatatype,itisneedtointroducenewdatatype.Array—anorderedsetwiththesamedatatype.1090569731inta[8]23654081inta[2][4]sametypeQuestion:intno,charname[20],floats1,s2,s3,s4onestudent:
strutruenumber,name,score1,score23WhatisStructureStructure1StructureisaadvanceddatatypeinC.2Structureisamethodforgroupingaseveralrelateddatatypetogether.3VariableswithdifferenttypescanbegroupedinastructureThesimplevariables:inta,b;a=20,b=85;-invidual
Array:floatgrade[20];
sametyperelatedgrade[0]grade[1]grade[2]grade[3]……WhatisStructureStructure14WhatisStructurenumnamegrade1grade210Li90.5A11Liu80B12Wen88B….
intcharfloatchar
Howcanwedefinerelateddatathathavedifferenttype?
struct{intnum;charname[10];floatgrade1;chargrade2;};WhatisStructurenumnamegrade15WhatisStructureAllvariableswiththesametypescanbegroupedinaarray.wang98889887li87668367zhan78877561liu90768172zhao87817471AllvariableswithdifferenttypescanbegroupedinastructureContentnameagesexaddwang18m3-110li20f7-121liu17f7-212zhao18m3-222structuremembers(type,name)StructurevariablesWhatisStructureAllvariable6Definition1
32Astructureisacollectionofrelatedvariables,anynumberoftypeofvariablesmaybeincludedwithinit.Definingstructure,thendeclaringstructvariables.Definingstructureandstructvariables.structure’snamecanbeomittedkeywordstructStructuremembersSturcturevariablesStructurevariablevaluesDefinition132Astructure7structname
{type1member1;type2member2;︰typenmembern;};1Definingstructure,thendeclaringstructvariables.Definition
structstudent{intnum;charname[10];floatgrade1;chargrade2;};Structureisonlyasstructuretemplate,notoccupyingmemory,becausenoanyvariableyet.A)Definingstructurenumnamegrade1grade2structname1Definingstruc8structdata{charname[20];intage;charaddress[30];longtelephone;}structstruct_namestruct_variable_name;structdatawang;structdatali,zhang;DefinitionB)Declaringstructurevariablenameageaddresstelephonestructdatastructstruct_name9name[20]
age20bytes2bytes30bytes4bytesaddresstelephonename[20]ageaddress[30]telephonestructdata{charname[20];intage;charaddress[30];longtelephone;};structdatawang;Afterdeclaration,computerwillassignmemoryunitstothem.Definitionname[20]age20bytes2byte10
main()
{charstr[20];
structdate{intyear,month,day;}today;
structaddress{charname[30],street[40],city[20],state[2];unsignedlongintzip;}wang;
printf("char:%d\t",sizeof(char));printf(“int:%d\t”,sizeof(int));printf("long:%d\t",sizeof(long));printf("float:%d\n",sizeof(float));printf("double:%d\t",sizeof(double));printf("str:%d\t",sizeof(str));printf("date:%d\t",sizeof(structdate));printf(“wang:%d\n”,sizeof(wang));}Definitionmain(){charstr[20];112.Definingstructureandstructvariables.structname{type1member1;type2member2;︰typenmembern;}variable1,variable2…..;
structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;numnamesexagescoreaddstu1stu2Definition2.Definingstructureandst123.structure’snamecanbeomittedstruct{type1member1;type2member2;︰typenmembern;}variable1,variable2…..;
struct{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;Definitionnumnamesexagescoreaddr201wangM2095Zhongguancunroad202liF1980GuangdaGarden3.structure’snamecanbeo13Initialization
structstudent
{intnum;charname[20];charsex;intage;charaddr[30];};structstudentstu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};Whendefiningstructure,assignvaluestoeachstruct_variable(eachmember)onebyone.Shouldbeagreedwiththetypeofcorrespondingmembers.Initializationstructstud14
structstudent{intnum;charname[20];charsex;intage;charaddr[30];}stu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};
struct{intnum;charname[20];charsex;intage;charaddr[30];}stu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};InitializationHowtousethesevalues?structstudentstructIn15Accessstructuremembersstud1.grade1=95;stud1.grade2=‘A’;where“.”iscalled
memberoperator.struct_var_name.member_name
struct{intnum;charname[10];floatgrade1;chargrade2;}stud1,stud2,stud3;scanf(“%s,%f,%c,”,&,&stud2.grade1,&stud2.grade2);printf(“%s,%f,%c,”,,stude2grade1,stude2grade2);Accessstructuremembersstud16/*assignment*/main(){struct{charinitial;/*lastnameinitial*/intage;/*childsage*/intgrade;/*childsgradeinschool*/}boy,girl;boy.initial='R';boy.age=17;boy.grade=75;
girl.age=boy.age-1;/*sheisoneyearyounger*/girl.grade=82;girl.initial='H';printf("%cis%dyearsoldandgotagradeof%d\n",girl.initial,girl.age,girl.grade);printf("%cis%dyearsoldandgotagradeof%d\n",boy.initial,boy.age,boy.grade);}/*assignment*/17AccessstructuremembersOnestructuremaybenestedwithinanother.nameworkaddressHomeaddresspostaddrtelpostaddrtelstructaddress{intpostcharaddr[100];chartel[20];};structpersonal{charname[20];structaddressworkaddr;structaddresshomeaddr;};struct_var_name.out_mem.inner_memAccessstructuremembersOne18main(){structdate{/*structuretypedate*/intyear,month,day;
};structdatetoday;/*stru-vartoday*/printf("Entertodaydate:");scanf(“%d%d%d”,&today.year,&today.month,&today.day);printf(“%d.%d.%d\n”,today.year,today.month,today.day);}date&today.daytoday.dayAccessstructuremembersmain()date&today.daytoday.da19Exercise:
structstudent{intnocharname[20];charsex;struct
{intyear;intmonth;intday;}birth;};structstudents;A)year=1982month=11day=11B)birth.year=1982,birth.month=11birth.day=11C)s.year=1982s.month=11s.day=11D)s.birth.year=1982s.birth.month=11s.birth.day=11
Ifthebirthdayis“11/11
/82”,whichisthecorrectassignmentstatement?AccessstructuremembersExercise:structstudentA)yea20
1DefinitionForm1
structstudent{intnum;charname[20];charsex;intage;};structstudentstu[30];Form2
structstudent{intnum;charname[20];charsex;intage;}stu[30];Form3
struct{intnum;charname[20];charsex;intage;}stu[30];Array–groupseveralrelatedvariableswiththesametype,storeinsequence30studentsInoneclassThetypeisstructureStructureandarray1DefinitionForm1Form2For21TherelationshipbetweenstructureandarrayArrayisamemberofastructureAnarraywhichmembersareallstructurevariablenumNameScore[0]Score[1]101wang9098110zhan8775struct{intnum;
charname[20];floatscore[2];}stu1,stu2;numNameScore[0]Score[1]101wang9098110zhan8775struct{intnum;
charname[20];floatscore[2];}stu[2];StructureandarrayTherelationshipbetweenstruc22Initualizationstructstud{intxh;......};structstudxscj[3]={{},{},{}};
OR:structstud{intxh;......}xscj[3]={{},{},{}};DatainonearrayStructureandarrayInitualizationstructstudOR:st23Accessmembersofstructurearraymain(){structxscjxs[4]={{"01",70,80},{"02",78,67},{"03",56,78},{"04",90,80}};inti;for(i=0;i<4;i++){xs[i].av=(xs[i].cj[0]+xs[i].cj[1])/2.0;printf("%s,%5.1f,%5.1f,%5.1f\n",xs[i].xh,xs[i].cj[0],xs[i].cj[1],xs[i].av);}}structxscj{char*xh;floatcj[2];floatav;};structurearraymemberofstructurememberofthearrayoutput01,70.0,80.0,75.002,78.0,67.0,72.503,56.0,78.0,67.004,90.0,80.0,85.0StructureandarrayAccessmembersofstructurear24Afterdeclaration,howtooutput‘M’?structperson{charname[9];intage;};structpersonclass[10]={“Wang”,17,“Zhang”,19,“Ming”,18,“Liu”,20};A)printf(“%c\n”,class[3].name);B)printf(“%c\n”,class[3].name[1]);C)printf(“%c\n”,class[2].name[1]);D)printf(“%c\n”,class[2].name[0]);MemberofarrayMemberofstructurearrayStructureandarrayAfterdeclaration,howtooutp25Structureandpointer
Structurepointersarejustlikepointerstoordinaryvariables.Thedeclarationstructname*ps;Ifpspointstoapointstructure,*psisthestructure,and(*ps).xand(*ps).yarethemembers.Ifpsisapointertoastructure,then
ps->member-of-structure
referstotheparticularmember.Toaccessthememberofstructurebypointer(*pointername).membernamepointername->membernameStructureandpointerStruc261Pointstothestructurevariablestructxscj{char*xh;floatcj[2];floatav;}xs={"02",78,67};main(){structxscj*p=&xs;p->av=(p->cj[0]+p->cj[1])/2;printf("%s,%5.1f,%5.1f,%5.1f\n",p->xh,p->cj[0],p->cj[1],p->av);}Structureandpointeroutput:02,78.0,67.0,72.5DeclarationofstructureDeclarationandinitualizationofstructurevariableDeclarationandinitualizationofstructurepointerAccessthestructurememberbypointer1Pointstothestructurevar27Pointstothestructurearraymain(){structxscjxs[4]={{"01",70,80},{"02",78,67},{"03",56,78},{"04",90,80}};structxscj*p;for(p=xs;p<xs+4;p++){p->av=(p->cj[0]+p->cj[1])/2.0;printf("%s,%5.1f,%5.1f,%5.1f\n",p->xh,p->cj[0],p->cj[1],p->av);}}structxscj{char*xh;floatcj[2];floatav;};01,70.0,80.0,75.002,78.0,67.0,72.503,56.0,78.0,67.004,90.0,80.0,85.0StructureandpointerPointstothestructurearray281Structurememberasargumentstructxscj{char*xh;floatcj[2];floatav;};Structureandfunctionmain(){structxscjxs[4]={{"01",70,80},{"02",78,67},{"03",56,78},{"04",90,80}};inti;for(i=0;i<4;i++){xs[i].av=ave(xs[i].cj[0],xs[i].cj[1]);printf("%s,%5.1f,%5.1f,%5.1f\n",xs[i].xh,xs[i].cj[0],xs[i].cj[1],xs[i].av);}}ave(floatx,floaty){floata;a=(x+y)/2.0;returna;}01,70.0,80.0,75.002,78.0,67.0,72.503,56.0,78.0,67.004,90.0,80.0,85.01Structurememberasargumen29Structureandfunction2Structureasargumentstructxscj{char*xh;floatcj[2];floatav;};voidf1(structxscjtj);main(){structxscjxs={"01",70,80,0};f1(xs);}
voidf1(structxscjtj){tj.av=(tj.cj[0]+tj.cj[1])/2.0;printf("%s,%5.1f,%5.1f,%5.1f\n",tj.xh,tj.cj[0],tj.cj[1],tj.av);}output01,70.0,80.0,75.0Structureandfunction2Stru30Structureandfunction3Structurepointerasargumentstructxscj{char*xh;floatcj[2];floatav;};voidf1(structxscj*tj);main(void){structxscj*pxs,xs={"01",70,80,0};pxs=&xs;f1(pxs);}voidf1(structxscj*tj){tj->av=(tj->cj[0]+tj->cj[1])/2.0;printf("%s,%5.1f,%5.1f,%5.1f\n",tj->xh,tj->cj[0],tj->cj[1],tj->av);}01,70.0,80.0,75.0Structureandfunction3Stru31統(tǒng)計(jì)十佳運(yùn)動(dòng)員選票,在統(tǒng)計(jì)之前要將預(yù)選的運(yùn)動(dòng)員名字賦給相應(yīng)的name數(shù)組,同時(shí)將每個(gè)人的選票計(jì)數(shù)器清零。structports{char*name;intcount;
}x[]={"LiNing",0,"LangPing",0,"ZhuJianHua",0,"LanJuJie",0}; main(){inti;for(i=0,i<=3;i++)printf("%s:%d\n",x[i].name,x[i].count);
}Examples統(tǒng)計(jì)十佳運(yùn)動(dòng)員選票,在統(tǒng)計(jì)之前要將預(yù)選的運(yùn)動(dòng)員名字賦給相應(yīng)的32簡(jiǎn)單的加密程序。可定義一個(gè)結(jié)構(gòu)table的成員in,存入輸入的字符,out輸出加密后的字符。
輸入(in):abcdefghijkw
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024自來(lái)水安裝工程合同樣本
- 2024定制窗簾合同范例
- 2024年裝修合同范本標(biāo)準(zhǔn)版
- 2024建筑裝飾裝修合同
- 2024個(gè)人借款合同書模板
- 2024水電改造合同(范本)
- 成功案例介紹:數(shù)字化農(nóng)業(yè)實(shí)踐
- 2024承攬合同電腦及相關(guān)設(shè)備維護(hù)服務(wù)合同
- 廣告語(yǔ)言與傳播理論考核試卷
- 天然氣開采業(yè)的安全生產(chǎn)與安全文化建設(shè)考核試卷
- 中小學(xué)-消防安全知識(shí)教育-課件
- 職業(yè)院校“金課”建設(shè)方案
- 新質(zhì)生產(chǎn)力-講解課件
- 組織行為與領(lǐng)導(dǎo)力智慧樹知到期末考試答案2024年
- 30道計(jì)量員崗位常見面試問題含HR問題考察點(diǎn)及參考回答
- 醫(yī)?;鸨O(jiān)管知識(shí)考試題庫(kù)300題(含答案)
- 校園欺凌談話記錄表
- 計(jì)算機(jī)專業(yè)生涯發(fā)展展示
- 體質(zhì)測(cè)試成績(jī)表(自動(dòng)統(tǒng)計(jì)數(shù)據(jù))(小學(xué)、初中)
- 基于PLC四層電梯控制系統(tǒng)設(shè)計(jì)畢業(yè)論文
- 齊魯制藥處方藥營(yíng)銷策略及實(shí)施
評(píng)論
0/150
提交評(píng)論