面向?qū)ο笤O(shè)計(jì)程序運(yùn)行結(jié)果復(fù)習(xí)題_第1頁(yè)
面向?qū)ο笤O(shè)計(jì)程序運(yùn)行結(jié)果復(fù)習(xí)題_第2頁(yè)
面向?qū)ο笤O(shè)計(jì)程序運(yùn)行結(jié)果復(fù)習(xí)題_第3頁(yè)
面向?qū)ο笤O(shè)計(jì)程序運(yùn)行結(jié)果復(fù)習(xí)題_第4頁(yè)
面向?qū)ο笤O(shè)計(jì)程序運(yùn)行結(jié)果復(fù)習(xí)題_第5頁(yè)
已閱讀5頁(yè),還剩22頁(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、面向?qū)ο蟪绦蛟O(shè)計(jì)期末綜合練習(xí)四(運(yùn)行結(jié)果)寫出程序運(yùn)行結(jié)果 1. #include #include void main() int a8=25,48,32,85,64,18,48,29; int max,min; max=min=a0; for(int i=0; iai) max=ai; if(xai) min=ai; coutmax:maxendl; coutmin:minendl; 2. #include void main() int a,b; for(a=1,b=2; b50;) couta b ; a=a+b; b=a+b; coutendl; couta b endl; 1 2

2、3 5 8 13 21 34 55 89 3. #include const int M=3, N=4; void main() int i,j,s=0; for(i=1;i=M;i+) for(j=1;j=N;j+) s+=i*j; cout”s=”sendl; s=60 4. #include void main() int a=2,b=5,c=0; if(a+b10) c=a*b; else c=3*a+b; if(c=20) coutc*c; else cout4+c-5; coutendl; a=a+b; b=a+b;c+=a+b; couta,b,c=a,b,cendl; 121

3、a,b,c=7,12,30 5. #include void main() int x=5; switch(2*x-3) case 4: coutx ; case 7: cout2*x+1 ; case 10: cout3*x-1 ; break; default: coutdefaultendl; coutswitch end.endl; 11 14 switch end. 6. #include #include int a4=36,-5,73,8; void main() int i,y; for(i=0; i4; i+) if(ai0) y=1; else if(ai10) y= ai

4、* ai+3; else if(ai60) y=4*ai-5; else y=int(sqrt(ai); / sqrt(x)為取x的平方根函數(shù) coutsetw(5)aisetw(5)y; 7. #include int a8=36,25,20,43,12,70,66,35; void main() int s0,s1,s2; s0=s1=s2=0; for(int i=0; i8; i+) switch(ai%3) case 0: s0+=ai;break; case 1: s1+=ai;break; case 2: s2+=ai;break; couts0 s1 s2endl; 8. #i

5、nclude const int N=5; void main() int i,p=1,s=0; for(i=1;iN; i+) p=p*i; s=s+p; coutsetw(5)isetw(5)p; coutsetw(5)sendl; 9. #include const int M=20; void main() int c2,c3,c5; c2=c3=c5=0; for(int i=1; i=M; i+) if(i%2=0) c2+; if(i%3=0) c3+; if(i%5=0) c5+; coutc2 c3 c5endl; 10. #include void main() int i

6、,j; for(i=0;i5;i+) for(j=i;j5;j+) cout*; coutendl; 11. #include void main() for(int i=1,s=0;i20;i+) if(i%2=0 | i%3=0) continue; couti ; s+=i; coutsendl; 12. #include const int T=6; void main() int i,j; for(i=1;i=T;i+=2) for(j=2;j=T;j+=2) if(i+jT) cout+; else cout*; 13. #include void main() int a,b,c

7、=0; for(a=1;a1;b-=2) if(a+b)%2=0) c+=a+b; else c+=a*b; if(b=2) couta b cendl; 14. #include const int B=2; void main() int p=1,s=1; while(s50) p*=B; s+=p; couts=sendl; 15. #include void main() int x=24,y=88; int i=2,p=1; do while(x%i=0 & y%i=0) p*=i; x/=i; y/=i; i+; while(x=i & y=i); coutp*x*yendl; 1

