c數(shù)據(jù)結(jié)構(gòu)上機(jī)題及實現(xiàn)代碼_第1頁
c數(shù)據(jù)結(jié)構(gòu)上機(jī)題及實現(xiàn)代碼_第2頁
c數(shù)據(jù)結(jié)構(gòu)上機(jī)題及實現(xiàn)代碼_第3頁
c數(shù)據(jù)結(jié)構(gòu)上機(jī)題及實現(xiàn)代碼_第4頁
已閱讀5頁,還剩109頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1:時鐘類與對象的定義2:平均數(shù)3:定義學(xué)生類4:打卡機(jī)5:找出每組數(shù)據(jù)中的最大值(函數(shù)重載)6:長方形類與對象的定義7:圓的面積8:輸出類對象9:構(gòu)造函數(shù)的重載10:構(gòu)造函數(shù)重載”:實現(xiàn)自定義的析構(gòu)函數(shù)12:構(gòu)造函數(shù)一定義矩形13:對象數(shù)組14:構(gòu)造函數(shù)與拷貝構(gòu)造函數(shù)ーー定義圓柱體類15:構(gòu)造函數(shù)、拷貝構(gòu)造和析構(gòu)函數(shù)ーー定義學(xué)生類16:統(tǒng)計學(xué)生人數(shù)和成績17:拷貝構(gòu)造函數(shù)18:三角形面積19:類的靜態(tài)與友元ーー職エ薪水20:統(tǒng)計學(xué)生信息21:學(xué)生成績等級22:類的繼承ーー定義學(xué)生類及其派生類23:類的繼承ーー定義點(diǎn)類及其派生的圓類24:長方形的面積和長方體的體積25:多繼承 Time類和Date類派生出Birthtime類26:RoundTable類27:類的繼承28:奇葩后代29:ATM30:虛基類ー在職讀書的教師類31:類的虛基類ーー客貨兩用汽車32:類的虛基類ーー主任工程師類33:NBA2K0L34:總價多少35:時鐘類36:平均數(shù)37:社會保障卡38:運(yùn)算符重載ーー重載+和ー對復(fù)數(shù)類對象進(jìn)行加減運(yùn)算39:復(fù)數(shù)類的運(yùn)算符重載40:三角形面積之和41:運(yùn)算符重載ーーT重載運(yùn)算符“>”,用于對給定的兩個字符串進(jìn)行比較42:復(fù)數(shù)43:計數(shù)器44:復(fù)整數(shù)相加45:運(yùn)算符重載矩陣相加46:數(shù)列中n個整數(shù)排序47:運(yùn)算符重載ーー重載賦值運(yùn)算符二用于字符串賦值48:字符串類49:圖形的面積50:求矩陣之和51:矩陣取反1:時鐘類與對象的定義

Tlme/MemoryLimit:1000MS/32768K

Submitted:60Accepted:45定義ー個時鐘類,它的數(shù)據(jù)成員包括:時、分、秒,它的函數(shù)成員有設(shè)置時間和顯示時間,如果設(shè)置時鐘的時、分、秒超過有效的范圍(24小時制),則輸出“error!”。編程并測試這個類。Input輸入數(shù)據(jù)有多組,每組占一行,每行中有三個數(shù),分別代表時、分、秒,用空格分隔。Output對于每組輸入數(shù)據(jù),輸出一行,每個數(shù)據(jù)間用:分隔。SampleInput1250SampleOutput12:5:0#include<iostream>usingnamespacestd;classTIME(private:intH,M,S;public:TIME(inth=0,intm=0,ints=0)(H=h;M=m;S=s;)voidshow()(if(H<0||H>=24||M<0||M>60||S<0||S>60)cout?Merror!n?endl;elsecout?H?',:,,?M?,,:n?S?endl;));intmain()(inth,m,s;while(cin?h?m?s)TIMEone(h,m,s);one.show();}return0;)2:平均數(shù)Time/MemoryLimit:1000MS/32768KSubmitted:59Accepted:45ProblemDescription定義一個學(xué)生類記錄學(xué)生的學(xué)號、姓名、成績。要求使用用靜態(tài)成員變量和靜態(tài)成員函數(shù)求若干個學(xué)生的平均成績。Input輸入數(shù)據(jù)有多行。每行一個浮點(diǎn)數(shù)。Output輸出數(shù)據(jù)有多行,每行輸出當(dāng)前分?jǐn)?shù)總和除以當(dāng)前總?cè)藬?shù)的結(jié)果(保留兩位小數(shù));SampleInput1000zhangsan851001lisi741002wangwu921003zhaoliu671004zhaoqian56SampleOutput85.0079.5083.6779.5074.80#include<iostream>#include<iomanip>#include<string>usingnamespacestd;classstudentprivate:stringname;stringnum;doublescore;staticdoubleave;staticdoublesum;staticintcount;public:student(stringnum=,,n,stringname=,,n,doubles=0){this->num=num;this->name=name;score=s;count++;sum=sum+score;ave=sum/count;)staticvoidshow()(cout?setiosflags(ios::fixed);cout?setprecision(2)?ave?endl;}};doublestudent::sum=0.0;doublestudent::ave=0.0;intstudent::count=0;intmain(){doubles;stringnum;stringname;while(cin?num?name?s)(studentone(num,name.s);student::show();)return0;)3:定義學(xué)生類Time/MemoryLimit:1000MS/32768KSubmitted:67Accepted:46定義學(xué)生類student,類的結(jié)構(gòu)如下classstudent(public:student(int,char*,int,float);//需提不Constructing...student();//需提不Destructing...z,voidprintstuO;private:intid;char*name;intage;floatscore;);將類定義完整,并在主函數(shù)中進(jìn)行測試。Input輸入數(shù)據(jù)有多行。Output對于每個測試數(shù)據(jù),輸出有多行。SampleInputzhangsan1980lisi2091wangwu1885SampleOutputConstructing...Num:1Name:zhangsanAge:19Score:80Destructing...Constructing...Num:2Name:lisiAge:20Score:91Destructing.?.Constructing...Num:3Name:wangwuAge:18Score:85Destructing...#include<iostream>#include<string.h>usingnamespacestd;classstudent

