-C語(yǔ)言程序設(shè)計(jì)課件(英文)C-prog_第1頁(yè)
-C語(yǔ)言程序設(shè)計(jì)課件(英文)C-prog_第2頁(yè)
-C語(yǔ)言程序設(shè)計(jì)課件(英文)C-prog_第3頁(yè)
-C語(yǔ)言程序設(shè)計(jì)課件(英文)C-prog_第4頁(yè)
-C語(yǔ)言程序設(shè)計(jì)課件(英文)C-prog_第5頁(yè)
已閱讀5頁(yè),還剩32頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論