例7.1 引用結(jié)構(gòu)體變量中的成員。
#include <iostream>
usingnamespacestd;
structDate //聲明結(jié)構(gòu)體類型Date
{intmonth;
intday;
intyear;
};
structStudent //聲明結(jié)構(gòu)體類型Student
{intnum;
charname[20];
charsex;
Date birthday; //聲明birthday為Date類型的成員
floatscore;
}student1,student2={10002,"Wang Li",'f',5,23,1982,89.5};
//定義Student 類型的變量student1,student2,并對student2初始化
intmain( )
{ student1=student2; //將student2各成員的值賦予student1的相應(yīng)成員
cout<<student1.num<<endl; //輸出student1中的num成員的值
cout<<student1.name<<endl; //輸出student1中的name成員的值
cout<<student1.sex<<endl; //輸出student1中的sex成員的值
cout<<student1.birthday.month<<'/'<<student1.birthday.day<<'/'
<<student1.birthday.year<<endl; //輸出student1中的birthday各成員的值
cout<<student1.score<<endl;
return0;
}
例7.2 對候選人得票的統(tǒng)計(jì)程序。設(shè)有3個(gè)候選人,最終只能有1人當(dāng)選為領(lǐng)導(dǎo)。今有10個(gè)人參加投票,從鍵盤先后輸入這10個(gè)人所投的候選人的名字,要求最后輸出這3個(gè)候選人的得票結(jié)果。
可以定義一個(gè)候選人結(jié)構(gòu)體數(shù)組,包括3個(gè)元素,在每個(gè)元素中存放有關(guān)的數(shù)據(jù)。
程序如下:
#include <iostream>
structPerson //聲明結(jié)構(gòu)體類型Person
{ charname[20];
intcount;
};
intmain( )
{ Person leader[3]={"Li",0,"Zhang",0,"Fun",0};
//定義Person類型的數(shù)組,內(nèi)容為3個(gè)候選人的姓名和當(dāng)前的得票數(shù)
inti,j;
charleader_name[20]; //leader_name為投票人所選的人的姓名
for(i=0;i<10;i++)
{cin>>leader_name; //先后輸入10張票上所寫的姓名
for(j=0;j<3;j++) //將票上姓名與3個(gè)候選人的姓名比較
if(strcmp(leader_name,leader[j].name)==0) leader[j].count++;
//如果與某一候選人的姓名相同,就給他加一票
}
cout<<endl;
for(i=0;i<3;i++) //輸出3個(gè)候選人的姓名與最后得票數(shù)
{cout<<leader[i].name<<":"<<leader[i].count<<endl;}
return0;
}
例7.3 指向結(jié)構(gòu)體變量的指針的應(yīng)用。
#include <iostream>
#include <string>
usingnamespacestd;
intmain( )
{structStudent //聲明結(jié)構(gòu)體類型student
{ intnum;
string name;
charsex;
floatscore;
};
Student stu; //定義Student類型的變量stu
Student *p=&stu; //定義p為指向Student類型數(shù)據(jù)的指針變量并指向stu
stu.num=10301; //對stu中的成員賦值
stu.name="Wang Fun"; //對string變量可以直接賦值
stu.sex='f';
stu.score=89.5;
cot<<stu. num<<" "<<stu.name<<" "<<stu.sex<<" "<<stu.score<<endl;
cout<<(*p)>num<<" "<<(*p).name<<" "<<(*p).sex<<" "<<(*p).score<<endl;
return0;
}
例7.4 建立一個(gè)如圖7.9所示的簡單鏈表,它由3個(gè)學(xué)生數(shù)據(jù)的結(jié)點(diǎn)組成。輸出各結(jié)點(diǎn)中的數(shù)據(jù)。
#define NULL 0
#include <iostream>
structStudent
{ longnum;
floatscore;
structStudent *next;
};
intmain( )
{ Student a,b,c,*head,*p;
a. num=31001; a.score=89.5; //對結(jié)點(diǎn)a的num和score成員賦值
b. num=31003; b.score=90; //對結(jié)點(diǎn)b的num和score成員賦值
c. num=31007; c.score=85; //對結(jié)點(diǎn)c的num和score成員賦值
head=&a; //將結(jié)點(diǎn)a的起始地址賦給頭指針head
a.next=&b; //將結(jié)點(diǎn)b的起始地址賦給a結(jié)點(diǎn)的next成員
b.next=&c; //將結(jié)點(diǎn)c的起始地址賦給b結(jié)點(diǎn)的next成員
c.next=NULL; //結(jié)點(diǎn)的next成員不存放其他結(jié)點(diǎn)地址
p=head; //使p指針指向a結(jié)點(diǎn)
do
{cout<<p->num<<" "<<p->score<<endl; //輸出p指向的結(jié)點(diǎn)的數(shù)據(jù)
p=p->next; //使p指向下一個(gè)結(jié)點(diǎn)
} while(p!=NULL); //輸出完c結(jié)點(diǎn)后p的值為NULL
return0;
}
例7.5 有一個(gè)結(jié)構(gòu)體變量stu,內(nèi)含學(xué)生學(xué)號、姓名和3門課的成績。要求在main函數(shù)中為各成員賦值,在另一函數(shù)print中將它們的值輸出。
(1) 用結(jié)構(gòu)體變量作函數(shù)參數(shù)
#include <iostream>
#include <string>
usingnamespacestd;
structStudent //聲明結(jié)構(gòu)體類型Student
{ intnum;
charname[20];
floatscore[3];
};
intmain( )
{ voidprint(Student); //函數(shù)聲明,形參類型為結(jié)構(gòu)體Student
Student stu; //定義結(jié)構(gòu)體變量
stu.num=12345; //以下5行對結(jié)構(gòu)體變量各成員賦值
stu.name="Li Fung";
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.5;
print(stu); //調(diào)用print函數(shù),輸出stu各成員的值
return0;
}
voidprint(Student stu)
{cout<<stu.num<<" "<<stu.name<<" "<<stu.score[0]<<" "
<<stu.score[1]<<" "<<stu.score[2]<<endl;
}
(2) 用指向結(jié)構(gòu)體變量的指針作實(shí)參
#include <iostream>
#include <string>
usingnamespacestd;
structStudent
{ intnum;
string name; //用string類型定義字符串變量
floatscore[3];
}stu={12345,"Li Fung",67.5,89,78.5}; //定義結(jié)構(gòu)體student變量stu并賦初值
intmain( )
{ voidprint(Student *); //函數(shù)聲明,形參為指向Student類型數(shù)據(jù)的指針變量
Student *pt=&stu; //定義基類型為Student的指針變量pt,并指向stu
print(pt); //實(shí)參為指向Student類數(shù)據(jù)的指針變量
return0;
}
voidprint(Student *p) //定義函數(shù),形參p是基類型為Student的指針變量
{ cout<<p->num<<" "<<p->name<<" "<<p->score[0]<<" "
<<p->score[1]<<" "<<p->score[2]<<endl;
}
(3) 用結(jié)構(gòu)體變量的引用作函數(shù)參數(shù)
#include <iostream>
#include <string>
usingnamespacestd;
structStudent
{ intnum;
string name;
floatscore[3];
}stu={12345,"Li Li",67.5,89,78.5};
intmain( )
{ voidprint(Student &); //函數(shù)聲明,形參為Student類型變量的引用
print(stu); //實(shí)參為結(jié)構(gòu)體Student變量
return0;
}
voidprint(Student &stud) //函數(shù)定義,形參為結(jié)構(gòu)體Student變量的引用
{cout<<stud.num<<" "<<stud.name<<" "<<stud.score[0]<<" "
<<stud.score[1]<<" "<<stud.score[2]<<endl;
}
例7.6 開辟空間以存放一個(gè)結(jié)構(gòu)體變量。
#include <iostream>
#include <string>
usingnamespacestd;
structStudent //聲明結(jié)構(gòu)體類型Student
{ string name;
intnum;
charsex;
};
intmain( )
{ Student *p; //定義指向結(jié)構(gòu)體類型Student的數(shù)據(jù)的指針變量
p=newStudent; //用new運(yùn)算符開辟一個(gè)存放Student型數(shù)據(jù)的空間
p->name="Wang Fun"; //向結(jié)構(gòu)體變量的成員賦值
p->num=10123;
p->sex='m';
cout<<p->name<<endl<<p->num<<endl<<p->sex<<endl; //輸出各成員的值
deletep; //撤銷該空間
return0;
}
例7.7 設(shè)有若干個(gè)人員的數(shù)據(jù),其中有學(xué)生和教師。學(xué)生的數(shù)據(jù)中包括:姓名、號碼、性別、職業(yè)、年級。教師的數(shù)據(jù)包括:姓名、號碼、性別、職業(yè)、職務(wù)。可以看出,學(xué)生和教師所包含的數(shù)據(jù)是不同的?,F(xiàn)要求把它們放在同一表格中,見圖7.13。
如果job項(xiàng)為s(學(xué)生),則第5項(xiàng)為grade(年級)。即Li是3年級的。如果job項(xiàng)是t(教師),則第5項(xiàng)為position(職務(wù))。Wang是prof(教授)。顯然對第5項(xiàng)可以用共用體來處理(將class和position放在同一段內(nèi)存中)。
要求輸入人員的數(shù)據(jù),然后再輸出。為簡化起見,只設(shè)兩個(gè)人(一個(gè)學(xué)生、一個(gè)教師)。
程序如下:
#include <iostream>
#include <string>
#include <iomanip> //因?yàn)樵谳敵隽髦惺褂昧丝刂品鹲etw
usingnamespacestd;
struct
{ intnum;
charname[10];
charsex;
charjob;
unionP //聲明共用體類型
{ intgrade; //年級
charposition[10]; //職務(wù)
}category; //成員category 為共用體變量
}person[2]; //定義共用體數(shù)組person,含兩個(gè)元素
intmain( )
{ inti;
for(i=0;i<2;i++) //輸入兩個(gè)學(xué)生的數(shù)據(jù)
{cin>>person[i].num>>person[i].name>>person[i].sex>>person[i].job;
if(person[i].job=='s') cin>>person[i].category.grade; //若是學(xué)生則輸入年級
elseif(person[i].job=='t') cin>>person[i].category.position;//若是教師則輸入職務(wù)
}
cout<<endl<<"No. Name sex job grade/position"<<endl;
for(i=0;i<2;i++)
{if(person[i].job=='s')
cout<<person[i].num<<setw(6)<<person[i].name<<" "<<person[i].sex
<<" "<<person[i].job<<setw(10)<<person[i].category.grade<<endl;
else
cout<<person[i].num<<setw(6)<<person[i].name<<" "<<person[i].sex
<<" "<<person[i].job<<setw(10)<<person[i].category.position<<endl;
}
return0;
}
例7.8 口袋中有紅、黃、藍(lán)、白、黑5種顏色的球若干個(gè)。每次從口袋中任意取出3個(gè)球,問得到3種不同顏色的球的可能取法,輸出每種排列的情況。
#include <iostream>
#include <iomanip> //在輸出時(shí)要用到setw控制符
usingnamespacestd;
intmain( )
{ enumcolor {red,yellow,blue,white,black}; //聲明枚舉類型color
color pri; //定義color類型的變量pri
inti,j,k,n=0,loop; //n是累計(jì)不同顏色的組合數(shù)
for(i=red;i<=black;i++) //當(dāng)i為某一顏色時(shí)
for(j=red;j<=black;j++) //當(dāng)j為某一顏色時(shí)
if(i!=j) //若前兩個(gè)球的顏色不同
{ for(k=red;k<=black;k++) //只有前兩個(gè)球的顏色不同,才需要檢查第3個(gè)球的顏色
if((k!=i) && (k!=j)) //3個(gè)球的顏色都不同
{n=n+1; //使累計(jì)值n加1
cout<<setw(3)<<n; //輸出當(dāng)前的n值,字段寬度為3
for(loop=1;loop<=3;loop++) //先后對3個(gè)球作處理
{switch(loop) //loop的值先后為1,2,3
{case1: pri=color(i);break; //color(i)是強(qiáng)制類型轉(zhuǎn)換,使pri的值為i
case2: pri=color(j);break; //使pri的值為j
case3: pri=color(k);break; //使pri的值為k
default:break;
}
switch(pri) //判斷pri的值,輸出相應(yīng)的“顏色”
{casered: cout<<setw(8)<<"red"; break;
caseyellow: cout<<setw(8)<<"yellow"; break;
caseblue: cout<<setw(8)<<"blue"; break;
casewhite: cout<<setw(8)<<"white"; break;
caseblack: cout<<setw(8)<<"black"; break;
default : break;
}
}
cout<<endl;
}
}
cout<<"total:"<<n<<endl; //輸出符合條件的組合的個(gè)數(shù)
return0;
}