private:intid;char*name;intage;floatscore;public:student(inti=0,char*na=NULL,inta=0,floats=0.0)(id=i;if(na!=NULL){name=newchar[strlen(na)+l];strcpy(name,na);}elsename=NULL;age=a;score=s;coutく<"Constructing…"vvendl;}~student()(if(name!=NULL)delete[Jname;cout?"Destructing..."?endl;}voidprintstu()(if(name!=NULL)Age:"?age?"cout?"Num:"?id?" Name:"?name?"Age:"?age?"Score:"?score?endl;));intmain()(inti,a;floats;charn[3OJ;while(cin?i?n?a?s)(studentone(i,n,a,s);one.printstu();return0;

4:打卡機(jī)Time/MemoryLimit:1000MS/32768KSubmitted:42Accepted:27ProblemDescriptionLPRJ小工廠是剛興起不久的標(biāo)準(zhǔn)工廠,每天早上八點(diǎn)開始上班,每天工作ハ小時,但是由于LPRJ小工廠近來員エ懈怠于工作,經(jīng)常遲到,于是經(jīng)理LP決定用考勤打卡機(jī)來記錄員エ的上班時間,經(jīng)理為了整頓ー下員エ的上班態(tài)度,決定員エ每遲到半小時將扣除10元的工資,若不遲到員エ每天的工資有80元,計算每位員エ在今天可獲得的工資。(要求用類完成)Input輸入數(shù)據(jù)有多組,每組第一行輸入ー個整數(shù)T,代表接下去有T行員エ測試數(shù)據(jù)每位員エ的資料包含エ號,姓名,打卡時間(hh:mm);Output輸出每位員エ的信息以及當(dāng)天可獲得的工資。SampleInput312078LP07:4512080shik08:0012012junl08:31212078LP07:4512080shik08:00SampleOutput12078LP07:458012080shik08:008012012junl08:316012078LP07:458012080shik08:0080#include<iostream>#include<string>usingnamespacestd;classstu

private:stringnum;stringname;intH,M;public:stu(stringnum=,,H,stringname="”,inthh=O,intmm=O){this->num=num;this->name=name;H=hh;M=mm;)intSS()(intsalary;if(H<8||(H==8&&M==0))salary=80;else(if(M%30!=0)salary=80?(H-8)*2*10-(M/30+1)*10;elsesalary=80-(H-8)*2*10-M/30*l0;)if(salary<=0)salary=0;returnsalary;)voidshow()(cout?this->num?""?this->name?"if(H<10)cout?"0";cout?H?":";if(M<10)cout?"0";cout?M?""?SS()?endl;));intmain()(intT,hh,mm;stringnum;stringname;charcl,c2;while(cin?T)(while(T—)(cin?num?name?hh;cin.get();cin?mm;stuone(num,name,hh,mm);one.show();))return0;1 5:找出每組數(shù)據(jù)中的最大值(函數(shù)重載)Time/MemoryLimit:1000MS/32768K

Submitted:68Accepted:46ProblemDescription定義函數(shù)Max實現(xiàn)找出每組測試數(shù)據(jù)中的最大值,在主函數(shù)中進(jìn)行調(diào)用,要求Max函數(shù)能夠?qū)崿F(xiàn)分別在2個int型數(shù)據(jù)、2個字符串(不包括空格,長度不超過50)、3個double型數(shù)據(jù)中找到最大值。Input輸入數(shù)據(jù)有三組,每組占一行。Output對于每組輸入數(shù)據(jù),輸出一行。SampleInput1255.625.788.8goodmorningSampleOutputmax:2max:88.8max:morning#include<iostream>#include<string.h>usingnamespacestd;voidMax(inta,inib)intmax;if(a>=b)max=a;elsemax=b;cout?"max:H?max?endl;)voidMax(doublex,doubley,doublez)(doublemax;if(x>=y)max=x;elsemax=y;if(max<z)max=z;cout?Hmax:n?max?endl;}voidMax(charn1[50],charn2[50])(charmax3[50];if(strcmp(nl,n2)>0)strcpy(max3,n1);elsestrcpy(max3,n2);cout?,'max:,,?max3?endl;)intmain()(doublex,y,z;inta,b;charnl[50],n2[50];cin?a?b;cin?x?y?z;cin?nl?n2;Max(a,b);Max(x,y,z);Max(nl,n2);return0;

6:長方形類與對象的定義Time/MemoryLimit:1000MS/32768KSubmitted:57Accepted:54ProblemDescription定義ー個類area,它有兩個整形的私有數(shù)據(jù)代表長方形的長和寬,有三個成員函數(shù)init、print、areas,init用來給長方形初始化,print用來輸出長方形的面積,areas用來計算長方形的面積,要求將類定義完整,并通過main函數(shù)定義相應(yīng)的長方形對象,輸出對象的面積。Input輸入數(shù)據(jù)有多組,每組占一行,每行中有兩個數(shù),用空格分隔。Output對于每組輸入數(shù)據(jù),輸出一行SampleInput2452SampleOutputio243516#include<iostream>usingnamespacestd;classArea(private:intl,w;public:voidinit(int11jntwl)1=11;w=wl;

)intSS()(ints;s=l*w;returns;)voidprint(););voidArea::print()(cout?SS()?endl;1intmain()(Areaa;int12,w2;while(cin?12?w2)(a.init(12,w2);a.print();)return0;)7:圓的面積Time/MemoryLimit:1000MS/32768KSubmitted:100Accepted:48ProblemDescription設(shè)計ー個Circle類,可以求圓的面積。Input輸入數(shù)據(jù)有多組,每組占一行,每行包括ー個實數(shù)r,表示圓的半徑。Output輸出圓的面積。其中PI=3.14。SampleInputSampleOutput3.14#include<iostream>usingnamespacestd;classCircle(private:doubler;public:voidinit(doublerl)(r=rl;)doubleSS()(doubles;s=r*r*3.14;returns;)voidprint(););voidCircle::print()(cout?SS()?endl;1intmain()(Circlex;doubler2;while(cin?r2)(x.init(r2);x.print();}return0;)8:輸出類對象Time/MemoryLimit:1000MS/32768K

Submitted:59Accepted:47ProblemDescription

定義ー個類Student,用成員變量name、no、age描述ー個學(xué)生的姓名、學(xué)號和年齡的信息?,F(xiàn)從鍵盤獲取三個不同學(xué)生的信息。按輸入的順序?qū)⑺鼈兇嫒胂鄳?yīng)的對象中,并顯示它們(一行一條記錄)。Input數(shù)據(jù)之間用空格分割,每行輸入ー個記錄。共3條記錄。Output數(shù)據(jù)之間用ー個空格分割,每行輸出ー個記錄。共3條記錄。SampleInputzhangsan121lishi219wangwu321SampleOutputzhangsan121lishi219wangwu321#include<iostream>#include<string.h>usingnamespacestd;classStudent(private:charname[20];intno,age;public:voidinit(charn1[20],intnol,intage1)(strcpy(name,nl);no=nol;age=age1;}voidprint()(cout?name?Mu?no?nM?age?endl;));intmain()