8、6. #include const int N=3; void main() int aNN=7,-5,3,2,8,-6,1,-4,-2; int bNN=3,6,-9,2,-8,3,5,-2,-7; int i,j,cNN; for(i=0;iN;i+) /計(jì)算矩陣C for(j=0;jN;j+) cij=aij+bij; for(i=0;iN;i+) /輸出矩陣C for(j=0;jN;j+) coutsetw(5)cij; coutendl; 17. #include int a=5; void main() int b=a+20; int a=10; couta bendl; int

9、a=0,b=0; for(int i=1; i6; i+) a+=i; b+=a; couta bendl; couta bendl; 18. #include int f1(int x, int y) x=x+y; y=x+y; coutx=x, y=yendl; return x+y; void main() int x=5,y=8; int z=f1(x,y); coutx=x, y=y; cout, z=zendl; 19. #include void f2(int& x, int& y) int z=x; x=y; y=z; void f3(int* x, int* y) int z

10、=*x; *x=*y; *y=z; void main() int x=10,y=26; coutx,y=x, yendl; f2(x,y); coutx,y=x, yendl; f3(&x,&y); coutx,y=x, yendl; x+; y-; f2(y,x); coutx,y=x, yendl; 20. #include void f4(int a, int n, int& s) s=0; for(int i=0; in; i+) s+=ai; void main() int b8=4,8,6,9,2,10,7,12; int x; f4(b,5,x); coutx ; int y;

11、 f4(b+3,4,y); couty ; coutx+yendl; 21. #include void main() int a8=36,25,48,14,55,40,50,24; int b1, b2; b1=b2=a0; for(int i=1;ib1) b2=b1; b1=ai; else if(aib2) b2=ai; coutb1 b2endl; 22. #include void main() char a=abcdabcabfgacd; int i1=0, i2=0, i=0; while(ai) if(ai=a) i1+; if(ai=b) i2+; i+; couti1 i

12、2 iendl; 23. #include void main() int a10=76,83,54,62,40,75,90,92,77,84; int b4=60,70,90,101; int c4=0; for(int i=0;i=bj) j+; cj+; for(i=0;i4;i+) coutci ; coutendl; 24. #include #include void main() char a510=student,worker,soldier,cadre,peasant; char s110, s210; strcpy(s1,a0); strcpy(s2,a0); for(in

13、t i=1;i0) strcpy(s1,ai); if(strcmp(ai, s2)0) strcpy(s2,ai); couts1 s2endl; worker cadre 25. #include const int N=5; void fun(); void main() for(int i=1; iN; i+) fun(); void fun() static int a; int b=2; cout(a+=3,a+b) ; 5 8 11 14 26. #include void main() char s35=1234,abcd,+-*/; char *p3; for(int I=0

14、;I=0;I-) coutpI ; coutendl; +-*/ abcd 1234 27. #include void main() int i,j,len3; char a38=year,month,day; for(i=0;i3;i+) for(j=0;j8;j+) if(aij=0) leni=j;break; coutai:leniendl; year:4 month:5day:3 28. #include #include class CD char* a; int b; public: void Init(char* aa, int bb) a=new charstrlen(aa

15、)+1; strcpy(a,aa); b=bb; char* Geta() return a; int Getb() return b; void Output() couta bendl; dx;void main()CD dy;dx.Init(abcdef,30);dy.Init(shenyafen,3*dx.Getb()+5);dx.Output();dy.Output();abcdef 30 shenyafen 95 29#include #include class CD char* a; int b; public: void Init(char* aa, int bb) a=ne

