C++課程實(shí)習(xí)及答案_第1頁(yè)
C++課程實(shí)習(xí)及答案_第2頁(yè)
C++課程實(shí)習(xí)及答案_第3頁(yè)
C++課程實(shí)習(xí)及答案_第4頁(yè)
C++課程實(shí)習(xí)及答案_第5頁(yè)
已閱讀5頁(yè),還剩66頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

實(shí)驗(yàn)題目(共4題,第1題)

標(biāo)題:1、字符串輸入輸出

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:編寫一個(gè)簡(jiǎn)單的控制臺(tái)應(yīng)用程序,先輸入姓名,如“John”,再輸出問(wèn)候語(yǔ),如

“Hello,John!”。

輸入:John

輸出:Hello,John!

輸入樣例:John

輸出樣例:Hello,John!

提示:1、使用string類定義字符串對(duì)象,需包含頭文件〈string〉;

2、使用cin和提取符>>從鍵盤輸入數(shù)據(jù),使用cout和插入符<<輸出結(jié)果到屏幕,需包含頭

文件<iostream>;

3、注意使用名稱空間std。

來(lái)源:

#include<iostream>

#include<stdio.h>

#include<stdlib.h>

usingnamespacestd;

intmain()

(

chars[10];

gets(s);

cout?uHello,"?s?"!"<<endl;

return0;

)

示例代碼--------------------------------

#include<iostream>

#include<string>

usingnamespacestd;

intmain()

stringszName;

cin?szName;

cout?"Hello,n?szName?"!”<<endl;

return0;

)

實(shí)驗(yàn)題目(共4題,第2題)

標(biāo)題:2、求3個(gè)數(shù)的平均值

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:從鍵盤上輸入3個(gè)浮點(diǎn)數(shù),求這3個(gè)數(shù)的平均值。

輸入:3個(gè)浮點(diǎn)數(shù)

輸出:3個(gè)數(shù)的平均值

輸入樣例:

輸出樣例:1.46667

提示:1、用usingnamespacestd;明確名字空間

2、用cin對(duì)象,采用>>運(yùn)算符輸入數(shù)據(jù)

3、用cout對(duì)象,采用<<運(yùn)算符輸出數(shù)據(jù)

來(lái)源:

#include<iostream>

usingnamespacestd;

intmain()

{

floata,b,c,aver=0;

cin?a;

cin?b;

cin?c;

aver=(a+b+c)/3.0;

cout?aver?endl;

return0;

)

示例代碼------------------------

#include<iostream>

usingnamespacestd;

intmain()

(

floatxl,x2,x3;

cin?x1?x2?x3;

cout?(x1+x2+x3)/3?endl;

return0;

)

實(shí)驗(yàn)題目(共4題,第3題)

標(biāo)題:3、求鞍點(diǎn)

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:輸入一個(gè)二維矩陣,找出其中所有鞍點(diǎn).如果矩陣有鞍點(diǎn),則輸出鞍點(diǎn)的信息:行

號(hào)、列號(hào)、值;

如果沒(méi)有鞍點(diǎn),則輸出"Notfound!”。

所謂“鞍點(diǎn)”,是指滿足以下條件的矩陣中的一個(gè)數(shù):在它所在的行上最小、所在列上最大。

該題中假設(shè)矩陣中任意兩個(gè)數(shù)互不相等。

輸入:輸入數(shù)據(jù)有多行:

第一行是矩陣的行數(shù)m和列數(shù)n

從第二行起共包含m行,每行包含n個(gè)數(shù),為矩陣的一行數(shù)據(jù)

輸出:如果矩陣有鞍點(diǎn),輸出鞍點(diǎn)的信息,包括:所在行、所在列、值

如果沒(méi)有鞍點(diǎn),輸出Notfound!

輸入樣例:34

11235647

12456690

16773418

輸出樣例:2016

提示:1、要求用動(dòng)態(tài)內(nèi)存分配來(lái)完成,可用new和delete實(shí)現(xiàn);

2、屏幕輸出只有2016(加回車換行),不能有其它信息。

來(lái)源:

#include<iostream>

usingnamespacestd;

voidan(inth,int1)

intma,mi,a=O,b=O;

int**p;

p=newint*|h];

for(inti=0;i<h;i++)

(

p[i]=newint[l];

)

for(inti=0;i<h;i++)

(

for(intj=0;j<l;j++)

(

cin?pli][j];

)

)

intflag=O;

for(inti=0;i<h&&flag==O;i++)