Studentx;charname2[20];intn,a,j=O;while(cin?name2?n?a){x.init(name2,n,a);x.print();)return0;}#include<iostream>#include<string.h>usingnamespacestd;classstudent(private:intno,age;char*name;public:student(char?na=,,,',intn=O,inta=0)(intlen=0;len=strlen(na);if(len!=O){name=newchar[len4-l];strcpy(name,na);)elsename=NULL;no=n;age=a;)~student()(if(strlen(name)!=O)delete[]name;)voidshow()(cout?name?M,,?no?*'n?age?endl;}};intmain()charn[20]=>,n;intnl,al;while(cin?n?n1?al)(students(n,nl,al);s.show();}return0;)9:構(gòu)造函數(shù)的重載Time/MemoryLimit:1000MS/32768K

Submitted:71Accepted:50ProblemDescription為日期類定義4個構(gòu)造函數(shù),分別是:不帶參數(shù),日期為2000/1/1;帶一個參數(shù),僅初始化日數(shù)據(jù),年數(shù)據(jù)為2000,月數(shù)據(jù)為1;帶兩個參數(shù),初始化月、日數(shù)據(jù),年數(shù)據(jù)為2000;帶三個參數(shù),初始化年、月、日;此外日期類還有一個輸出成員函數(shù),用來輸出年月日。在主函數(shù)中進(jìn)行測試。Input輸入數(shù)據(jù)有3行,第一行只有一個數(shù)據(jù),代表日的數(shù)值;第二行有兩個數(shù)據(jù),代表月與日的數(shù)值,第三行有三個數(shù)據(jù),代表年、月、日的數(shù)值。Output輸出有4行,每行代表一個日期。SampleInput1io1201211SampleOutput2000/1/12000/10/12012/1/12000/1/1#include<iostream>usingnamespacestd;classDATE(private:intyear,month,day;public:DATE()(year=2OOO;month=l;day=l;)DATE(intda){ year=2000;month=l;day=da;)DATE(intm,intd)(year=2OOO;month=m;day=d;)DATE(inty,intm,intd)(year=y;month=m;day=d;)voidshow()(cout?year?,7,,?month?,7,,?day?endl;));intmain()(inty,m,d;cin?d;DATEdate2(d);date2.show();cin?m?d;DATEdate3(m,d);date3.show();cin?y?m?d;DATEdate4(y,m,d);date4.show();DATEdatel;datel.show();return0;)10:構(gòu)造函數(shù)重載Time/MemoryLimit:1000MS/32768K

Submitted:60Accepted:54ProblemDescription自定義類ー個類Audiengce,類Audiengce內(nèi)僅有一個整型、ー個字符數(shù)組數(shù)據(jù)成員,請分別定義兩個構(gòu)造函數(shù),ー個僅以整型數(shù)據(jù)作為行參,另外一個僅以字符指針作為行參。從鍵盤輸入一個字符串和一個整型數(shù)據(jù),用這些數(shù)據(jù)去創(chuàng)建兩個對象,使其分別調(diào)用兩個不同的構(gòu)造函數(shù)。并分別把對象中的數(shù)據(jù)顯示出來。Input任意ー個字符串,空格之后緊跟ー個整數(shù)。Output第一個對象中字符串,第二個對象中整數(shù),各占一行。SampleInputguest7SampleOutputguest7#include<iostream>usingnamespacestd;classAudiengce(private:intx;char*str;public:Audiengce(char*q)intlen=O;len=strlen(q);if(len!=O)str=newchar[len+l];strcpy(str,q);)elsestr=NULL;x=0;)Audiengce(inty)(x=y;str=NULL;}-Audiengce()(if(str!=NULL)delete[]str;)voidshowone()(cout?str?endl;)voidshowtwo()(cout?x?endl;));intmain()(intx;charstrflOO];while(cin?str?x)(Audiengceone(str);one.showone();Audiengcetwo(x);two.showtwo();)return0;11:實現(xiàn)自定義的析構(gòu)函數(shù)Time/MemoryLimit:1000MS/32768K

