c++題庫(kù)編程題_第1頁(yè)
c++題庫(kù)編程題_第2頁(yè)
c++題庫(kù)編程題_第3頁(yè)
c++題庫(kù)編程題_第4頁(yè)
c++題庫(kù)編程題_第5頁(yè)
已閱讀5頁(yè),還剩19頁(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)介

1、/2-5#include<iostream>#include<string>#include<math.h>usingnamespacestd;classTriangleinta,b,c;public:doubleArea()doubles=(a+b+c)/2;returnsqrt(s*(s-a)*(s-b)*(s-c);intPerimeter()returna+b+c;voidInput()cin>>a>>b>>c;voidmain()Trianglet1;t1.Input();cout<<t1.Area(

2、)<<""<<t1.Perimeter();/2-3#include<iostream.h>inlineintmax(inta,intb)if(a>b)returna;elsereturnb;inlineintmax(inta,intb,intc)returnmax(a,max(b,c);voidmain()intx=21,y=15,z=22,d;d=max(x,max(y,z);cout<<d;/2-4#include<iostream.h>intoverload(intn);charoverload(ch

3、arn);voidmain()intn;chars;cout<<"輸入5"cin>>n;cout<<"n="<<overload(n)<<endl;cout<<"輸入a"<<endl;cin>>s;cout<<"a="cout<<overload(s)<<endl;intoverload(intn)returnn;charoverload(charn)cout<<"

4、;b"<<endl;return0;/2-2#include"iostream"usingnamespacestd;inlinechartrans(charch);intmain()charch;while(ch=getchar()!='n')cout<<trans(ch);cout<<endl;return0;inlinechartrans(charch)if(ch>='a'&&ch<='z')returnch-32;elsereturnch+32;/

5、3-1#include<iostream>usingstd:cout;usingstd:endl;intMin(inta,intb)return(a<b?a:b);intMin(inta,intb,intc)return(Min(a,b)<c?Min(a,b):c);intMin(inta,intb,intc,intd)return(Min(a,b,c)<d?Min(a,b,c):d);voidmain(void)cout<<"MIN(1,2)="<<Min(1,2)<<endl;cout<<&q

6、uot;MIN(3,4,5)="<<Min(3,4,5)<<endl;cout<<"MIN(4,5,6,7)="<<Min(4,5,6,7)<<endl;/3-2#include<iostream>usingnamespacestd;voidArea(floatr)cout<<"thecircle'sareais:"<<3.14*r*r<<endl;voidArea(floata,floatb)cout<<"

7、therectangle's(square's)areais:"<<a*b<<endl;voidArea(floata,floatb,floath)cout<<"thetrapzoid'sareais:"<<(a+b)*h/2<<endl;voidmain()Area(5);Area(3,3);Area(3,5,4);/3-3#include<iostream>usingnamespacestd;voidSort(inta,intn)boolflag;for(inti=

8、0;i<n-1;i+)flag=false;for(intj=1;j<n-i;j+)if(aj-1>aj)inttemp=aj-1;aj-1=aj;aj=temp;flag=true;if(!flag)break;for(intm=0;m<n;m+)cout<<am<<''cout<<endl;voidSort(floatb,intn)boolflag;for(inti=0;i<n-1;i+)flag=false;for(intj=1;j<n-i;j+)if(bj-1>bj)floattemp=bj-

9、1;bj-1=bj;bj=temp;flag=true;if(!flag)break;for(intm=0;m<n;m+)cout<<bm<<''cout<<endl;voidmain()inta10=3,5,1,2,9,0,8,6,4,7;floatb10=0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0;Sort(a,10);Sort(b,10);/3-4#include<iostream>usingnamespacestd;classRectanglefloata,b;public:Rec