(

mi=p[i][O];

a=i,b=O;

for(intj=0;j<l;j++)

(

if(plij[j]<mi)

(

mi=p[ij[j];

a=i,b=j;

)

)

for(intt=i+l;t<h;t++)

{

ma=mi;

if(ma<p[t][b])ma=p[t][b];

)

if(ma==mi)

{

flag=1,cout?a?""?b?""?ma?endl;

)

I

if(flag==O)

cout?"Notfound!"?endl;

delete[]p;

intmain()

inth,i;

cin?h?l;

an(h,l);

return0;

)

示例代碼

#include<iostream>

usingnamespacestd;

intmain()

(

int**mat;

int*matRow,*matCol;

intnMaxRow=0,nMaxCol=0;

boolbFind=false;

intnTargetRow=0,nTargetCol=0,nSaddlePoint=0;

inti,j;

//cout?*'Pleaseinputthenumberofrowsandthenumberofcolumns:"?endl;

cin?nMaxRow?nMaxCol;

//Allocatememoriesforthetwodimensionalmatrices

mat=newint*[nMaxRow];

for(i=0;i<nMaxRow;i++)

mat[i]=newint[nMaxCol];

matRow=newint[nMaxRow];//Bufferstosavetheminimumelementineachrow

matCol=newint[nMaxCol];//Bufferstosavethemaximumelementineachcolumn

//Inputtheelements

//cout?"Pleaseinputtheelements:"?endl;

for(i=0;i<nMaxRow;i++)

for(j=0;j<nMaxCol;j++)

cin?mat[i][j];

//Findtheminimumelementineachrow

for(i=0;i<nMaxRow;i++)

(

intnMin=mat[i][0];

for(j=l;j<nMaxCol;j++)

if(mat[i][j]<nMin)nMin=mat[ij[j];

matRow[i]=nMin;

//Findthemaximumelementineachcolumn

for(j=0;j<nMaxCol;j4-+)

(

intnMax=mat[O][j];

for(i=1;i<nMaxRow;i++)

(

if(mat[i][j]>nMax)nMax=mat[i][j];

)

matColfj]=nMax;

)

//Findthesaddlepoint

for(i=0;i<nMaxRow&&JbFind;i++)

(

for(j=0;j<nMaxCol;j++)

(

if(mat[i][j]==matRow[i]&&mat[i][j]==matCol[j])

(

nTargetRow=i;

nTargetCol=j;

nSaddlePoint=mat[i][j];

bFind=true;

break;

}

)

)

//Outputthesearchedrowandcolumnandthecorrespondingsaddlepoint

if(!bFind)

cout?nNotfound!',?endl;

else

cout?nTargetRow?11"?nTargetCol?n"?nSaddlePoint?endl;

//Releasememories

delete[JmatCol;

delete[JmatRow;

for(i=0;i<nMaxRow;i++)

delete[]mat[i];

delete[]mat;

return0;

實(shí)驗(yàn)題目(共4題,第4題)

標(biāo)題:4、鏈表操作

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:建立一個(gè)鏈表,每個(gè)節(jié)點(diǎn)包括學(xué)生的學(xué)號(hào)、姓名、性別、年齡。先輸入5個(gè)學(xué)

生的數(shù)據(jù),再輸入一個(gè)年齡,如果鏈表中有年齡等于此年齡的記錄,則刪除所有年齡等于此

年齡的記錄,否則在鏈表的最后增加一個(gè)新節(jié)點(diǎn),學(xué)號(hào)為180姓名為“aaa“,性別為“male”。。

輸入:創(chuàng)建鏈表時(shí)輸入5個(gè)職工的職工號(hào)和工資,學(xué)號(hào)為大于100且小于200的整數(shù),

姓名為長(zhǎng)度小于20的字符串,性別為長(zhǎng)度小于10的字符串,年齡為大于等于0且小于200

的整數(shù)。

輸出:按順序輸出鏈表中的所有數(shù)據(jù),每個(gè)數(shù)據(jù)占一行。

輸入樣例:101zhangsanmale30

103lisifemale18

105wangwumale25

107maliumale28

109niuqifemale22

28

輸出樣例:101

zhangsan

male

30

103

lisi

female

18

105

wangwu

male

25

109

niuqi

female

22

提示:要求用動(dòng)態(tài)內(nèi)存分配實(shí)現(xiàn),注意new和delete的使用。

來(lái)源:

#include<iostream>

usingnamespacestd;

classnode

private:

int_num,_age;

string_name,_sexy;

public:

node(intnum,intage,stringname,stringsexy)

(

_num=num;

_age=age;

_name=name;

_sexy=sexy;

)

voiddisplay(void)

{

cout?_num?endl;

cout?_name?endl;

cout?_sexy?endl;

cout?_age?endl;

)

intsame(intage)

{

if(age==_age)return1;

return0;

)

);

classLinkedList

!

public:

node*Node;

LinkedList"next;

LinkedList(intnum,intage,stringname,stringsexy)

(

Node=newnode(num,age,name,sexy);

next=NULL;

)

);

intmain(void)

(

intnum,age;

stringname,sexy;

cin?num?name?sexy?age;

LinkedListhead(num,age,name,sexy);

LinkedList*p=&head;

for(inti=l;i<=4;i++)

(

cin?num?name?sexy?age;

p->next=newLinkedList(num,age,name,sexy);

p=p->next;

)

cin?age;

LinkedList*pp=&head;

intflag=O;

while(pp!=NULL)

(

if(pp->Nocle->same(age))flag++;

elsepp->Node->display();

pp=pp->next;

)

if(!flag)

(

noden(180,age,"aaa"Jmale”);

n.display();

)

return0;

)

示例代碼-----------------------------------------------------------------------

#include<iostream>

#include<cstring>

usingnamespacestd;

constintMAX_STR_LEN=32;

structStuNode

(

intID;

charname[MAX_STR_LEN];

chargender[MAX_STR_LENJ;

intage;

structStuNode*next;

);

typedefstructStuNode*StuLink;

voidCreateHeadNode(StuLink&pHead,StuLink&pTail)

pHead=newStuNode;

if(pHead==NULL)return;

pHead->next=NULL;

pTail=pHead;

)

voidAddStudent(StuLink&pTail,StuNodedata)

(

StuLinkp;

p=newStuNode;

if(p==NULL)return;

p->ID=data.ID;

strcpy(p->name,);

strcpy(p->gender,data.gender);

p->age=data.age;

p->next=NULL;

//Makethecurrentnodepointerpointtothelastaddeddata

pTail->next=p;

pTail=p;

)

boolRemoveStudent(StuLink&pHead,StuLink&pTail,intage)

(

boolbFind=false;

StuLinkpCurr,pPre;

pPre=pHead;

//Skiptothesecondnodebecausethefirstnodeistheheadnodewithoutdata

pCurr=pHead->next;

while(pCurr!=NULL)

(

if(pCurr->age==age)

(

pPre->next=pCurr->next;

deletepCurr;

//Recoverthecurrentpointertofindthenextnodethathasthesamekey

pCurr=pPre->next;

bFind=true;

else

//Movetothenextnode

pPre=pCurr;

pCurr=pCurr->next;

//Recoverthetailpointerincasethelastnodeisdeleted

if(bFind&&pPre->next==NULL)pTail=pPre;

returnbFind;

)

voidOutputAllStudentsInfo(constStuLinkpHead)

(

StuLinkp;

//Skiptothesecondnodebecausethefirstnodeistheheadnodewithoutdata

p=pHead->next;

if(p==NULL)return;

while(p!=NULL)

(

cout?p->ID?endl;

cout?p->name?endl;

cout?p->gender?endl;

cout?p->age?endl;

p=p->next;

)

)

voidInputStudentInfo(StuNode&Emp)

(

cin?Emp.ID?E?Emp.gender?Emp.age;

while(Emp.ID>=200||Emp.ID<100||

strlen(E)>=20||

strlen(Emp.gender)>=10||

Emp.age<0||Emp.age>=200)

cin?Emp.ID?E?Emp.gender?Emp.age;

intmain()

(

StuLinkEmpInslHead,EmpInstTail;

StuNodeEmpTemp;

inti,age;

//Createaheadnodeforthelinkedlist

CreateHeadNode(EmpInstHead,EmpInstTail);

for(i=0;i<5;i++)

(

InputStudentlnfo(EmpTemp);

AddStudent(EmpInstTail,EmpTemp);

)

cin?age;

//Removeaheadnodefromthelinkedlist

if(!RemoveStudent(EmpInstHead,EmpInstTail,age))

(

EmpTemp.age=age;

EmpTemp.ID=180;strcpy(EmpT,"aaa");strcpy(EmpTemp.gender,"male'1);

EmpTemp.next=NULL;

AddStudent(EmpInstTail,EmpTemp);

)

OutputAIIStudentsInfo(EmpInstHead);

return0;

)

實(shí)驗(yàn)題目(共4題,第1題)

標(biāo)題:1.函數(shù)重載

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:設(shè)計(jì)一菜單程序,利用函數(shù)重載實(shí)現(xiàn)員工月工資的計(jì)算,計(jì)算方法如下:

(1)管理人員的月工資=月薪一缺勤天數(shù)X月薪+22;

(2)銷售人員的月工資=底薪+銷售金額X提成比例;

(3)計(jì)件工人的月工資=產(chǎn)品件數(shù)X每件報(bào)酬;

(4)計(jì)時(shí)工人的月工資=工作小時(shí)X小時(shí)報(bào)酬;

輸入:職工類別及相關(guān)信息。

職工類別:1表示管理人員;2表示銷售人員;3表示計(jì)件工人;4表示計(jì)時(shí)工人;其余字符

表示退出。

相關(guān)信息:若為管理人員,則輸入月薪和缺勤天數(shù);若為銷售人員,則輸入底薪、銷售金額

和提成比例;若為計(jì)件工人,則輸入產(chǎn)品件數(shù)和每件報(bào)酬;若為計(jì)時(shí)工人,則輸入工作小時(shí)

和小時(shí)報(bào)酬。

輸出:?jiǎn)T工月工資。

輸入樣例:1〈一一職工類別

5000.01(一一月薪和缺勤天數(shù)

輸出樣例:4772.73

提示:1.計(jì)算管理人員、銷售人員、計(jì)件工人、計(jì)時(shí)工人的月工資的函數(shù)原型可以分別設(shè)

計(jì)如下:

doublegetEarning(doublesalary,intabsenceDays);

doublegetEarning(doublebaseSalary,doublesalesSum,doublerate);

doublegetEaming(intworkPieces,doublewagePerPiece);

doublegetEarning(doublehours,doublewagePerHour);

2.菜單程序設(shè)計(jì)如下:

intmain()

(

cout?”Pleaseselect..."?endl;

cout?u1:Manager."?endl;

cout?"2:SalesMan."?endl;

cout?"3:PiecesWorker."?endl;

cout?"4:Hour-Worker."?endl;

cout?"Others:Quit11?endl;

cin?sei;

switch(sel)

(

case1:

cin?

cout?getEarning(...);

break;

case2:

cin?

cout?getEarning(...);

break;

case3:

cin?

cout?getEarning(...);

break;

case4:

cin?

cout?getEarning(...);

break;

default:

break;

)

return0;

)

#include<iostream>

usingnamespacestd;

doublegetEarning(doublesalary,intabsenceDays)

(

returnsalary-absenceDays*salary/22;

)

doublegetEarning(doublebaseSlary,doublesalesSum,doublerate)

(

returnbaseSlary+salesSum*rate;

)

doublegetEaming(intworkPieces,doublewagePerPiece)

returnworkPieces*wagePerPiece;

doublegetEarning(doublehours,doublewagePerHour)

(

returnhours*wagePerHour;

)

intmain()

(

doublesalary,baseSalary,salesSum,rate,hours,wagePerHour,wagePerPiece;

intabsenceDays,workPieces,sel;

cout?”Pleaseselect..."?endl;

cout?uI:Manager."?endl;

cout?”2:SalesMan.',?endl;

cout?"3:PiecesWorker."?endl;

cout?"4:Hour-Worker.”<<endl;

cout?"Others:Quit',?endl;

cin?sei;

switch(sel)

(

case1:

cin?salary?absenceDays;

cout?getEarning(salary,absenceDays);

break;

case2:

cin?baseSalary?salesSum?rate;

cout?getEarning(baseSalary,salesSum,rate);

break;

case3:

cin?workPieces?wagePerPiece;

cout?getEarning(workPieces,wagePerPiece);

break;

case4:

cin?hours?wagePerHour;

cout?getEarning(hours,wagePerHour);

break;

default:

break;

)

return0;

)

示例代碼---------------------------------------------------

#include<iostream>

usingnamespacestd;

doublegetEaming(doublesalary,intabsenceDays)

(

return(salary-salary*absenceDays/22);

}//admin

doublegetEaming(doublebaseSalary,doublesalesSum,doublerate)

(

return(baseSalary+saIesSum*rate);

)

doublegetEaming(intworkPieces,doublewagePerPiece)

(

return(workPieces*wagePerPiece);

)

doublegetEaming(doublehours,doublewagePerHour)

(

return(hours*wagePerHour);

)

intmain()

(

intkind=0;

cout?"Pleaseselect...'*?endl;

cout?"1:Manager."?endl;

cout?"2:SalesMan."?endl;

cout?"3:PiecesWorker."?endl;

cout?"4:Hour-Worker."?endl;

cout?"Others:Quit"?endl;

cin?kind;

switch(kind)

(

case1:

{

doublesalary;

intabDays;

cin?salary?abDays;

cout?getEarning(salary,abDays);

break;

)

case2:

(

doublebase;

doublesalesSum;

doublerate;

cin?base?salesSum?rate;

cout?getEaming(base,salesSum,rate);

break;

case3:

{

intworkPieces;

doublewagePerPiece;

cin?workPieces?wagePerPiece;

cout?getEaming(workPieces,wagePerPiece);

break;

)

case4:

(

doublehours;

doublewagePerHour;

cin?hours?wagePerHour;

cout?getEaming(hours,wagePerHour);

break;

)

default:

break;

)

//cout?getEarning(100.0,11);

//cout?getEarning(100.0,10.0,0.1);

//cout?getEarning(100,0.2);

//cout?getEaming(200,0.3);

return0;

來(lái)源:

實(shí)驗(yàn)題目(共4題,第2題)

標(biāo)題:2.引用傳遞

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:設(shè)計(jì)一個(gè)函數(shù),將兩個(gè)浮點(diǎn)數(shù)傳入,然后通過(guò)引用把其和、差、積傳出。

函數(shù)原型如下:voidMath(floata,floatb,float&sum,float&sub,float&pro);

輸入:輸入兩個(gè)浮點(diǎn)數(shù)

輸出:輸出兩個(gè)浮點(diǎn)數(shù)的和、差、積。

輸入樣例:6.52.3

輸出樣例:5

提示:

來(lái)源:

#include<iostream>

#include<stdio.h>

#include<stdlib.h>

usingnamespacestd;

voidMath(iloata,floatb)

(

floatsum,sub,pro;

sum=a+b;

sub=a-b;

pro=a*b;

cout?sum?”"?sub?'*n?pro?endl;

)

intmain()

(

floata,b;

cin?a?b;

Math(a,b);

return0;

)

示例代碼-------------------------------------------------

#include<iostream>

usingnamespacestd;

voidMath(floata,floatb,float&sum,float&sub,float&pro)

!

sum=a+b;

sub=a-b;

pro=a*b;

)

intmain()

(

floatsum=0;

floatsub=0;

floatpro=0;

floata=0;

floatb=0;

cin?a?b;

Math(a,b,sum,sub,pro);

cout?sum?"n?sub?Mn?pro?endl;

return0;

實(shí)驗(yàn)題目(共4題,第3題)

標(biāo)題:3.函數(shù)模板

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:設(shè)計(jì)一個(gè)函數(shù)模板,實(shí)現(xiàn)兩個(gè)同類型數(shù)據(jù)的交換。

將設(shè)計(jì)好的函數(shù)模板分別實(shí)例化為兩個(gè)整型數(shù)交換、兩個(gè)字符交換的模板函數(shù),調(diào)用這

些函數(shù)并輸出運(yùn)行結(jié)果。

輸入:分別輸入兩個(gè)整型數(shù)和兩個(gè)字符

輸出:分別輸出兩個(gè)整型數(shù)和兩個(gè)字符交換的結(jié)果

輸入樣例:59

輸出樣例:95

提示:

來(lái)源:

#include<iostream>

usingnamespacestd;

template<classTypoTypehuan(Typea,Typeb)

(

Typec;

c=a;

a=b;

b=c;

cout?a?*'?b?endl;

intmain()

(

inta,b;

cin?a?b;

huan(a,b);

charc,d;

cin?c?d;

huan(c,d);

return0;

)

示例代碼-----------------------------------------

#include<iostream>

usingnamespacestd;

template<classT>

Tcg(T&a,T&b)

(

Tc;

c=a;

a=b;

b=c;

)

intmain()

(

inta=0;

intb=0;

charc=0;

chard=0;

cin?a?b?c?d;

cg(a.b);

cg(c,d);

cout?a?""?b?endl?c<<""?d?endl;

return0;

實(shí)驗(yàn)題目(共4題,第4題)

標(biāo)題:4.默認(rèn)形參值

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:設(shè)計(jì)一個(gè)求空間兩點(diǎn)距離的函數(shù),要求第2個(gè)點(diǎn)的默認(rèn)值為坐標(biāo)原點(diǎn)。

輸入:兩個(gè)點(diǎn)的坐標(biāo)。

輸出:輸出第一個(gè)點(diǎn)與原點(diǎn)之間的距離及輸入的兩個(gè)點(diǎn)之間的距離。

輸入樣例:111

555

輸出樣例:1.73205

6.9282

提示:函數(shù)原型可設(shè)計(jì)如下:

floatdistance(floatx1,floaty1,floatzl,floatx2=0,floaty2=0,floatz2=0);

來(lái)源:

#include<iostream>

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

usingnamespacestd;

floatdistanceyuan(floatxl,floatyl,floatzl,floatx2=0,floaty2=0,floatz2=0)

(

floatjul;

ju1=sqrt(x1*x1+y1*y1+z1*z1);

returnjul;

)

floatdistanceliang(floatxI,floaty1,floatzl,floatx2,floaty2,floatz2)

(

floatju2,a,b,c;

a=xl-x2;b=y1-y2;c=zl-z2;

ju2=sqrt(a*a+b*b+c*c);

returnju2;

)

intmain()

(

floatx,y,z,w,m,n,jul,ju2;

cin?x?y?z;

cin?w?m?n;

jul=distanceyuan(x,y,z);

ju2=distanceliang(x,y,z,w,m,n);

cout?jul?endl?ju2?endl;

return0;

)

示例代碼----------------------------------------------------

#include<iostream>

#include<cmath>

usingnamespacestd;

floatdistance(floatx1,floatyl,floatzl,floatx2=0,floaty2=0,floatz2=0)

(

returnsqrt(pow(x1-x2,2)+pow(y1-y2,2)+pow(zl-z2,2));

)

intmain()

floatxl=0;

floatx2=0;

floatyl=0;

floaty2=0;

floatzl=0;

floatz2=0;

cin?x1?y1?z1?x2?y2?z2;

cout?distance(xl,yl,zl)?endl;

cout?distance(x1,y1,z1,x2,y2,z2)?endl;

return0;

實(shí)驗(yàn)題目(共2題,第1題)

標(biāo)題:類的定義一靜態(tài)常量

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:設(shè)計(jì)并實(shí)現(xiàn)一個(gè)圓柱類Cylinder,要求:

(1)設(shè)計(jì)兩個(gè)double型的私有數(shù)據(jù)成員:高度height和底圓半徑radius。

(2)設(shè)計(jì)一個(gè)double型的公有靜態(tài)常量數(shù)據(jù)成員pi(其值初始化為3.1415926),以及一個(gè)

獲取該常量的公有靜態(tài)成員函數(shù)GetPL

(3)在構(gòu)造函數(shù)的初始化列表中初始化高度height和底圓半徑radiuso

(4)設(shè)計(jì)用于獲取半徑的成員函數(shù)GetRadius、獲取高度的成員函數(shù)GetHeight、設(shè)置半徑

的成員函數(shù)SetRadius、設(shè)置高度的成員函數(shù)SetHeight。

(5)設(shè)計(jì)用于計(jì)算圓柱體積的成員函數(shù)M)lume()、計(jì)算圓柱表面積的成員函數(shù)Area。

在main函數(shù)中:

根據(jù)輸入的高度和半徑構(gòu)造圓柱對(duì)象,并輸出圓柱的基本信息、體積和表面積。

輸入:圓柱的高度和底圓半徑

輸出:圓周率pi、圓柱高度、底圓半徑、圓柱的體積和表面積

輸入樣例:2.01.0

輸出樣例:pi=3.14159,height=2,radius=l:volume=6.28319,area=18.8496

提示:

來(lái)源:

#include<iostream>

usingnamespacestd;

classCylinder

(

private:

doubleheight,radius;

public:

staticconstdoublePi=3.1415926;

Cylinder(doublea=0,doubleb=0)

(

height=a;

radius=b;

)

doubleGetPI()

(

returnPi;

)

voidGetRadius()

(

cin?radius;

)

voidGetHeight()

(

cin?height;

)

doubleSetRadius()

(

returnradius;

)

doubleSetHeight()

(

returnheight;

)

doubleVolume()

(

returnPi*radius*radius*height;

)

doubleArea()

return2*Pi*radius*height+2*Pi*radius*radius;

intmain()

Cylindery;

y.GetHeight();

y.GetRadius();

cout?upi=,'?y.GetPI()?u,n?”

cout?uheight=u?y.SetHeight()?'\n?”

cout?Uradius=n?y.SetRadius()?;

cout?"volume="<<y.Vblume()?"Jv<"";

cout?uarea="?y.Area()?endl;

return0;

)

實(shí)驗(yàn)題目(共2題,第2題)

標(biāo)題:類的定義一深拷貝

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:設(shè)計(jì)并實(shí)現(xiàn)一個(gè)動(dòng)態(tài)整型數(shù)組類Vect,要求:

(1)實(shí)現(xiàn)構(gòu)造函數(shù)重載,可以根據(jù)指定的元素個(gè)數(shù)動(dòng)態(tài)創(chuàng)建初始值為0的整型數(shù)組,或根

據(jù)指定的內(nèi)置整型數(shù)組動(dòng)態(tài)創(chuàng)建整型數(shù)組。

(2)設(shè)計(jì)拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù),注意使用深拷貝。

(3)設(shè)計(jì)存取指定位置的數(shù)組元素的公有成員函數(shù),并進(jìn)行下標(biāo)越界,若越界則輸出“out

ofboundary

(4)設(shè)計(jì)獲取數(shù)組元素個(gè)數(shù)的公有成員函數(shù)。

(5)設(shè)計(jì)用于輸出數(shù)組元素的公有成員函數(shù),元素之間以空格分隔,最后以換行符結(jié)束。

在main函數(shù)中按以下順序操作:

(1)根據(jù)內(nèi)置的靜態(tài)整型數(shù)組{1,2,3,4,5}構(gòu)造數(shù)組對(duì)象vl,根據(jù)輸入的整型數(shù)構(gòu)造數(shù)組對(duì)

象v2。

(2)調(diào)用Vect的成員函數(shù)依次輸出vl和v2的所有元素。

(3)輸入指定的下標(biāo)及對(duì)應(yīng)的整型數(shù),設(shè)置數(shù)組對(duì)象vl的指定元素。

(4)根據(jù)數(shù)組對(duì)象vl拷貝構(gòu)造數(shù)組對(duì)象v3。

(5)調(diào)用Vect的成員函數(shù)依次輸出vl和v3的所有元素。

輸入:用于構(gòu)建數(shù)組對(duì)象v2的元素個(gè)數(shù)

用于設(shè)置數(shù)組對(duì)象vl的指定下標(biāo)及元素值

輸出:數(shù)組對(duì)象vl和v2的所有元素

數(shù)組對(duì)象vl和v3的所有元素

輸入樣例:10

66

輸出樣例:12345

0000000000

outofboundary

12345

12345

如?。?/p>

來(lái)源:

#include<iostream>

usingnamespacestd;

classVect

(

public:

Vect(intn=0);

Vect(intarr[],intn);

Vect(constVect&v);

?Vect();

intSize()

returnarrSize;

intGetData(inti)

if(i<0||i>=arrSize)

(

cout?"outofboundary"?endl;

)

else

returnarrDatafi];

)

voidSetData(inti,intval)

(

if(i<0||i>=arrSize)

(

cout?"outofboundary"?endl;

)

else

arrData[i]=val;

)

voidPrintData()

(

if(arrSize>0)cout?arrData[0];

fbr(inti=l;i<arrSize;i++)

(

cout?Mn?arrData[i];

)

cout?endl;

)

private:

intarrSize;

int*arrData;

);

Vect::Vect(intn):arrSize(n)

(

arrData=newint[arrSizel;

for(inti=0;i<arrSize;i++)

(

arrData[i]=0;

)

)

Vect::Vect(intarr[],intn):arrSize(n)

arrData=newintlarrSize];

for(inti=0;i<arrSize;i++)

arrData[i]=arr[i];

)

)

Vect::Vect(constVect&v):arrSize(v.arrSize)

(

arrData=newint[arrSize];

for(inti=0;i<arrSize;i++)

(

arrData[i]=v.arrData[i];

Vect:>Vect()

(

if(arrData)

(

delete[]arrData;

arrSize=0;

intmain()

(

intarr[]={1,2,3,4,5};

intn;

cin?n;

Vectvl(aiT,5),v2(n);

vl.PrintData();

v2.PrintData();

inti,data;

cin?i?data;

vl.SetData(i,data);

Vectv3(vl);

vl.PrintData();

v3.PrintData();

return0;

}

實(shí)驗(yàn)題目(共3題,第1題)

標(biāo)題:虛基類

時(shí)限:1000ms

內(nèi)存限制:10000K

總時(shí)限:3000ms

描述:(1)定義人員類Person:

公有成員:姓名(Name);

保護(hù)成員:性別(Gender),年齡(Age);

構(gòu)造函數(shù)和析構(gòu)函數(shù)

(2)從人員類Person派生學(xué)生記錄類StudenlRecord:

添加公有成員:學(xué)號(hào)(Number),班級(jí)(ClassName),

添加靜態(tài)公有成員:學(xué)生總?cè)藬?shù)(TotalCount);

添加保護(hù)成員:平均成績(jī)(Score);

實(shí)現(xiàn)構(gòu)造函數(shù)和析構(gòu)函數(shù)。

⑶從人員類Person派生教師記錄類TeacherRecord:

添加公有成員:學(xué)院(CollegeName),系(DeparlmentName);

添加保護(hù)成員:教齡(Year);

實(shí)現(xiàn)構(gòu)造函數(shù)和析構(gòu)函數(shù)。

(4)從學(xué)生記錄類StudentRecord和教師記錄類TeacherRecord派生學(xué)生助教類

TeachingAssistant:

添加公有成員:輔導(dǎo)課程(LectureName);

實(shí)現(xiàn)公有函數(shù):顯示人員信息(Show),屏幕打印姓名,性別,年齡,學(xué)號(hào),班級(jí),學(xué)生

總?cè)藬?shù),平均成績(jī),學(xué)院,系,教齡,輔導(dǎo)課程。

實(shí)現(xiàn)構(gòu)造函數(shù)和析構(gòu)函數(shù)。為檢驗(yàn)類間結(jié)構(gòu)設(shè)計(jì)是否正確,設(shè)計(jì)函數(shù)voidSetName(String

name)實(shí)現(xiàn)更改一名助教的姓名的功能。

創(chuàng)建一個(gè)助教類的對(duì)象

助教

姓名性別年齡學(xué)號(hào)班級(jí)平均成績(jī)學(xué)院系教齡輔導(dǎo)課程

鄭七男222010123軟2010189信息軟件1數(shù)據(jù)結(jié)構(gòu)

顯示其信息。

調(diào)用更改姓名的函數(shù),更改其姓名為“鄭八”,并再次顯示其信息。

輸入:無(wú)

輸出:顯示構(gòu)造的信息和更改前和更改后的助教信息

輸入樣例:無(wú)

輸出樣例:Person鄭七constructed

Student鄭七constructed

teacher鄭七constructed

teachingassistant關(guān)B七constructed

Name:關(guān)B七Gender:男Age:22Number:2010123ClassName:^20101TotalCount:!Score:8

9coHegeName:信息DepartmenlName:軟件Year:lLectureName:數(shù)據(jù)結(jié)構(gòu)

Name:鄭八Gender:男Age:22Number:2010123ClassName:^20101TotalCount:!Score:8

9coHegeName:信息DepartmenlName:軟件Year:lLeclureName:數(shù)據(jù)結(jié)構(gòu)

teachingassistant關(guān)口八destructed

teacher關(guān)B八destructed

Student關(guān)K八destructed

Person鄭八destructed

提示:各類的構(gòu)造函數(shù)和析構(gòu)函數(shù)都有輸出。

調(diào)用公有函數(shù)Show,以分別顯示各個(gè)記錄的人員信息。

在派生助教類時(shí),使用虛基類。

來(lái)源:

#include<iostream>

#include<cstring>

usingnamespacestd;

classPerson

!

protected:

stringGender;

intAge;

public:

stringName;

Person(stringgender,intage,stringname)

(

Gender=gender;

Age=age;

//Name=newchar[strlen(name)+1];

//strcpy(Name,name);

Name=name;

cout?nPerson"?Name?nconstructed"?endl;

)

?Person。

cout?"PersonH?Name?"destructed"?endl;

//delete[]Name;

//delete[]Gender;

)

/*voidShow()

(

cout?"Name:,,?Name?n"?"Gender:n?Gender?n"?nAge:"?Age?endl;

產(chǎn)/

classStudentRecord:virtualpublicPerson

(

protected:

intScore;

public:

stringNumber;

stringClassName;

staticintTotalCount;

StudentRecord(stringnumber,stringclassname,intscore,stringgender,intage,string

name):Person(gender,age,name)

//Number=newchar[strlen(number)+1];

//strcpy(Number,number);

Number=number;

//ClassName=newchar[strlen(classname)+1];

//strcpy(ClassName,classname);

ClassName=classname;

Score=score;

++TotalCount;

cout?,,Student',?Name?"constructed',?endl;

}

-StudentRecord()

(

//delete[]ClassName;

//delete[]Number;

-TotalCount;

cout?',Studentu?Name?,'destructed,'?endl;

)

/*voidShow()

{

cout?',Name:',?Name?,'“<<"Gende亡"<<Gender<<""<<“Age:"<<Age<<”

"〈〈"Number:"?Number?u,,?MClassName:"?ClassName?"

,,?',TotalCount:',?TotalCount?u"?nScore:u?Score?endl;

}*/

);

intStudentRecord::TotalCount=0;

classTeacherRecord:virtualpublicPerson

(

protected:

intYear;

public:

stringCollegeName;

stringDepartmentName;

TeacherRecord(stringcollegename,stringdepartmentname,intyear,stringgender,int

age,stringname):Person(gender,age,name)

(

//CollegeName=newchar[strlen(collegename)+1J;

//strcpy(CollegeName,collegename);

CollegeName=collegename;

//DepartmentName=newchar[strlen(departmentname)+1];

/Zstrcpy(DepartmentName,departmentname);

DepartmentName=departmentname;

Year=year;

cout?nTeacher"?Name?"constructedn?endl;

)

?TeacherRecord。

(

//delete[]DepartmentName;

//delete[]CollegeName;

cout?,,Teacher"?Name?',destructed',?endl;

)

/*voidShow()

(

cout?"Name:,,?Name?,'n?MGender:"?Gender?""?"Age:"?Age?u

",?',CollegeName:',?CollegeName?,'',?',DepartmentName:,,?DepartmentName?,,

"?"Year:',?Year?endl;

}*/

);

classTeachingAssistant:publicStudentRecord,publicTeacherRecord

(

public:

stringLectureName;

TeachingAssistant(stringlecturename,stringnumber,string

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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)論