Submitted:66Accepted:48ProblemDescription自定義一個類Person,類中僅有一個字符指針成員變量name,用于保存從鍵盤接收ー任意長度的字符串。要求根據(jù)該串的大小在創(chuàng)建對象時候動態(tài)申請一片空間,在析構(gòu)該類的每個對象之前釋放該對象擁有的堆中內(nèi)存。請編寫類析構(gòu)函數(shù)以實現(xiàn)這一意圖。(為了觀察,請在析構(gòu)對象時候顯示deleted。)Input輸入一字符串,不含空格。Output該對象中字符串內(nèi)容deletedSampleInputabedSampleOutputabeddeleted#include<iostream>usingnamespacestd;classPerson(private:char*name;public:Person(char*str=',M){intlen=0;len=strlen(str);if(len!=0)(name=newchar[len+lJ;strcpy(name,str);elsestr=NULL;)-Person。(if(name!=NULL)delete[]name;cout?ndeletedn?endl;)voidshow。(cout?name?endl;)I;intmain。(charnflOOO];while(cin?n)(Personone(n);one.show。;)return0;12:構(gòu)造函數(shù).定義矩形Time/MemoryLimit:1000MS/32768KSubmitted:53Accepted:50ProblemDescription定義ー個矩形類,要求定義成員函數(shù)實現(xiàn):構(gòu)造長方形、計算長方形的面積、周長,假定長和寬分別由兩個整型變量high和width表示。Input輸入數(shù)據(jù)有多行,每行有兩個數(shù)據(jù),代表矩形的長和寬。Output每組輸出占一行,為矩形的面積和周長并用空格隔開.SampleInput21020100200SampleOutput62006020000600#include<iostream>usingnamespacestd;classRectangle(private:doublehigh,width;public:Rectangle(doubleh=0,doublew=0)(high=h;width=w;}voidarea();voidperimeter();};voidRectangle::area()(doubles;s=high*width;cout?s?"H;)voidRectangle::perimeter(){double1;l=2*(high+width);cout?l?endl;)intmain()(doubleh,w;while(cin?h?w)(Rectangleone(h,w);one.area();one.perimeter();

)return0;}13:對象數(shù)組Time/MemoryLimit:1000MS/32768KSubmitted:77Accepted:50ProblemDescription定義ー個學(xué)生類,數(shù)據(jù)成員包括:學(xué)號(整數(shù)),姓名,年齡,成績;成員函數(shù)包括設(shè)置值和輸出顯示。在主函數(shù)中定義學(xué)生數(shù)組,表示多個學(xué)生,給每個學(xué)生設(shè)置值,然后輸出顯示。Input輸入數(shù)據(jù)有多行,第一行為整數(shù)n,表示一共有n個學(xué)生數(shù)據(jù),接下來的n行為學(xué)生數(shù)據(jù)信息,每行一個學(xué)生。Output輸出有n行,每行代表ー個學(xué)生,輸出要有相應(yīng)的提示,每行的信息之間用空格分隔。SampleInput4"wang"1886"li"1872nzhao"1780"guo"1884SampleOutputId:1Name:"wang"Age:18Score:86Id:2Name:"li"Age:18Score:72Id:3Name:"zhao"Age:17Score:80Id:4Name:"guo"Age:18Score:84#include<iostream>#include<string>usingnamespacestd;classstudentprivate:

intnum,age,score;stringname;public:student(intn1=0,stringname=,H',inta1=0,intsl=0)(num=n1;this->name=name;age=al;score=sl;)voidshow()(cout?',Id:,,?num?n Name:,'?this->name?u Age:',?age?nScore:n?score?endl;));intmain()(studentone[100];intno,n,a,s,i;stringname;cin?n;for(i=0;i<n;i++)(cin?no?name?a?s;one[i]=student(no,name,a,s);)for(i=0;i<n;i++)one[i].show();return0;14:構(gòu)造函數(shù)與拷貝構(gòu)造函數(shù)——定義圓柱體

類Time/MemoryLimit:1000MS/32768K

Submitted:57Accepted:44ProblemDescription為圓柱體編寫ー個Cylinder類,圓柱體由底面半徑和高描述(float型),這個類具有如下的函數(shù):a)得出圓柱體的體積:圓柱的體積=底面積X高b)得出圓柱體的表面積:圓柱的表面積=上下底面面積+側(cè)面積c)能以如下方式創(chuàng)建一個圓柱體對象:Cylindercl:〃構(gòu)造ー個默認(rèn)底面半徑為10,高為10的圓柱體Cylinderc2(20,100)5〃構(gòu)造ー個底面半徑為20,高為100的圓柱體

Cylinderc3(cl);〃用ー個已有的圓柱體構(gòu)造ー個新的圓柱體在主函數(shù)中進(jìn)行測試。Input輸入數(shù)據(jù)有多行,每行有兩個數(shù)據(jù),代表圓柱體的底面半徑和高。Output輸出有多行,每行有兩個數(shù)據(jù),代表圓柱體的體積和表面積,最后兩行分別表示調(diào)用默認(rèn)構(gòu)造函數(shù)和拷貝構(gòu)造函數(shù)的圓柱體對象的體積和表面積。SampleInput20100457SampleOutput12560015072251.2226.0887.92113.043140125631401256#include<iostream>usingnamespacestd;classCylinder(private:floath,r;public:Cylinder()(r=10;h=10;)Cylinder(floatrl,floathl)(r=rl;h=hl;)floatVV()(floatv;v=3.14*r*r*h;

floatSS()(floats;s=2*3.14*r*h+2*3.14*r*r;returns;}voidshow(){cout?VV()?nn?SS()?endl;));intmain()(Cylinderc2(20,100);Cylindercl;Cylinderc3(cl);floath2,r2;while(cin?r2?h2)(Cylinderc2(r2,h2);c2.show();)c1.show();c3.show();return0;)15:構(gòu)造函數(shù)、拷貝構(gòu)造和析構(gòu)函數(shù)——定義

學(xué)生類Time/MemoryLimit:1000MS/32768K

