c++課件第七章自定義數(shù)據(jù)類型_第1頁
c++課件第七章自定義數(shù)據(jù)類型_第2頁
c++課件第七章自定義數(shù)據(jù)類型_第3頁
c++課件第七章自定義數(shù)據(jù)類型_第4頁
c++課件第七章自定義數(shù)據(jù)類型_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、第七章 自定義數(shù)據(jù)類型7.1 結(jié)構(gòu)體類型7.1.1 結(jié)構(gòu)體的概述一個學生的學號、姓名、性別、年齡、成績、家庭住址 num name sex age score addr 10010 Li Fun M 18 87.5 BeiJing聲明一個新的結(jié)構(gòu)體的類型:struct Studentint num;char name20;char sex;int age;float score;char addr30;7.1.2 結(jié)構(gòu)體類型變量的定義方法及其初始化1. 定義結(jié)構(gòu)體變量的方法(1) 先聲明結(jié)構(gòu)體的類型再定義變量名Student student1,student2;(2) 聲明類型的同時定義變量s

2、truct Studentint num;char name20;char sex;int age;float score;char addr30;std1,std2;(3) 直接定義結(jié)構(gòu)體類型變量struct int num;char name20;char sex;int age;float score;char addr30;std1,std2;(4) 成員也可以是一個結(jié)構(gòu)體變量struct Dateint month;int day;int year;struct Studentint num;char name20;char sex;int age;Date birthday;flo

3、at score;char addr30;2. 結(jié)構(gòu)體變量的初始化struct Studentint num;char name20;char sex;int age;float score;char addr30;stdent=10001,Zhang Xin,M,19,90.5,shanghai;Student student2=10002,Wang Li,F,20,98,Beijing;7.1.3 結(jié)構(gòu)體變量的引用(1) 可以將一個結(jié)構(gòu)體變量的值賦給另一個具有相同結(jié)構(gòu)的結(jié)構(gòu)體變量。student1=student2;(2) 可以引用一個結(jié)構(gòu)體變量中的一個成員的值。student1.num=

4、10010; .是成員運算符,它的優(yōu)先級最高。(3) 對于結(jié)構(gòu)體嵌套,要逐級引用。student1.birthday.month=11;(4) 不能將一個結(jié)構(gòu)體變量作為一個整體進行輸入和輸出。(5) 對于結(jié)構(gòu)體變量的成員可以像普通變量一樣進行各種運算。(6) 可以引用結(jié)構(gòu)體變量成員的地址,也可以引用結(jié)構(gòu)體變量的地址。cout&student1;cout&student1.age;例7.1 引用結(jié)構(gòu)體變量中的成員#include using namespace std;struct Dateint month;int day;int year;struct Studentint num;char

5、 name20;char sex;Date birthday;float score;char addr30;student1,student2=10002,Wang Li,F,5,23,1982,89.5;void main()student1=student2;coutstudent1.numendl;endl;coutstudent1.sexendl;coutstudent1.birthday.month/student1.birthday.day/student1.birthday.yearendl;coutstudent1.scoreendl;100

6、02Wang LiF5/23/1982 結(jié)構(gòu)體數(shù)組1. 定義結(jié)構(gòu)體數(shù)組struct Studentint num;char name20;char sex;int age;float score;char addr30;stu3;Student x8;2. 結(jié)構(gòu)體數(shù)組的初始化Student y2=10101,Li Lin,M,18,87.5,103 Beijing Road ,10102,Zhang Fun,M19,99,130 Shanghai Road;3. 結(jié)構(gòu)體數(shù)組應用舉例例7.2 對候選人得票統(tǒng)計程序.#include using namespace std;str