16、w charstrlen(aa)+1; strcpy(a,aa); b=bb; char* Geta() return a; int Getb() return b; void Output() couta bendl; ;void main()CD dx,dy;char a20;dx.Init(abcdef,30);strcpy(a,dx.Geta();strcat(a,xyz);dy.Init(a,dx.Getb()+20);dx.Output();dy.Output(); 30. #include class CE private: int a,b; int getmax() retur

17、n (ab? a:b); public: int c; void SetValue(int x1,int x2, int x3) a=x1; b=x2; c=x3; int GetMax(); ;int CE:GetMax() int d=getmax();return (dc? d:c);void main()int x=5,y=12,z=8;CE ex, *ep=&ex;ex.SetValue(x,y,z);coutex.GetMax()SetValue(x+y,y-z,20);coutGetMax()endl; 31. #include class CE private: int a,b

18、; int getmin() return (ab? a:b); public: int c; void SetValue(int x1,int x2, int x3) a=x1; b=x2; c=x3; int GetMin(); ;int CE:GetMin() int d=getmin();return (dSetValue(x+y,y-z,10);coutGetMin()endl;CE a=*ep;couta.GetMin()*3+15endl; 32. #include class Franction /定義分?jǐn)?shù)類 int nume; /定義分子 int deno; /定義分母 pu

19、blic: /把*this化簡(jiǎn)為最簡(jiǎn)分?jǐn)?shù),具體定義在另外文件中實(shí)現(xiàn)void FranSimp(); /返回兩個(gè)分?jǐn)?shù)*this和x之和,具體定義在另外文件中實(shí)現(xiàn)Franction FranAdd(const Franction& x); /置分?jǐn)?shù)的分子和分母分別0和1 void InitFranction() nume=0; deno=1; /置分?jǐn)?shù)的分子和分母分別n和d void InitFranction(int n, int d) nume=n; deno=d; /輸出一個(gè)分?jǐn)?shù)void FranOutput() coutnume/denoendl; ;void main() Francti

20、on a,b,c,d; a.InitFranction(7,12); b.InitFranction(-3,8); c.InitFranction(); c=a.FranAdd(b); d=c.FranAdd(a);couta: ; a.FranOutput(); coutb: ; b.FranOutput(); coutc: ; c.FranOutput(); coutd: ; d.FranOutput(); 33. #include class Franction /定義分?jǐn)?shù)類 int nume; /定義分子 int deno; /定義分母 public: /把*this化簡(jiǎn)為最簡(jiǎn)分?jǐn)?shù),具

21、體定義在另外文件中實(shí)現(xiàn)void FranSimp(); /返回兩個(gè)分?jǐn)?shù)*this和x之和,具體定義在另外文件中實(shí)現(xiàn)Franction FranAdd(const Franction& x); /置分?jǐn)?shù)的分子和分母分別0和1 void InitFranction() nume=0; deno=1; /置分?jǐn)?shù)的分子和分母分別n和d void InitFranction(int n, int d) nume=n; deno=d; /輸出一個(gè)分?jǐn)?shù)void FranOutput() coutnume/denoendl; ;void main() Franction a,b,c,d; a.InitFran

22、ction(6,15); b.InitFranction(3,10); c.InitFranction(); c=a.FranAdd(b); d=c.FranAdd(a);couta: ; a.FranOutput(); coutb: ; b.FranOutput(); coutc: ; c.FranOutput(); coutd: ; d.FranOutput(); 34. #include#includeclass A char *a;public: A(char *s) a=new charstrlen(s)+1; strcpy(a,s); coutaendl; A() delete a

23、; coutDestructor!endl; ;void main() A x(xuxiaokai); A *y=new A(weirong); delete y; 35. #includeclass A int *a;public: A(int x=0):a(new int(x) A() delete a; int getA() return *a; void setA(int x) *a=x;void main() A x1,x2(3); A *p=&x2;p-setA(x2.getA()+5); x1.setA(15+x1.getA();coutx1.getA() x2.getA()en

24、dl; 36. #includeclass A int a;public: A(int aa=0): a(aa) couta ; A() coutXxk;void main() A *p; A x3=1,2,3,y=4;coutendl;p=new A3; coutendl;delete p;coutendl; 37. #includeclass A int a,b;public: A() a=b=0; A(int aa, int bb) a=aa; b=bb;int Sum() return a+b;int* Mult() int *p=new int(a*b);return p;void