Submitted:57Accepted:40ProblemDescription定義學(xué)生類,該類包含的數(shù)據(jù)成員有:學(xué)生編號,姓名,年齡,成績;成員函數(shù)有:構(gòu)造函數(shù),拷貝構(gòu)造函數(shù),析構(gòu)函數(shù),輸出學(xué)生信息函數(shù)。其中構(gòu)造函數(shù)要輸出"Constructing...”提示;拷貝構(gòu)造函數(shù)要輸出"CopyConstructing..."提示;析構(gòu)函數(shù)要輸出"Destructing..."提示。在主函數(shù)中進(jìn)行測試:主函數(shù)中先定義ー個學(xué)生對象si,數(shù)據(jù)從鍵盤中輸入,再定義ー個對象s2,用對象si對s2進(jìn)行初始化。Input輸入數(shù)據(jù)只有一行,分別代表學(xué)生的編號,姓名,年齡,成績。Output輸出兩個學(xué)生對象的信息,具體如下所示。SampleInput10wang1988SampleOutputConstructing...CopyConstructing...Id:10Name:wangAge:19Score:88Id:10Name:wangAge:19Score:88Destructing...Destructing...#include<iostream>#include<string>usingnamespacestd;classstudent(private:intnum,age,score;char*name;public:student(intn1=0,char*na="",inta1=0,intsl=0)(num=nl;name=newchar[strlen(na)+l];strcpy(name,na);age=al;score=sl;cout?,,Constructing...,,?endl;)student(student&p)(num=p.num;name=newchar[strlen()+l];strcpy(name,);age=p.age;score=p.score;cout?MCopyConstructing...n?endl;)~student()

delete[]name;cout?MDestructing..."?endl;)voidshow(){coutvv"Id:“vvnumvv"Name:'^?name?^^Age:n?age?HScore:"?score?endl;));intmain()(intn,a,s;charna[20];cin?n?na?a?s;studentsl(n,na,a,s);students2(sl);sl.show();s2.show();return0;' 16:統(tǒng)計學(xué)生人數(shù)和成績Time/MemoryLimit:1000MS/32768KSubmitted:57Accepted:47ProblemDescription定義ー個Student類記錄學(xué)生的學(xué)號和C++課程的成績。要求使用靜態(tài)成員變量和靜態(tài)成員函數(shù)計算全班學(xué)生C++課程的總成績和平均成績。Input輸入每個學(xué)生的學(xué)號和成績,每個學(xué)生的信息占一行,直到文件結(jié)束。Output輸出包括兩行,第一行全班人數(shù)和總成績,用空格隔開;第二行平均成績。SampleInput1013010250103901046010570SampleOutput530060#include<iostream>#include<string>usingnamespacestd;classstudent(private:doublescore;stringnum;staticintcount;staticdoublesum;staticdoubleave;public:student(stringnum,doublescore)(this->num=num;this->score=score;count++;sum=sum+score;ave=double(sum/count);)staticvoidshow()(cout?count?Hn?sum?endl;cout?ave?endl;));intstudent::count=0;doublestudent::sum=0.0;doublestudent::ave=0.0;intmain()(doubles;stringnum;while(cin?num?s)(studentstu(num,s);)student::show();17:拷貝構(gòu)造函數(shù)Time/MemoryLimit:1000MS/32768K

Submitted:52Accepted:40ProblemDescription自定義一個類User,類中有兩int型成員變量no、grade〇請定義ー個拷貝構(gòu)造函數(shù),以實現(xiàn)用ー個已經(jīng)存在的對象去初始化一個新建的對象。為了方便觀察,在拷貝構(gòu)造函數(shù)被調(diào)用時顯示"copyctrcalled”,在普通構(gòu)造函數(shù)被調(diào)用時顯示"ctrcalled"(":此處純粹用于表示定界,實際輸出時不必顯示它,另外相鄰的兩個單詞之間有一個空格)?,F(xiàn)從鍵盤獲取2個int數(shù)據(jù),先用它們創(chuàng)建ー個對象,然后用該對象再創(chuàng)建出一個新對象。Input兩個數(shù)據(jù)之間用空格分割,回車結(jié)束。Output第一次創(chuàng)建對象時顯示輸出,占一行。第二次調(diào)用拷貝構(gòu)造函數(shù)時顯示輸出,占一行。SampleInput123SampleOutputctrcalledcopyctrcalled#include<iostream>usingnamespacestd;classUser(private:intno,grade;public:User(intn1=0,intgl=0)(no=n1;grade=g1;cout?Hctrcalled"?endl;)User(User&p)(no=p.no;grade=p.grade;cout?ncopyctrcalled"?endl;)};intmain()(intn,g;while(cin?n?g)(Userstul(n,g);Userstu2(stul);)return0;)18:三角形面積Time/MemoryLimit:1000MS/32768KSubmitted:66Accepted:48ProblemDescription設(shè)計ー個三角形類Triangle,包含三角形三條邊長的私有數(shù)據(jù)成員。定義ー個友元函數(shù),以實現(xiàn)求兩個三角形對象的面積之和。Input輸入數(shù)據(jù)有多組,每組占二行,每行包括3個整數(shù),表示三角形的三條邊(保證能構(gòu)成三角形)。Output對于每組數(shù)據(jù),輸出占一行。先是第1個三角形的面積,然后是第2個三角形的面積,接下來是兩個面積之和。中間用空格隔開。SampleInput4556SampleOutput

