![計算概論結(jié)構(gòu)_第1頁](http://file4.renrendoc.com/view/f6286e58370cd296cd5dc8c5ea05604c/f6286e58370cd296cd5dc8c5ea05604c1.gif)
![計算概論結(jié)構(gòu)_第2頁](http://file4.renrendoc.com/view/f6286e58370cd296cd5dc8c5ea05604c/f6286e58370cd296cd5dc8c5ea05604c2.gif)
![計算概論結(jié)構(gòu)_第3頁](http://file4.renrendoc.com/view/f6286e58370cd296cd5dc8c5ea05604c/f6286e58370cd296cd5dc8c5ea05604c3.gif)
![計算概論結(jié)構(gòu)_第4頁](http://file4.renrendoc.com/view/f6286e58370cd296cd5dc8c5ea05604c/f6286e58370cd296cd5dc8c5ea05604c4.gif)
![計算概論結(jié)構(gòu)_第5頁](http://file4.renrendoc.com/view/f6286e58370cd296cd5dc8c5ea05604c/f6286e58370cd296cd5dc8c5ea05604c5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
計算概論結(jié)構(gòu)第一頁,共四十一頁,2022年,8月28日什么是結(jié)構(gòu)問題:現(xiàn)實世界中的事物都具有一些屬性;例如,學生有“學號”、“姓名”、“性別”、“年齡”等;如果在程序中分別定義“學號”…,難記,難用;難以體現(xiàn)出某些信息都是隸屬于某個事物的;在程序中,希望能夠用一個相對獨立的數(shù)據(jù)結(jié)構(gòu)來存儲與某個事物相關(guān)的信息;能不能設(shè)計一種數(shù)據(jù)結(jié)構(gòu),把這些分散的屬性封裝起來讓他們“看起來”象一個整體,用起來也可以作為整體來用結(jié)構(gòu)是一種構(gòu)造類型,是由各種類型構(gòu)造而成;將各種不同類型但相關(guān)的數(shù)據(jù)“集合”起來;第二頁,共四十一頁,2022年,8月28日什么是結(jié)構(gòu)聲明一個名為“學生”的結(jié)構(gòu)structstudent \\結(jié)構(gòu)的名字為“student”;
{ intid; \\聲明學號為int型;
char
name[20];\\聲明姓名為字符數(shù)組;
char
sex; \\聲明性別為字符型;
intage; \\聲明年齡為整型;
float
score;\\聲明成績?yōu)閷嵭停?/p>
char
addr[30];\\聲明地址為字符數(shù)組
}; \\注意大括號后的“;”第三頁,共四十一頁,2022年,8月28日結(jié)構(gòu)的定義struct結(jié)構(gòu)體名稱
{類型名1成員名1;類型名2成員名2;
……;類型名n成員名n;
};第四頁,共四十一頁,2022年,8月28日聲明結(jié)構(gòu)類型的變量錯誤的理解“給出了student類型數(shù)據(jù)的定義,就可以使用student這個結(jié)構(gòu)了” NO?。。。?!聲明的結(jié)構(gòu)是一種數(shù)據(jù)類型
student僅僅是一種新生的“數(shù)據(jù)類型”從此,編譯器認識一種“student類型”,就像int型,float型,char型一樣。必須利用所聲明的結(jié)構(gòu),定義“結(jié)構(gòu)類型的變量”才能夠使用必須聲明一個“student類型”的變量才能夠使用就像不能夠直接對“int”,“float”…進行計算操作第五頁,共四十一頁,2022年,8月28日定義結(jié)構(gòu)類型的變量定義結(jié)構(gòu)變量的方式(1)直接用已聲明的結(jié)構(gòu)體類型定義變量名
structstudentstudent1,student2;(結(jié)構(gòu)類型名)(結(jié)構(gòu)體變量名);
studentstudent1,student2;
對比:inta; (structstudent相當于int)floata;(structstudent相當于float)第六頁,共四十一頁,2022年,8月28日定義結(jié)構(gòu)類型的變量(2)在聲明類型的同時定義變量structstudent \\結(jié)構(gòu)的名字為“student”;
{int id; \\聲明學號為int型;
char
name[20];\\聲明姓名為字符數(shù)組;
char
sex; \\聲明性別為字符型;
int
age; \\聲明年齡為整型;
float
score; \\聲明成績?yōu)閷嵭停?/p>
char
addr[30]; \\聲明地址為字符數(shù)組
}S_1,S_2; \\注意最后的“;”第七頁,共四十一頁,2022年,8月28日定義結(jié)構(gòu)體類型的變量(3)直接定義結(jié)構(gòu)變量struct \\聲明無名字結(jié)構(gòu)體;
{ int id; \\聲明學號為int型;
char
name[20];\\聲明姓名為字符數(shù)組;
char
sex; \\聲明性別為字符型;
int
age; \\聲明年齡為整型;
float
score; \\聲明成績?yōu)閷嵭停?/p>
char
addr[30]; \\聲明地址為字符數(shù)組
}S_1,S_2; \\注意最后的“;”第八頁,共四十一頁,2022年,8月28日結(jié)構(gòu)可以嵌套structstudent{ intid; charname[20]; charsex; intage;
structdatebirthday; charaddr[30];}student1,student2;structdate{ intmonth; intday; intyear;};第九頁,共四十一頁,2022年,8月28日結(jié)構(gòu)變量的引用引用結(jié)構(gòu)變量中成員的方式為
結(jié)構(gòu)變量名.成員名結(jié)構(gòu)體變量中的成員要單獨引用時,使用成員運算符“.”。如:student1.id=10010; 有嵌套的結(jié)構(gòu)成員,需用多個“.”訪問。
如:student1.birthday.month=10;不能將一個結(jié)構(gòu)變量作為一個整體進行輸入和輸出不正確的引用:cout<<student1;cin>>student1;只能對結(jié)構(gòu)變量中的各個成員分別進行輸入和輸出正確的引用:cin>>student1.id;cout<<student1.id;第十頁,共四十一頁,2022年,8月28日結(jié)構(gòu)變量的引用結(jié)構(gòu)中的成員,可以單獨使用,相當于普通變量:student1.age=student2.age;student1.age++;成員名可以與程序中的變量名相同:structdate{ intmonth; intday; intyear;};main(){ intday=7; structdateChristmas={12,25,2007}; cout<<day<<endl; cout<<Chrismas.day<<endl;}第十一頁,共四十一頁,2022年,8月28日結(jié)構(gòu)變量的初始化structdate{ intmonth; intday; intyear;};structstudent{ intnum; charname[20]; charsex; intage; structdatebirthday; charaddr[30];}student1={121,“zhang",'M',20,{12,30,2000},"PKU"},student2={122,“wang”,‘M’,20,{12,30,2000},“PKU”};studentstudent3 ={123,“zhao",'M',20,{12,30,2000},"PKU"};結(jié)構(gòu)可以在定義時進行初始化第十二頁,共四十一頁,2022年,8月28日結(jié)構(gòu)變量的賦值類型相同的結(jié)構(gòu)變量可以進行賦值例如:要將student1和student2互換temp=student1;student1=student2;student2=temp;(temp也必須是相同類型結(jié)構(gòu)變量)第十三頁,共四十一頁,2022年,8月28日結(jié)構(gòu)變量的賦值不同類型的結(jié)構(gòu)變量,即使成員完全一樣也不允許互相賦值。如下例中若:person1=student1;,則編譯錯誤!structstudent{ intstuNo; intname[20]; charsex; intage; charaddr[30];}student1,student2;structperson{ intstuNo; intname[20]; charsex; intage; charaddr[30];}person1,person2;第十四頁,共四十一頁,2022年,8月28日結(jié)構(gòu)數(shù)組結(jié)構(gòu)數(shù)組每個數(shù)組元素都是一個結(jié)構(gòu)變量舉例
structstudentstu[3];第十五頁,共四十一頁,2022年,8月28日結(jié)構(gòu)數(shù)組的初始化struct student{ intnum; charname[20]; charsex; intage; floatscore; charadd[30];}stu[3]={ {10101,“LiLin”,‘M’,18,87.5,“103BeijingRoad”}, {10102,“ZhangFun”,‘M’,19,99,“130ShanghaiRoad”}, {10104,“WangMin”,‘F’,20,78.5,“1010ZhongshanRoad”} };第十六頁,共四十一頁,2022年,8月28日結(jié)構(gòu)數(shù)組的引用structdate{ intmonth; intday; intyear;};structstudent{ intnum; charname[20]; charsex; intage; structdatebirthday; charaddr[30];}mystudents={ {121,“zhang",'M',20,{12,30,2000},"PKU"}, {122,“wang",'M',20,{12,30,2000},"PKU"}, {123,“zhao",'M',20,{12,30,2000},"PKU"}};正確的引用:
mystudents[0].age; mystudents[0].date.day;錯誤??!
mystudents.age[0];第十七頁,共四十一頁,2022年,8月28日結(jié)構(gòu)數(shù)組應用示例(1)對候選人的得票進行統(tǒng)計,不用結(jié)構(gòu)voidmain(){ charname[5][20]={"A","B","C","D","E"}; intcount[5]={0,0,0,0,0}; charvote[20]; for(inti=1;i<=10;i++) { cin.getline(vote,20); for(intj=0;j<5;++) if(strcmp(vote,name[j])==0) count[j]++; } for(i=0;i<5;i++) { cout<<setw(5)<<name[i]<<setw(2)<<count[i]<<endl; }}第十八頁,共四十一頁,2022年,8月28日voidmain(){ structperson {charname[20]; intcount; }candidate[5]={"A",0,"B",0,"C",0,"D",0,"E",0}; charvote[20]; for(inti=1;i<=10;i++) { cin.getline(vote,20); for(intj=0;j<5;j++) if(strcmp(vote,candidate[j].name)==0) candidate[j].count++; } for(i=0;i<3;i++) cout<<setw(5)<<candidate[i].name <<setw(2)<<candidate[i].count<<endl;}結(jié)構(gòu)數(shù)組應用示例(1)第十九頁,共四十一頁,2022年,8月28日#include<iostream.h>#include<string.h>#include<iomanip.h>voidmain(){ charch;intk; structalpha //定義結(jié)構(gòu)用于存放字母及出現(xiàn)次數(shù)
{ charname; intcount; }letter[26],t; for(inti=0;i<26;i++) //對結(jié)構(gòu)體變量letter進行初始化
{ letter[i].name='a'+i; letter[i].count=0; } for(i=0;i<50;i++) //輸入字母并記錄每個字母的出現(xiàn)次數(shù)
{ cin>>ch; k=ch-'a';
letter[k].count++; }結(jié)構(gòu)數(shù)組應用示例(2)從鍵盤上輸入50個字母(小寫),按字母出現(xiàn)的頻數(shù)由大到小排序第二十頁,共四十一頁,2022年,8月28日for(i=0;i<25;i++) //排序運算
for(intj=0;j<25-i;j++) if(letter[j].count<letter[j+1].count) { t=letter[j]; letter[j]=letter[j+1]; letter[j+1]=t; }for(i=0;i<25;i++) cout<<letter[i].name<<letter[i].count;}結(jié)構(gòu)數(shù)組應用示例(2)從鍵盤上輸入50個字母(小寫),按字母出現(xiàn)的頻數(shù)由大到小排序第二十一頁,共四十一頁,2022年,8月28日指向結(jié)構(gòu)類型數(shù)據(jù)的指針因為結(jié)構(gòu)類型與其他數(shù)據(jù)類型相同;一個結(jié)構(gòu)變量在內(nèi)存中占用一段連續(xù)的區(qū)域,有一個起始地址;所以可以設(shè)計一個指針變量,用于存放結(jié)構(gòu)變量的起始地址;即,指向結(jié)構(gòu)類型數(shù)據(jù)的指針;第二十二頁,共四十一頁,2022年,8月28日指向結(jié)構(gòu)類型數(shù)據(jù)的指針main(){ structstudent{long
num;
char
name[20];
charsex;
floatscore; }stu_1;
struct
student*p;
p=&stu_1;stu_1.num=89101;
strcpy(stu_1.name,"LiLin");
stu_1.sex='M';
stu_1.score=89.5;
cout<<stu_1.num<<stu_1.name<<stu_1.sex<<stu_1.score); cout<<(*p).num<<(*p).name<<(*p).sex<<(*p).score);}第二十三頁,共四十一頁,2022年,8月28日voidmain(){ studentstu_1,*p; int*p1; char*p2,*p3; float*p4; p=&stu_1;
p1=&stu_1.stuNo; p2=stu_1.name; p3=&stu_1.sex; p4=&stu_1.score; stu_1.stuNo=89101; strcpy(stu_1.name,"LiLin"); stu_1.sex='M'; stu_1.score=90; cout<<stu_1.stuNo<<""<<stu_1.name<<""<<stu_1.sex<<""<<stu_1.score<<endl; cout<<(*p).stuNo<<""<<(*p).name<<""<<(*p).sex<<""<<(*p).score<<endl;}structstudent{ intstuNo; charname[20]; charsex; floatscore;};結(jié)構(gòu)與指針第二十四頁,共四十一頁,2022年,8月28日指向運算符在C++語言中,為了使用方便和直觀,可以把(*p).stuNo改用p->stuNo來代替,它表示*p所指向的結(jié)構(gòu)變量中的stuNo成員。以下三種形式等價:①結(jié)構(gòu)體變量.成員名②(*p).成員名③p->成員名其中->稱為指向運算符。第二十五頁,共四十一頁,2022年,8月28日指向結(jié)構(gòu)數(shù)組的指針structstudent{ intnum;
charname[20];
charsex;
intage;};structstudentstu[3]={ {10101,”LiLin”,’M’,18},
{10102,”ZhangFun”,’M’,19},
{10104,”WangMin”,’F’,20}};main(){ structstudent*p;
cout<<“No.”<<“Name”<<”sex”<<”age";
for(p=stu;p<stu+3;p++) cout<<p->num<<
p->name <<p->sex<<
p->age;}運行結(jié)果如下:
No.Name
sexage
10101
LiLin
M
1810102
ZhangFunM
19
10104WangMinF
20第二十六頁,共四十一頁,2022年,8月28日指向結(jié)構(gòu)數(shù)組的指針解釋p用來指向一個structstudent型的數(shù)據(jù),不應用來指向stu數(shù)組元素中的某一成員。
p加1意味著p所增加的值為結(jié)構(gòu)體數(shù)組stu的一個元素所占的字節(jié)數(shù)(本例中為4+20+1+4=29字節(jié))。第二十七頁,共四十一頁,2022年,8月28日指向結(jié)構(gòu)體數(shù)組的指針structtest*p;p=stu;cout<<p++->num<<endl;cout<<++p->num<<endl;cout<<p->num++<<endl;cout<<p->num<<endl;cout<<(++p)->num++<<endl;cout<<p->num<<endl;numname10‘A’20‘B’30‘C’40‘D’注意:->運算的優(yōu)先級非常高,僅次于()[]要把p->num看成是一個變量;++是針對這個變量而言structtest{intnum;charname;}stu[4]={{10,‘A’},{20,‘B’},{30,‘C’},{40,‘D’}};第二十八頁,共四十一頁,2022年,8月28日結(jié)構(gòu)體做函數(shù)參數(shù)structstru{ intx; charc;};voidfunc(strub){ b.x=20; b.c=‘y’;}intmain(){ strua={10,'x'}; func(a); cout<<a.x<<“”<<a.c; return0;}可見:結(jié)構(gòu)體做參數(shù)時采用值傳遞的方式;系統(tǒng)會構(gòu)造一個結(jié)構(gòu)體的副本給函數(shù)使用;第二十九頁,共四十一頁,2022年,8月28日studentGetStudent(){ studentt; cout<<“請輸入學號”; cin>>t.No; cout<<“請輸入學生姓名:”; cin.getline(,20); cout<<“請輸入數(shù)學、英語、C++成績:”; cin>>t.score[0]>>t.score[1]>>t.score[2]; returnt;}intmain(){ studentstu[4]; for(inti=0;i<4;i++) { stu[i]=GetStudent(); print(stu[i]); }}結(jié)構(gòu)體做函數(shù)返回值一個函數(shù)可以返回一個結(jié)構(gòu)體!第三十頁,共四十一頁,2022年,8月28日指向結(jié)構(gòu)體的指針做參數(shù)structstudent{ intNo; charname[20]; floatscore[3];};voidprint(student*p){ cout<<p->No<<p->name<<p->score[0]
<<p->score[1]<<p->score[2];}intmain(){ structstudentstu; cin>>stu.No>>>>stu.score[0] >>stu.score[1]>>stu.score[2]; print(&stu); return0;}例:在主函數(shù)中輸入結(jié)構(gòu)體各成員的值,在子函數(shù)中輸出;(要求:在主函數(shù)中,實參是地址,在子函數(shù)中用指針接收.)第三十一頁,共四十一頁,2022年,8月28日結(jié)構(gòu)體數(shù)組元素做參數(shù)voidmain(){ studentallone[4]= { {1001,“jone”,60,60,80}, {1002,“david”,70,70,90}, {1003,“marit”,80,80,60}, {1004,“yoke”,90,90,70} };for(inti=0;i<4;i++) print(allone[i]);}voidprint(studentp){cout<<p.No << <<p.score[0] <<p.score[1] <<p.score[2] <<endl;}第三十二頁,共四十一頁,2022年,8月28日結(jié)構(gòu)體數(shù)組做參數(shù)voidmain(){studentallone[4]= { {1001,“jone”,60,60,80}, {1002,“david”,70,70,90}, {1003,“marit”,80,80,60}, {1004,“yoke”,90,90,70} }; print(allone,4);}voidprint(student*p,intn){ for(inti=0;i<n;i++,p++) cout<<p->No <<p->name <<p->score[0] <<p->score[1] <<p->score[2] <<endl;}第三十三頁,共四十一頁,2022年,8月28日結(jié)構(gòu)體應用示例-生日相同問題Description在一個有100人的大班級中,存在兩個人生日相同的概率非常大,現(xiàn)給出每個學生的學號,出生月日。試找出所有生日相同的學生。Input第一行為整數(shù)n,表示有n個學生,n<100。此后每行包含一個字符串和兩個整數(shù),分別表示學生的學號(字符串長度小于10)和出生月(1<=m<=12)日(1<=d<=31)。學號、月、日之間用一個空格分隔。Output對每組生日相同的學生,輸出一行,其中前兩個數(shù)字表示月和日,后面跟著所有在當天出生的學生的學號,數(shù)字、學號之間都用一個空格分隔。對所有的輸出,要求按日期從前到后的順序輸出。對生日相同的學號,按輸入的順序輸出。第三十四頁,共四十一頁,2022年,8月28日思路:把所以可能的日期羅列出來用每個學生的生日去匹配每一個日期,如果有匹配,計數(shù)器加1每個日期,有多于兩個匹配上就輸出for(m=1;m<=12;m++)for(d=1;d<=31;d++)flag=0;j=0;for(inti=0;i<n;i++) if(stu[i].month==m&&stu[i].day==d) {count[++j]=i;flag++;}第三十五頁,共四十一頁,2022年,8月28日voidmain(){ inti,j,k,n,flag,count[100]={0}; cout<<"howmanystudents?"; cin>>n; for(inti=0;i<n;i++) cin>>stu[i].ID>>stu[i].month>>stu[i].day; for(intm=1;m<=12;m++) for(intd=1;d<=31;d++) { flag=0;j=0; for(inti=0;i<n;i++) if(stu[i].month==m&&stu[i].day==d) {count[++j]=i;flag++;} if(flag>1) { cout<<m<<""<<d<<""; for(k=1;k<=j;k++) cout<<stu[count[k]].ID<<""<<endl; } }}structstuden
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度高端辦公室文件消毒及深度保養(yǎng)合同
- 租賃期間房屋買賣合同
- 公司之間的借款協(xié)議
- 出租車停運損失上訴狀
- 電器代理合同協(xié)議
- 財務管理系統(tǒng)操作與應用手冊指南
- 農(nóng)業(yè)科技行業(yè)現(xiàn)代農(nóng)業(yè)技術(shù)推廣與應用策略
- 廣告招牌安裝合同年
- 辦公室租賃合同書
- 安全事故賠償協(xié)議書
- 110kV變電站專項電氣試驗及調(diào)試方案
- 2024年廣西桂盛金融信息科技服務有限公司招聘筆試沖刺題(帶答案解析)
- 外賣星級(商家評分)計算表
- DZ∕T 0215-2020 礦產(chǎn)地質(zhì)勘查規(guī)范 煤(正式版)
- 外出檢查病人突發(fā)呼吸心跳驟停應急預案演練
- 《火力發(fā)電廠汽水管道設(shè)計規(guī)范+DLT+5054-2016》詳細解讀
- 幕墻施工成品及半成品保護措施
- 基于單片機的交通燈控制系統(tǒng)設(shè)計畢業(yè)論文
- 2024年執(zhí)業(yè)醫(yī)師考試-醫(yī)師定期考核(口腔)筆試參考題庫含答案
- 中國律師學 課件 陳衛(wèi)東 第10-17章 律師收費制度-律師非訴訟業(yè)務(二)
- 中國移動行測測評題及答案
評論
0/150
提交評論