10、tangle()cout<<"inputthetwosideoftherectangle:"<<endl;cin>>a>>b;Rectangle(floatx,floaty):a(x),b(y)floatCir()return(2*(a+b);floatArea()return(a*b);voidmain()RectangleRe;cout<<"therectangle'scircumferenceis:"<<Re.Cir()<<endl<<"

11、;therectangle'sareais:"<<Re.Area()<<endl;/3-5#include<iostream>usingnamespacestd;classCirclefloatr;public:Circle()cout<<"inputtheradiusofthecircle:"<<endl;cin>>r;Circle(floatx):r(x)floatCir()return2*3.14*r;floatArea()return3.14*r*r;voidmain()Cir

12、cleC(5);cout<<"theCircle'scircumferenceis:"<<C.Cir()<<endl<<"theCircle'sAreais:"<<C.Area()<<endl;/4-1classstudentintno;charname20;public:student();intgetno()const;char*getname();#include<string.h>student:student()no=1;strcpy(name,

13、"wang");intstudent:getno()constreturnno;char*student:getname()returnname;intmain()students1;s1.getno();s1.getname();conststudents2;s2.getno();return0;/4-3#include<iostream.h>classobjectfloata;floatweight;public:object(floatw);object()cout<<"destructweight"<<endl

14、;classbox:publicobjectfloatb;floatheight,widt;public:box(floatw,floath,floatwi):object(w),height(h),widt(wi)cout<<"constructheight"<<endl;cout<<"constructwidt"<<endl;box()cout<<"destructheight"<<endl;cout<<"destructwidt&quo

15、t;<<endl;object:object(floatw)weight=w;cout<<"constructweight"<<endl;intmain()boxb(1.1,2.2,3.3);return0;/4-4#include"iostream"usingnamespacestd;classbaseclasspublic:baseclass()cout<<"ac"<<endl;virtualbaseclass()cout<<"ad"<

16、;<endl;classderivedclass:publicbaseclasspublic:derivedclass()cout<<"bc"<<endl;derivedclass()cout<<"bd"<<endl;intmain()derivedclassobj;baseclass*p;p=&obj;return0;/4-5#include"iostream"#include<string.h>usingnamespacestd;classDoumentch

17、ar*name;public:Doument(char*na)name=newcharstrlen(na)+1;strcpy(name,na);virtualvoidprint()constcout<<"姓名:"<<name<<endl;classBook:publicDoumentintpagecount;char*name;public:Book(intp,char*na):Doument(na)p=pagecount;name=newcharstrlen(na)+1;strcpy(name,na);voidprint()constc

18、out<<"頁(yè)碼:"<<pagecount<<"姓名:"<<name<<endl;voiddisp(Doument&i)i.print();intmain()Bookb(23,"小明");disp(b);return0;/5-1#include"iostream"usingnamespacestd;classpointprivate:intx;inty;intz;staticintnum;public:point(inta,intb,intc);i

19、ntprint();intpoint:num=0;intmain()pointp1(1,2,3);p1.print();pointp2(5,3,2);p1.print();pointp3(3,6,4);p1.print();return0;point:point(inta,intb,intc)x=a;y=b;z=c;num+;intpoint:print()cout<<"第"<<num<<"個(gè)點(diǎn)"<<x<<","<<y<<","&l

20、t;<z<<endl;return0;/5-2#include<iostream>#include<cstring>usingnamespacestd;classStucharname15;intage;floatscore;public:staticintcount;staticfloatsum;Stu(char*p,inta,floats):age(a),score(s)strcpy(name,p);count+;sum+=score;Stu()cout<<"inputthestudent'sname,age,scor

21、e:"<<endl;cin>>name>>age>>score;count+;sum+=score;intStu:count=0;floatStu:sum=0;voidmain()Stup("子機(jī)",19,98),q;cout<<"thenumberofstudentsis:"<<Stu:count<<endl<<"thetotalscoreis:"<<Stu:sum<<endl;/5-3#include&

22、lt;iostream>usingnamespacestd;classPointfloatx,y,z;public:Point(floata=0,floatb=0,floatc=0):x(a),y(b),z(c)Pointoperator+();Pointoperator-();Pointoperator+(int);Pointoperator-(int);voidshow()cout<<"(x,y,z)="<<"("<<x<<","<<y<<",

23、"<<z<<")"<<endl;PointPoint:operator+()returnPoint(+x,+y,+z);PointPoint:operator-()returnPoint(-x,-y,-z);PointPoint:operator+(int)Pointtemp;temp.x=x+;temp.y=y+;temp.z=z+;returntemp;PointPoint:operator-(int)Pointtemp;temp.x=x-;temp.y=y-;temp.z=z-;returntemp;voidmain()P

24、ointa(1,1,1),b(2,2,2),c,d;cout<<"a:"a.show();cout<<"b:"b.show();cout<<endl;c=a+;cout<<"c=a+n"cout<<"c:"c.show();cout<<endl;d=+b;cout<<"d=+bn"cout<<"d:"d.show();cout<<"Newaandb:n&quo