6.009.9215.92Hints提示:已知三角形三條邊求面積,一般采用海侖公式:S=sqrt(p(p-a)(p-b)(p-c));a,b,c, 三角形邊長p=(a+b+c)/2#include<iostream>#include<iomanip>#include<cmath>usingnamespacestd;classTriangle(private:inta,b,c;public:Triangle(intx=O,inty=O,intz=O)(a=x;b=y;c=z;)friendvoidCalculate(constTriangle&pl,constTriangle&p2););voidCalculate(constTriangle&pl,constTriangle&p2)(doublec1=0.0,c2=0.0,sl=0.0,s2=0.0;c1=double((pl.a+pl.b+p1.c)/2.0);c2=double((p2.a+p2.b+p2.c)/2.0);si=double(sqrt(cl*(c1-pl.a)*(c1-p1.b)*(c1-pl.c)));s2=double(sqrt(c2*(c2-p2.a)*(c2-p2.b)*(c2-p2.c)));cout?setiosflags(ios::fixed);cout?setprecision(2)?sl?M,,?s2?H,,?sl+s2?endl;)intmain()(intxl,yl,zl;intx2,y2,z2;while(cin?x1?y1?z1)(cin?x2?y2?z2;rriangleone(xl,yl,zl);Triangletwo(x2,y2,z2);Calculate(one,two);

)return0;1 19:類的靜態(tài)與友元——職エ薪水Time/MemoryLimit:1000MS/32768KSubmitted:65Accepted:48ProblemDescription設(shè)計ー個職エ類,職エ類中包括職エ姓名、薪水和所有職工薪水總和(靜態(tài)成員)三個數(shù)據(jù)成員,成員函數(shù)包括有構(gòu)造函數(shù)、析構(gòu)函數(shù)、及返回所有職エ薪水總和的靜態(tài)成員函數(shù),此外還包含有一個友元函數(shù),用來比較兩個員エ的薪水,結(jié)果返回薪水大的那個員エ。要求將類定義完整并且用主函數(shù)進(jìn)行測試。Input輸入包括3個測試數(shù)據(jù)Output輸出包括5行,分別是3個職エ的信息,總的薪水總和,還有薪水最高的那個職ェ的信息。SampleInputZhangsan1200Lisi2400Wangwu1800SampleOutputZhangsan1200Lisi2400Wangwu1800Allsalaryis:5400Thehighestsalaryis:Lisi2400#include<iostream>#include<string.h>usingnamespacestd;classSTAFF(private:intsalary;char*name;staticintsum;public:STAFF(char*n=NULL,ints=0)(if(n!=NULL)(name=newchar[strlen(n)+l];strcpy(name,n);salary=s;sum=sum+salary;)elsename=NULL;)STAFF(STAFF&x){name=newchar[strlen()+1];strcpy(name,);salary=x.salary;}?STAFF。(if(name!=NULL)delete[]name;)voidshow1()(if(name!=NULL)cout?name?HM?salary?endl;)staticvoidshow2(){cout?HAllsalaryis:n?sum?endl;)friendSTAFFJUDGE(STAFF&pl,STAFF&p2);voidshow3(){if(name!=NULL)cout?"Thehighestsalaryis:"?name?n"?salary?endl;));intSTAFF::sum=0;STAFFJUDGE(STAFF&p1,STAFF&p2)if(p1.salary>=p2.salary)returnpl;elsereturnp2;}intmain()(ints;charn[30];cin?n?s;STAFFone(n,s);cin?n?s;STAFFtwo(n,s);cin?n?s;STAFFthree(n,s);one.showl();two.show1();three.showl();STAFF::show2();STAFFA=JUDGE(one,two);STAFFB=JUDGE(A,three);B.show3();return0;1 20:統(tǒng)計學(xué)生信息Time/MemoryLimit:1000MS/32768KSubmitted:78Accepted:49ProblemDescription現(xiàn)如今人人都已經(jīng)統(tǒng)計完了綜測分而輕松,然而LP卻接到學(xué)委賦予的偉大任務(wù)而手忙腳亂:統(tǒng)計班級綜測分的情況,好吧,你懂的,他在向你求助。Input輸入的數(shù)據(jù)有多行,每行為ー個同學(xué)的姓名,學(xué)號及個人綜測分,輸入至文件尾結(jié)束。Output對于每行輸入的數(shù)據(jù)都進(jìn)行輸出,每次輸出的數(shù)據(jù)有2行:第一行為該同學(xué)的姓名,學(xué)號及綜測分;

第二行為當(dāng)前已統(tǒng)計的同學(xué)人數(shù)及當(dāng)前已統(tǒng)計的所有同學(xué)的平均綜測分。(具體輸出詳見Sample)SampleInputLinping120000862580Jinkun1200008645100SampleOutputName:LinpingNo:1200008625Score:80Count:1Ave:80Name:JinkunNo:1200008645Score:100Count:2Ave:90#include<iostream>#include<string>usingnamespacestd;classstudent(private:stringname;stringnum;doublescore;staticintcount;staticdoublesum;staticdoubleave;public:student(stringname=,,H,stringnum='M,,doublescore=0.0){this->name=name;this->num=num;this->score=score;count++;sum=sum+this->score;ave=sum/count;)voidshowone()(cout?,,Name:',?this->name<<"No:',?this->num?"Score:M?this->score?endl;)staticvoidshowtwo()cout?MCount:,,?count?,'Ave:H?ave?endl;

);intstudent::count=0;doublestudent::sum=0.0;doublestudent::ave=0.0;intmain()(stringnum;stringname;doubles;while(cin?name?num?s)(studentone(name,num,s);one.showone();student::showtwo();)return0;21:學(xué)生成績等級

Time/MemoryLimit:1000MS/32768K

Submitted:56Accepted:47ProblemDescription有一個學(xué)生類student,包括學(xué)生姓名、成績,設(shè)計ー個友元函數(shù),輸出成績對應(yīng)的等級:大于等于90:A;8〇、90:B;7〇?79:C;6〇?69:D;小于60:E。Input先輸入數(shù)據(jù)n,表示有n個學(xué)生,接下是n個學(xué)生信息,包括姓名和成績。Output對于所有的輸入數(shù)據(jù),輸出學(xué)生數(shù)據(jù)(包括姓名、成績和等級)。SampleInput5stl67st280st390st456st588

SampleOutputstl67Dst280Bst390Ast456Est588B#include<iostream>#include<string>usingnamespacestd;classstudent(private:stringname;doublescore;public:student(stringname=,n,,doublescore=0.0)(this->name=name;this->score=score;}voidshowone()(cout?this->name<v""vvthis->score〈v"}friendvoidJUDGE(student&););voidJUDGE(student&p)(if(p.score>=90)cout?',A"?endl;elseif(p.score>=80)cout?,,B,'?endl;elseif(p.score>=70)cout?,,C"?endl;elseif(p.score>=60)cout?MDn?endl;elsecout?',E''?endl;}intmain()(stringname;doubles;

intn;cin?n;while(n—)cin?name?s;studentone(name,s);one.showone();JUDGE(one);)return0;22:類的繼承——定義學(xué)生類及其派生類Time/MemoryLimit:1000MS/32768KSubmitted:85Accepted:49ProblemDescription定義ー個學(xué)生類Student,它包括學(xué)生名、住址和出生年份等私有數(shù)據(jù)以及輸出函數(shù)show(),從它派生出ー個大學(xué)生Academician類及ー個小學(xué)生類Primary_scholar0在大學(xué)生類中增加數(shù)據(jù)成員大學(xué)校名university-name,在小學(xué)生獎中所增加的數(shù)據(jù)成員是兩門課的成績,兩個派生類所有有關(guān)后輸出都由函數(shù)show()來實現(xiàn)。Input輸入數(shù)據(jù)有多行,第一行有兩個整數(shù)n和m,分別代表大學(xué)生對象的個數(shù)n和小學(xué)生對象的個數(shù)m,接下來的n行為大學(xué)生的數(shù)據(jù)信息,m行為小學(xué)生的數(shù)據(jù)信息Output輸出每個學(xué)生的信息,一個學(xué)生一行SampleInput3zhangFuzhou1988ZhangzhouNormalUniversityliXiamen1987FuzhouUniversitywangZhangzhou19987589suQuanzhou19979090wuPutian19967887