25、main() int *k;A x(2,3), *p; p=new A(4,5); coutx.Sum() *(x.Mult()endl; coutSum() Mult()endl; delete k; 38. #includeclass A int a10; int n; public: A(int aa, int nn): n(nn) for(int i=0; in; i+) ai=aai; int Get(int i) return ai;int SumA(int n) int s=0;for(int j=0; jn; j+) s+=aj;return s;void main() int

26、 a=2,5,8,10,15,20; A x(a,4); A y(a,6); int d=1;for(int i=0; i4; i+) d*=x.Get(i); int f=y.SumA(5);coutd=dendl;coutf=fendl; 39. #includeclass A int a,b; public: A(int aa, int bb) a=aa; b=bb; float Multip(char op) switch(op) case +: return a+b; case -: return a-b; case *: return a*b; case /: if(b!=0)re

27、turn float(a)/b;else cout除數(shù)為0!endl; return 0; default: coutnop非法運(yùn)算符!endl; return 0; ;void main() A x(10,4); char a6=+-*/; int i=0; while(ai) float k=x.Multip(ai);if(k!=0) coutk ;i+; coutendl; 40. #include class Point int x,y;public:Point(int x1=0, int y1=0) :x(x1), y(y1) coutPoint:x yn;Point() coutP

28、oint destructor!n;class Circle Point center;/圓心位置 int radius; /半徑public:Circle(int cx,int cy, int r):center(cx,cy),radius(r) coutCircle radius:radiusn;Circle() coutCircle destructor!n;void main() Circle c(3,4,5); 41. #include #include class Point int x,y;public:Point(int x1=0, int y1=0) :x(x1), y(y1

29、) coutPoint:x yn;Point() coutPoint des!n;class Text char text100;/文字內(nèi)容public:Text(char * str) strcpy(text,str); coutText con!n;Text()coutText des!n;class CircleWithText : public Point,public Text public:CircleWithText(int cx,int cy, char *msg): Point(cx,cy),Text(msg) coutPoint with Text con!n;Circle

30、WithText() coutPoint with Text desn;void main()CircleWithText cm(3,4,hello); 42. #include class Date public: void SetDate(int y,int m,int d) Year=y; Month=m; Day=d; void PrintDate() coutYear/Month/Dayendl; Date()SetDate(2000,1,1); Date(int y,int m,int d)SetDate(y,m,d); protected: int Year,Month,Day;

31、class Time public: void SetTime(int h,int m,int s) Houre=h; Minutes=m; Seconds=s; void PrintTime() coutHoure:Minutes:Secondsendl; Time()SetTime(0,0,0); Time(int h,int m,int s)SetTime(h,m,s); protected: int Houre, Minutes, Seconds;class Date_Time: public Date, public Time public: Date_Time( ):Date(),

32、Time(); Date_Time(int y,int mo,int d,int h,int mi,int s): Date(y,mo,d), Time(h,mi,s) void PrintDate_Time()PrintDate();PrintTime();void main( ) Date_Time dt_a, dt_b(2002,10,1,6,0,0); dt_a.PrintDate_Time(); dt_b.SetTime(23,59,59); dt_b.PrintDate_Time(); dt_a.SetDate(2002,12,31); dt_a.PrintDate_Time();

33、 43. #include class Date public: Date(int y=2001,int m=1,int d=1)Year=y; Month=m; Day=d; void PrintDate() coutYear/Month/Dayendl; protected: int Year,Month,Day;class Time public: Time(int h=5,int m=30,int s=0)Houre=h; Minutes=m; Seconds=s; void PrintTime() coutHoure:Minutes:Secondsendl; protected: int Houre, Minutes, Seconds;class Date_Time: public Date, public Time public: Date_Time( ); Date_Time(int y,int mo,int d,int h=0,int mi=0,int s=0): Date(y,mo,d), Time(h,mi,s) void PrintDate_Time()PrintDate();PrintTime();void main( ) Date_Time a, b(2002,10,1,6,20,0), c(2003,3,8,6,7); a.PrintDa

溫馨提示

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