25、t;a.show();b.show();cout<<endl;c=a-;cout<<"c=a-n"cout<<"c:"c.show();cout<<endl;d=-b;cout<<"d=-bn"cout<<"d:"d.show();cout<<"Newaandb:n"a.show();b.show();/5-4#include<iostream>usingnamespacestd;classCompl

26、exfloatreal,image;public:Complex(floata,floatb):real(a),image(b)Complex()friendbooloperator=(Complex&x,Complex&y)return(x.real=y.real&&x.image=y.image);voidmain()ComplexCp1(1,2),Cp2(3,4),Cp3(1,2);cout<<(Cp1=Cp2)<<endl<<(Cp1=Cp3)<<endl;#include<iostream>u

27、singnamespacestd;classComplexfloatreal,image;public:Complex(floatx,floaty):real(x),image(y)Complex()Complexoperator+(Complex&a)returnComplex(real+a.real,image+a.image);Complexoperator-(Complex&b)returnComplex(real-b.real,image-b.image);friendostream&operator<<(ostream&output,Co

28、mplex&c)output<<'('<<c.real<<','<<c.image<<')'returnoutput;voidmain()ComplexCp1(1,-2),Cp2(2,5),Cp3,Cp4;Cp3=Cp1+Cp2;Cp4=Cp1-Cp2;cout<<Cp1<<'+'<<Cp2<<'='<<Cp3<<endl<<Cp1<<'-

29、9;<<Cp2<<'='<<Cp4<<endl;/6-2#include<iostream>usingnamespacestd;classcomputerfloatx,y;public:computer(floata=0,floatb=0):x(a),y(b)computeroperator+();computeroperator-();computeroperator+(int);computeroperator-(int);voidshow()cout<<"(x,y)="<&l

30、t;"("<<x<<","<<y<<")"<<endl;computercomputer:operator+()returncomputer(+x,+y);computercomputer:operator-()returncomputer(-x,-y);computercomputer:operator+(int)computertemp;temp.x=x+;temp.y=y+;returntemp;computercomputer:operator-(int)comput

31、ertemp;temp.x=x-;temp.y=y-;returntemp;voidmain()computera(1,1),b(2,2),c,d;cout<<"a:"a.show();cout<<"b:"b.show();cout<<endl;c=a+;cout<<"c=a+n"cout<<"c:"c.show();cout<<endl;d=+b;cout<<"d=+bn"cout<<"d

32、:"d.show();cout<<"Newaandb:n"a.show();b.show();cout<<endl;c=a-;cout<<"c=a-n"cout<<"c:"c.show();cout<<endl;d=-b;cout<<"d=-bn"cout<<"d:"d.show();cout<<"Newaandb:n"a.show();b.show();/6-3#inc

33、lude<iostream.h>#include<string.h>classstringchar*xstring;public:string(char*str);string();string&operator=(conststring&);voidprint();string:string(char*str)xstring=newcharstrlen(str)+1;strcpy(xstring,str);string:string()deletexstring;voidstring:print()cout<<xstring<<

34、endl;intmain()strings1("hello");strings2("yangchenhuan");s2.print();s2=s1;cout<<"修改后:"s2.print();return0;string&string:operator=(conststring&s)if(this=&s)return*this;deletexstring;xstring=newcharstrlen(s.xstring)+1;strcpy(xstring,s.xstring);return*this

35、;/6-4#include<iostream>#include<string>usingnamespacestd;classStringpublic:String()p=NULL;String(char*str);friendbooloperator=(String&string1,String&string2);voiddisplay();private:char*p;String:String(char*str)p=str;voidString:display()cout<<p;/輸出P所指向的字符串booloperator=(Strin

36、g&string1,String&string2)if(strcmp(string1.p,string2.p)=0)returntrue;elsereturnfalse;voidcompare(String&string1,String&string2)if(operator=(string1,string2)=0)string1.display();cout<<"="string2.display();elsecout<<"error!"<<endl;intmain()Stringst

37、ring1("Hello"),string2("china");compare(string1,string2);return0;/6-5#include<cstring>#include<iostream>usingnamespacestd;classStringchar*p;public:String()p=NULL;String(constchar*s)p=newchar(strlen(s)+1);strcpy(p,s);char*operator+(constchar*s)char*temp=newchar(strlen(