SampleOutputName:zhang,Address:Fuzhou,Year:1988,University_Name:ZhangzhouNormalUniversityName:li,Address:Xiamen,Year:1987,University_Name:FuzhouUniversityName:wang,Address:Zhangzhou,Year:1998,Score:7589Name:su,Address:Quanzhou,Year:1997,Score:9090Name:wu,Address:Putian,Year:1996,Score:7887#include<iostream>#include<string>usingnamespacestd;classStudent(private:stringname;stringAddress;intage;public:Student(stringname1,stringAddress1,intagel)(name=namel;Address=Address1;age=age1;)voidshow()(cout?,,Name:",?name?,,,Address:',?Address?,,,Year:,'?age?,,,n;));classAcademician:publicStudent(private:stringuniversity_name;public:Academician(string name1,string Address1,int age1,stringuniversity_namel):Student(name1,Address1,age1){university_name=university_name1;)voidshow(){Student::show();cout?,,University_Name:,'?university_name?endl;);classPrimary_scholar:publicStudent(private:doublescorel,score2;public:Primary_scholar(string name1,stringAddressl,int age1,double s1,doubles2):Student(name1,Addressl,age1)(scorel=sl;score2=s2;)voidshow()(Student::show();cout?nScore:H?score1?**n?score2?endl;));intmain()(intn,m;stringnl,Al,ul;intal;doublesl,s2;while(cin?n?m)(while(n—){cin?n1?A1?a1?u1;Academicianone(n1,A1,a1,u1);one.show();)while(m—){cin?n1?Al?al?sl?s2;Primary_scholartwo(n1,A1,a1,s1,s2);two.show();})return0;,23:類的繼承——定義點(diǎn)類及其派生的圓類ProblemDescription定義ー個點(diǎn)類point,它帶有兩個私有數(shù)據(jù)成員:x,y(整數(shù));還有構(gòu)造函數(shù)(x和y的默認(rèn)值為〇),顯示坐標(biāo)函數(shù);point類共有派生出圓類circle,circle類具備point類的全部特征,同時自身也有自己的特點(diǎn):圓有半徑成員(可以是小數(shù)),求面積成員函數(shù),顯示成員函數(shù),該顯示函數(shù)可以顯示圓的圓心坐標(biāo),半徑,和圓的面積。在主函數(shù)中對circle類進(jìn)行測試。Input輸入數(shù)據(jù)有多行,第一行有一個整數(shù)n,表示ー共有n個圓對象,接下來的n行,每行包括ー共圓對象的圓心坐標(biāo)和它的半徑。Output輸出有n行,每行為ー個圓對象的圓心坐標(biāo)、半徑和它的面積。SampleInput3002123236.5SampleOutputCentre:(0z0)Radius:2Area:12.56Centre:(1z2)Radius:3Area:28.26Centre:(2,3)Radius:6.5Area:132.665#include<iostream>usingnamespacestd;classpoint(private:intx,y;public:point(intx1=0,intyl=0)(x=xl;y=yl;)voidshow()cout?MCentre:(,,?x?,,,,,?y?,')

));classcircle:publicpoint(private:doubler;public:circle(intxl=0,inty1=0,doublerl=0.0):point(xl,yl)(r=rl;)doubleSS()(return3.14*r*r;)voidshow()(point::show();cout?,,Radius:,'?r?nArea:u?SS()?endl;}};intmain()(intn,xl,yl;doublerl;while(cin?n)(while(n—)(cin?xl?yl?rl;circleone(xl,yl,rl);one.show();))return0;)24:類的繼承Time/MemoryLimit:1000MS/32768KSubmitted:67Accepted:49ProblemDescription編寫ー個學(xué)生和教師數(shù)據(jù)輸入和顯示程序,學(xué)生數(shù)據(jù)有編號、姓名、班號和成績,教師數(shù)據(jù)有編號、姓名、職稱和部門。要求設(shè)計ー個Person類,包含編號、姓名數(shù)據(jù)成員及構(gòu)造和顯示成員函數(shù),該類作為學(xué)生類和教師類的基類。Input輸入包括兩行,第一行是學(xué)生的相關(guān)信息,第二行是教師的相關(guān)信息Output輸出包括兩行,第一行是學(xué)生的相關(guān)信息,第二行是教師的相關(guān)信息SampleInput2009050102MarryComputer(1) 8902017TomyLecturerComputerSampleOutputNum:2009050102Name:MarryClass:Computer(1)Score:89Num:02017Name:Tomyprofession:LecturerDepartment:Computer#include<iostream>#include<string>usingnamespacestd;classperson(private:stringnum;stringname;public:person(stringnum1,stringname1)(num=numl;name=namel;)voidshow()(coutv<"Num:"《num<v"Name:,,?name?"M;});classstudent:publicperson(private:stringno;doublescore;public:student(stringnum1,stringname1,stringno1,doubles1):person(num1,name1)(no=nol;score=sl;)voidshow()(person::show();cout?MClass:H?no?"Score:M?score?endl;}};classteacher:publicperson(private:stringpro;stringdepartment;public:teacher(stringnum1,stringname1,stringp1,stringd1):person(num1,name1)(pro=pl;department=dl;)voidshow()(person::show();cout?',profession:',?pro?HDepartment:H?department?endl;));intmain()(stringnl,nul,nol;stringn2,nu2,p,d;doubles;cin?n1?nu1?no1?s;cin?n2?nu2?p?d;studentone(n1,nul,nol,s);one.show();teachertwo(n2,nu2,p,d);two.show();return0;24:長方形的面積和長方體的體積ProblemDescription先創(chuàng)建一個長方形類,有帶參的構(gòu)造函數(shù),能夠計算長方形的面積;在派生出長方體類,有帶參的構(gòu)造函數(shù),能夠計算長方體的表面積和體積。Input輸入數(shù)據(jù)有多組,每組包含2行數(shù)據(jù),第1行2個實數(shù),表示長方形的長和寬;第2行3個實數(shù),表示長方體的長'寬和高。Output輸出長方形的面積和長方體的表面積和體積;具體格式見樣例。SampleInput23456SampleOutput6148120#include<iostream>usingnamespacestd;classA(protected:doublel,w;public:A(double11=0.0,doublew1=0.0)(1=11;w=wl;)doubleSS()(returnl*w;)voidshow()cout?SS()?endl;classB:publicA(private:doubleh;public:B(double11=0.0,doublew1=0.0,doublehl=0.0):A(ll,wl)(h=hl;)doubleS()(doubles;s=2*SS()+2*l*h+2*w*h;returns;}doubleVV()(returnh*SS();)voidshow()(cout?S()?nM?VV()?endl;}};intmain(){doubleIl,wl,12,w2,h2;while(cin?ll?w1)(cin?12?w2?h2;Aone(ll,wl);one.show();Btwo(12,w2,h2);two.showQ;)return0;}25:多繼承——Time類和Date類派生出Birthtime類ProblemDescription定義ー個Time類,其數(shù)據(jù)成員包括時、分、秒,成員函數(shù)包括構(gòu)造函數(shù)和輸出函數(shù);定義一個Date類,其數(shù)據(jù)成員包括年、月、日,成員函數(shù)包括構(gòu)造函數(shù)和輸出函數(shù);定義一個派生類,它繼承類Time和Date,并且增加一個數(shù)據(jù)成員childname用于表示小孩的名字;設(shè)計主程序顯示孩子的姓名和出生時間。Input輸入數(shù)據(jù)有多行,每行包括一個字符串和六個整數(shù),分別表示姓名、出生年月日和出生時分秒。Output輸出有多行,對應(yīng)每個輸入輸出一行。輸出格式見SampleOutput.SampleInputJack201211101010Marry2011618030SampleOutputName:JackBirthday:2012/1/1Time:10:10:10Name:MarryBirthday:2011/6/1Time:8:0:30#include<iostream>#include<string>usingnamespacestd;classTIME(private:inthour,minute,second;public:TIME(inth,intm,ints){hour=h;minute=m;second=s;}voidshow()(cout?"Time:*,?hour?,,:M?minute?,,:,,?second?endl;)};classDATEprivate:intyear,month,day;public:DATE(inty,intml,intd)(year=y;month=m1;day=d;}voidshow(){cout?,,Birthday:,,?year?7',?month?,7,'?day?MM;)};classCHILD:publicTIME,publicDATE(private:stringchildname;public:CHILD(stringn,inty,intml,intd,inth,intm,ints):DATE(y,m1,d),TIME(h,m,s)(childname=n;)voidshow()(cout?',Name:',?childname?HH;DATE::show();TIME::show();));intmain()(stringn;inty,ml,d,h,m,s;while(cin?n?y?m1?d?h?m?s)(CHILDone(n,y,m1,d,h,m,s);one.show();)return0;)26:RoundTable類ProblemDescription定義并描述ー"t*Table類,ー個Circle類和一個RoundTable類,Table類包括長、寬、高基本信息,Circle類包含有半徑基本信息。RoundTable是ー個圓形桌子類。要求將相應(yīng)的類定義完整,并在主函數(shù)中定義ー個RoundTable的對象,輸出該對象的所有信息。Input輸入包括多組測試數(shù)據(jù)Output每組測試數(shù)據(jù)輸出兩行SampleInput123410.5610.6SampleOutputTable*slong,width,heightare:123Table1sradiusis:4Table*slong,width,heightare:10.561Table*sradiusis:0.6Hints思考:圓桌與圓形、桌子之間是繼承還是組合的關(guān)系?#include<iostream>#include<string>usingnamespacestd;classTable]private:doublel,w,h;public:Table(double11,doublew1,doubleh1)(l=ll;w=wl;h=hl;)voidshow()

coutvv"Table'slong,width,heightare:'^?l?,^M?w?MM?h?endl;classCircle(private:doubler;public:Circle(doublerl)(r=rl;)voidshow()(coutvぐ'Table'sradiusis:"?r?endl;));classRoundTable:publicTable,publicCircle(public:RoundTable(double11,doublew1,doubleh1,doubler1):Table(l1,w1,h1),Circle(r1)()voidshow()(Table::show();Circle::show();)};intmain()(doublell,wl,hl,rl;while(cin?ll?wl?hl?rl)(RoundTableone(ll,wl,hl,rl);one.show();)return0;)#include<iostream>#include<iomanip>usingnamespacestd;classTableprivate:doubleL,W,H;public:Table(double1=0.0,doublew=0.0,doubleh=0.0){L=l;W=w;H=h;}voidshow(){cout?"Table*slong,width,heightare:,,?L?n,,?W?Hn?H

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論