7、uct Personchar name20;int count;void main()Person leader3=Li,0,Zhang,0,Fun,0;int i,j;char leader_name20;for(i=0;ileader_name;for(j=0;j3;j+)if(strcmp(leader_name,)=0)leaderj.count+;break;for(i=0;i3;i+):leaderi.countendl;ZhangLiFunLiZhangLiZhangLiFunWangLi:4Zhang:3Fun:27.1.

8、5 指向結(jié)構(gòu)體變量的指針1. 通過指向結(jié)構(gòu)體變量的指針引用結(jié)構(gòu)體變量中的成員例7.3 指向結(jié)構(gòu)體變量的指針的應用#include #include using namespace std;void main()struct Studentint num;string name;char sex;float score;Student stu;Student *p=&stu;stu.num=10301;=Wang Fun;stu.sex=F;stu.score=89.5;coutstu.num stu.sex stu.scoreendl;cout(*p).num

9、 (*p).name (*p).sex (*p).scoreendl;coutnum name sex score是指向運算符,即指向結(jié)構(gòu)體變量運算符。請分析以下幾種運算:p-np-n+p-n#include #include using namespace std;void main()struct Studentint num;string name;char sex;float score;Student stu;Student *p=&stu;stu.num=10301;coutnumendl;coutnum+endl;coutnumendl;coutstu.numendl;10301

10、1030110303103032. 用結(jié)構(gòu)體變量和指向結(jié)構(gòu)體變量的指針構(gòu)成鏈表struct Studentint num;float score;Student *next;利用指向自己的指針構(gòu)成鏈表。#define NULL 0#include using namespace std;struct Studentint num;float score;Student *next;void main()Student a,b,c,*head,*p;a.num=31001;a.score=89.5;b.num=31003;b.score=90;c.num=31007;c.score=85;hea

11、d=&a;a.next=&b;b.next=&c;c.next=NULL;p=head;while(p!=NULL)coutnum scorenext;31001 89.531003 9031007 857.1.6 結(jié)構(gòu)體類型數(shù)據(jù)作為函數(shù)參數(shù)(1) 用結(jié)構(gòu)體變量作參數(shù)(傳值)。#include #include using namespace std;struct Studentint num;char name20;float score3;void main()void print(Student);Student stu;stu.num=12345;strcpy(,Li

12、Feng);stu.score0=67.5;stu.score1=89;stu.score2=78.5;print(stu);void print(Student stu)coutstu.num stu.score0 stu.score1 stu.score2endl;12345 Li Feng 67.5 89 78.5(2) 用指向結(jié)構(gòu)體變量的指針做參數(shù)(傳地址)。#include #include using namespace std;struct Studentint num;char name20;float score3;void main()void print

13、(Student*);Student stu=12345,Li Feng,67.5,89,78.5;print(&stu);void print(Student *p)coutnum name score0 score1 score2endl;12345 Li Feng 67.5 89 78.5(3) 用結(jié)構(gòu)體變量的引用做參數(shù)(傳引用)。#include #include using namespace std;struct Studentint num;string name;float score3;void main()void print(const Student &);Studen

14、t stu;stu.num=12345;=Li Feng;stu.score0=67.5;stu.score1=89;stu.score2=78.5;print(stu);void print(const Student &stu)coutstu.num stu.score0 stu.score1 stu.score2endl;12345 Li Feng 67.5 89 動態(tài)分配和撤銷內(nèi)存的運算符new和deleteint *p=new int;delete p;int *p=new int(100);delete p;char *pc=n

15、ew char10;delete pc;/刪除數(shù)組int (*pp)4=new int54;deletepp;/刪除數(shù)組例7.6 開辟空間存放一個結(jié)構(gòu)體變量。#include using namespace std;struct Studentint num;string name;char sex;void main()Student *p;p=new Student;p-num=10123;p-name=Wang Fun;p-sex=M;coutname num sexendl;delete p;Wang Fun 10123 M new和delete運算符與含有指向自己的指針的結(jié)構(gòu)體,就可

16、以實現(xiàn)動態(tài)鏈表,在數(shù)據(jù)結(jié)構(gòu)課中要詳細介紹。7.2 共用體7.2.1 共用體的概念幾個不同的變量共占同一段內(nèi)存的結(jié)構(gòu),稱為共用體(有的書稱之為聯(lián)合)。7.2.2 對共用體變量的訪問方式【例】演示聯(lián)合的例子。這里exam是個聯(lián)合,為說明數(shù)據(jù)成員val和h開始于同一地址,考慮下面的程序:#include #includeunion examshort val; char h2;void main()exam var;var.h0= A;coutThe value of var.val:var.valendl;coutThe value of var.h0:var.h0endl;var.val=66;