38、p)+strlen(s)+1);strcpy(temp,p);strcat(temp,s);returntemp;friendbooloperator=(constString&a,constString&b)return(!strcmp(a.p,b.p);friendostream&operator<<(ostream&output,constString&d)output<<d.p;returnoutput;voidmain()StringStr1("Malagebi!"),Str2("Ready

39、,Go!"),Str3;Str3=Str1+"nishi"cout<<Str3<<endl<<(Str1=Str2)<<endl;#include<iostream>usingnamespacestd;classCarintwnum;floatweight;public:Car(inta,floatb):wnum(a),weight(b)voidCshow()cout<<"輪子個(gè)數(shù):"<<wnum<<"個(gè)"<<endl

40、<<"車重:"<<weight<<"T"<<endl;classSmallCar:privateCarints_cnum;public:SmallCar(inta,floatb,intc):Car(a,b),s_cnum(c)voidSshow()Cshow();cout<<"載客量:"<<s_cnum<<"人次"<<endl;classBigCar:privateCarintb_cnum;floatb_cweight;

41、public:BigCar(inta,floatb,intc,floatd):Car(a,b),b_cnum(c),b_cweight(d)voidBshow()Cshow();cout<<"載客量:"<<b_cnum<<"人次"<<endl<<"載重量"<<b_cweight<<"T"<<endl;intmain()Carcar(4,8);SmallCarsmallcar(8,16,8);BigCarbigcar(16

42、,32,16,32);cout<<"汽車:"<<endl;"<<endl;"<<endl;car.Cshow();cout<<"n小車:smallcar.Sshow();cout<<"n卡車:bigcar.Bshow();return0;/7-2#include<iostream>#include<cmath>usingnamespacestd;classShapefloatx,y;public:Shape(floata,floatb):x

43、(a),y(b)voidshow()cout<<"左上角坐標(biāo)(x,y)=("<<x<<','<<y<<")n"virtualfloatGetArea()=0;classRec:virtualpublicShapefloathigh,width;public:Rec(floata,floatb,floatc,floatd):Shape(a,b),high(c),width(d)voidshow()cout<<"矩形:n"Shape:show();co

44、ut<<"矩形寬、高:"<<width<<''<<high<<endl;floatGetArea()show();returnhigh*width;classEll:virtualpublicShapefloatxx,yy;public:Ell(floata,floatb,floatc,floatd):Shape(a,b),xx(c),yy(d)voidshow()cout<<"橢圓:n"Shape:show();cout<<"橢圓x軸,y軸:&

45、quot;<<xx<<''<<yy<<endl;floatGetArea()show();return4*atan(1)*xx*yy;intmain()Recrec(5,5,5,5);Ellell(8,8,12,10);Shape*s;s=&rec;cout<<"矩形的面積:"<<s->GetArea()<<endl<<endl;s=&ell;cout<<"橢圓的面積:"<<s->GetAre

46、a()<<endl<<endl;return0;/7-3#include<iostream>/#include<string>usingnamespacestd;classmammalintm;public:mammal(inti=0):m(i)cout<<"mammalisconstruct"<<endl;mammal()cout<<"mammalisdestruct"<<endl;classcat:publicmammalintc;public:cat(i

47、ntj=0):c(j)cout<<"catisconstruct"<<endl;cat()cout<<"catisdestruct"<<endl;classdog:publicmammalintd;public:dog(intk=0):d(k)cout<<"dogisconstruct"<<endl;dog()cout<<"dogisdestruct"<<endl;intmain()catc;dogd;return0;/

48、7-4p39.7/8-1#include<iostream>usingnamespacestd;template<classT1,classT2>doublesum(T1a,T2b)return(a+b);voidmain()cout<<sum(1,3)<<endl;cout<<sum(1.2,3)<<endl;cout<<sum(1,3.1)<<endl;cout<<sum(1.2,3.34)<<endl;/8-2P課本179/8-3#include<iostream>usingnamespacestd;constintM=100;template<classT>classArrayTarrayM;intlen;public:Array();voidsort();voidsearch()cout<<"thenumyouwanttosearchis:"Tx;cin>>x;for(inti=0;i<len;i+)if(x=arrayi)cout<&l

溫馨提示

  • 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)論