17、var.h1=A;coutvar.valendl;coutvar.h0endl;coutvar.h1endl;/65*256+66=16706The value of var.val:65The value of var.h0:A16706BA7.2.3 共用體類型數(shù)據(jù)的特點(1) 每一瞬時只有一個成員起作用,其它成員不起作用。(2) 共用體變量的地址和它的各個成員的地址都是同一地址。(3) 不能對共用體變量賦值;不能企圖引用變量名來得到一個值;不能在定義共用體變量時對它進行初始化;不能用共用體變量作為函數(shù)參數(shù)。例7.7 name num sex job grade/positionLi 10

18、11 f s 3Wang 2085 m t prof#include #include #includeusing namespace std;struct int num;char name10;char sex;char job;union Pint grade;char position10;category;person2;void main()int i;for(i=0;personi.sexpersoni.job;if(personi.job=s) cinpersoni.category.grade;else cinpersoni.c

19、ategory.position;coutNo. Name sex job grade/positionendl;for(i=0;i2;i+)coutpersoni.numsetw(6) personi.sex personi.job;if(personi.job=s) coutsetw(10)personi.category.grade;else coutsetw(10)personi.category.position;coutendl;101 Li f s 3102 Wang m t profNo. Name sex job grade/position101 L

20、i f s 3102 Wang m t prof7.3 枚舉類型如果一個變量只有幾中可能的值,可以定義為枚舉類型 enum colorRED,BLUE,GREEN;關(guān)鍵字enum標志枚舉類型定義開始,分號標志其結(jié)束。在C+中允許不寫enum;enum后的標識符color稱為枚舉類型名。枚舉元素(枚舉常量):花括號內(nèi)用逗號隔開的標識符是為這個類型定義的常量,枚舉元素的缺省賦值:編譯器為這些枚舉常量賦予不同的值,第一個常量RED值為0,以后的常量的值是它前面常量的值增1,所以,BLUE為1,GREEN為2 。聲明枚舉變量:枚舉標記可以用類型名說明具有該類型的變量,例如: color CorVari

21、able;上面的語句說明了變量CorVariable,可以將說明類型color時所列舉的枚舉常量中的任何一個置給變量CorVariable , 例如: CorVariable=GREEN; coutCorVariableendl;#includevoid main()enum colorRED,BLUE,GREEN;color CorVariable;CorVariable=GREEN;coutCorVariableendl;coutsizeof(GREEN)endl;/CorVariable=1;/這樣不行CorVariable=(color)1;24枚舉常量的自定義賦值:說明可在枚舉常量名

22、之后使用等號將一個(char或int類型的)常量置給一個枚舉常量名,以改變編譯的缺省賦值,例如: enum color RED=-1,BLUE,GREEN=6,YELLOW;這里RED代表值1,而BLUE為它前 面的枚舉常量的值加1,所以BLUE代表值0。同樣,GREEN的值為6,而YELLOW的值為7。為枚舉常量指定重復的值也是合法的,例如: enum state FALSE,TRUE,FAIL=0,BAD=0;在這個說明中FALSE,FAIL和BAD的值都為0。枚舉常量的類型:枚舉常量的類型隱含是unsigned char 或int類型 ,但到底是何種類型取決于枚舉常量的值。如果所有的值都

23、可用unsigned char表示,則它就是每個枚舉常量的類型。例7.8 口袋中有紅、黃、藍、白、黑5種顏色的球若干個。每次從口袋中任意取出3個球,問得到3種不同顏色球的可能取法,輸出每種排列的情況。#include#includeusing namespace std;enum colorred,yellow,blue,white,black;void main()void print(color);color pri;int i,j,k,n=0;for(i=red;i=black;i+)for(j=red;j=black;j+)if(i!=j)for(k=red;k=black;k+)if

24、(k!=i)&(k!=j)coutsetw(3)+n;pri=color(i);print(pri);pri=color(j);print(pri);pri=color(k);print(pri);coutendl;couttotal:nendl;void print(color co)coutsetw(8);switch(co)case red:coutred;break;case yellow:coutyellow;break;case blue:coutblue;break;case white:coutwhite;break;case black:coutblack;break;default:break; 38 white red blue 39 white red black 40 white yellow red 41 white yellow blue

溫馨提示

  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論