C++課后習(xí)題解答_第1頁
C++課后習(xí)題解答_第2頁
C++課后習(xí)題解答_第3頁
C++課后習(xí)題解答_第4頁
C++課后習(xí)題解答_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、 C+課后習(xí)題程序3-14.用遞歸的方法編寫程序求N階勒讓德多項(xiàng)式的值,在主程序中實(shí)現(xiàn)輸入輸出。遞歸公式為 1 (n=0) Pn(x)= x (n=1) (2n-1)x*Pn-1(x)-(n-1)Pn-2(x)/n (n>1)#include<iostream>using namespace std; long p(int x,int y) /Legendre polynomials函數(shù)定義 if(y=0) return 1; else if(y=1) return x; else return (2*y-1)*x*p(x,y-1)-(y-1)*p(x,y-2)/y); vo

2、id main() int a; /legendre polynomials函數(shù)中x的值 int n; /級(jí)數(shù)n cout<<"請(qǐng)輸入legendre polynomials函數(shù)中x的值:n" cin>>a; cout<<"請(qǐng)輸入所要求的多項(xiàng)式級(jí)數(shù):n" cin>>n; p(a,n); cout<<"n階勒讓德多項(xiàng)式的值為(當(dāng)x="<<a<<"時(shí)):n" cout<<p(a,n)<<endl; /輸出多項(xiàng)式的

3、值 4-8 定義一個(gè)Dog 類,包含的age、weight等屬性,以及對(duì)這些屬性操作的方法。實(shí)現(xiàn)并測(cè)試這個(gè)類。 (第一個(gè)程序待調(diào)試)?#include <iostream.h>class Dog public:Dog (int initialAge = 0, int initialWeight = 5);Dog();int GetAge() return itsAge; / inline!void SetAge (int age) itsAge = age; / inline!int GetWeight() return itsWeight; / inline!void SetWe

4、ight (int weight) itsAge = weight; / inline!private:int itsAge, itsWeight;Dog:Dog(int initialAge, int initialWeight)itsAge = initialAge;itsWeight = initialWeight;Dog:Dog() /destructor, takes no actionint main() Dog Jack(2,10);cout << "Jack is a Dog who is " ;cout << Jack.GetAge

5、() << " years old and"cout << Jack.GetWeight() << " pounds weight.n"Jack.SetAge(7);Jack.SetWeight(20);cout << "Now Jack is " ;cout << Jack.GetAge() << " years old and"cout << Jack.GetWeight() << " pounds wei

6、ght."return 0;程序二:#include <cstdlib> #include<iostream> using namespace std; class dog private: int m_age; int m_weight; public: dog():m_age(5),m_weight(10) void setAge(int i)m_age=i; void setWeight(int i)m_weight=i; int age()return m_age; int weight()return m_weight; ; int main() d

7、og d; cout<<d.age()<<" "<<d.weight()<<endl; return 0; 4-9 設(shè)計(jì)并測(cè)試一個(gè)名為Rectangle的矩形類,其屬性為矩形的左下角與右上角兩個(gè)點(diǎn)的坐標(biāo),能計(jì)算矩形的面積。#include <iostream.h>class Rectangle public:Rectangle (int top, int left, int bottom, int right);Rectangle () int GetTop() const return itsTop; int G

8、etLeft() const return itsLeft; int GetBottom() const return itsBottom; int GetRight() const return itsRight; void SetTop(int top) itsTop = top; void SetLeft (int left) itsLeft = left; void SetBottom (int bottom) itsBottom = bottom; void SetRight (int right) itsRight = right; int GetArea() const;priv

9、ate:int itsTop;int itsLeft;int itsBottom;int itsRight;Rectangle:Rectangle(int top, int left, int bottom, int right) itsTop = top;itsLeft = left;itsBottom = bottom;itsRight = right;int Rectangle:GetArea() const int Width = itsRight-itsLeft;int Height = itsTop - itsBottom;return (Width * Height);int m

10、ain() Rectangle MyRectangle (100, 20, 50, 80 );int Area = MyRectangle.GetArea();cout << "Area: " << Area << "n"return 0;4-11 定義一個(gè)矩形類,有長(zhǎng)、寬兩個(gè)屬性,有成員函數(shù)計(jì)算矩形的面積 #include <iostream.h>class Rectangle public:Rectangle(float len, float width)Length = len;Width = wid

11、th;Rectangle();float GetArea() return Length * Width; float GetLength() return Length; float GetWidth() return Width; private:float Length;float Width;void main() float length, width;cout << "請(qǐng)輸入矩形的長(zhǎng)度:"cin >> length;cout << "請(qǐng)輸入矩形的寬度:"cin >> width;Rectangl

12、e r(length, width);cout << "長(zhǎng)為" << length << "寬為" << width << "的矩形的面積為:" << r.GetArea () << endl;4-12 定義一個(gè)"數(shù)據(jù)類型" datatype類,能處理包含字符型、整型、浮點(diǎn)型三種類型的數(shù)據(jù),給出其構(gòu)造函數(shù)。 #include <iostream.h>class datatypeenumcharacter,integer,

13、floating_point vartype;union char c;int i;float f;public:datatype(char ch) vartype = character;c = ch;datatype(int ii) vartype = integer;i = ii;datatype(float ff) vartype = floating_point;f = ff;void print();void datatype:print() switch (vartype) case character:cout << "字符型: " <&l

14、t; c << endl;break;case integer:cout << "整型: " << i << endl;break;case floating_point:cout << "浮點(diǎn)型: " << f << endl;break;void main() datatype A('c'), B(12), C(1.44F);A.print();B.print();C.print();4-13 定義一個(gè)Circle類,有數(shù)據(jù)成員半徑Radius,成員函

15、數(shù)GetArea(),計(jì)算圓的面積,構(gòu)造一個(gè)Circle的對(duì)象進(jìn)行測(cè)試。#include <iostream.h> class Circle public:Circle(float radius) Radius = radius;Circle()float GetArea() return 3.14 * Radius * Radius; private:float Radius;void main() float radius;cout << "請(qǐng)輸入圓的半徑:"cin >> radius;Circle p(radius);cout &l

16、t;< "半徑為" << radius << "的圓的面積為:" << p.GetArea () << endl;程序二:#include<iostream>using namespace std;const float PI=(float)3.14159;class Circle public: Circle(float r); float getArea(); private: float radius;Circle:Circle(float r)radius=r;float Circ

17、le:getArea() return PI*radius*radius;void main() float radius; float FenceCost; cout<<"請(qǐng)輸入半徑:"<<endl; cin>>radius; Circle pool(radius); FenceCost=pool.getArea(); cout<<"圓的面積為:"<<FenceCost<<endl;4-14 定義一個(gè)tree類,有成員ages,成員函數(shù)grow(int years)對(duì)ages加上y

18、ears,age()顯示tree對(duì)象的ages的值。#include <iostream.h>class Tree int ages;public:Tree(int n=0);Tree();void grow(int years);void age();Tree:Tree(int n) ages = n;Tree:Tree() age();void Tree:grow(int years) ages += years;void Tree:age() cout << "這棵樹的年齡為" << ages << endl;void

19、main() Tree t(12); t.age(); t.grow(4);5-14 定義Boat與Car兩個(gè)類,二者都有weight屬性,定義二者的一個(gè)友元函數(shù)totalWeight(),計(jì)算二者的重量和。#include<iostream>using namespace std;class Car;class Boat private: int Boatwe;public: Boat() /無參數(shù)構(gòu)造函數(shù) Boatwe=300; friend int totalWeight(Boat &x,Car &y);class Car private: int Carwe

20、;public: Car( ) /無參數(shù)構(gòu)造函數(shù) Carwe=200;friend int totalWeight(Boat &x,Car &y);int totalWeight(Boat &x,Car &y) return x.Boatwe+y.Carwe;int main() Boat a; Car b; cout<<"總重量為"<<totalWeight(a,b)<<endl; return 0;3-9 編寫函數(shù)判斷一個(gè)數(shù)是否是質(zhì)數(shù),在主程序中實(shí)現(xiàn)輸入、輸出。#include <iostream

21、.h>#include <math.h>int prime(int i); /判一個(gè)數(shù)是否是質(zhì)數(shù)的函數(shù)void main() int i; cout << "請(qǐng)輸入一個(gè)整數(shù):" cin >> i; if (prime(i) cout << i << "是質(zhì)數(shù)." << endl;elsecout << i << "不是質(zhì)數(shù)." << endl;int prime(int i) int j,k,flag;flag = 1;k

22、 = sqrt(i);for (j = 2; j <= k; j+) if(i%j = 0) flag = 0; break;if (flag)return 1;elsereturn 0;.3-10 編寫函數(shù)求兩個(gè)整數(shù)的最大公約數(shù)和最小公倍數(shù)#include <iostream.h>#include <math.h>int fn1(int i,int j); /求最大公約數(shù)的函數(shù)void main() int i,j,x,y;cout << "請(qǐng)輸入一個(gè)正整數(shù):"cin >> i ;cout << "

23、;請(qǐng)輸入另一個(gè)正整數(shù):"cin >> j ;x = fn1(i,j);y = i * j / x;cout << i << "和" << j << "的最大公約數(shù)是:" << x << endl;cout << i << "和" << j << "的最小公倍數(shù)是:" << y << endl;int fn1(int i, int j) int temp

24、;if (i < j) temp = i;i = j;j = i;while(j != 0) temp = i % j;i = j;j = temp;return i;3-12 在主程序中提示輸入整數(shù)n,編寫函數(shù)用遞歸的方法求1 + 2 + + n的值。#include <iostream.h>#include <math.h>int fn1(int i);void main() int i;cout << "請(qǐng)輸入一個(gè)正整數(shù):"cin >> i ;cout << "從1累加到" <

25、<i << "的和為:" << fn1(i) << endl;int fn1(int i) if (i = 1)return 1;elsereturn i + fn1(i -1);2-28 編寫一個(gè)完整的程序,實(shí)現(xiàn)功能:向用戶提問"現(xiàn)在正在下雨嗎?",提示用戶輸入Y或N。若輸入為Y,顯示"現(xiàn)在正在下雨。"; 若輸入為N,顯示"現(xiàn)在沒有下雨。";否則繼續(xù)提問"現(xiàn)在正在下雨嗎?"#include <iostream.h>#include <

26、stdlib.h>void main() char flag;while(1) cout << "現(xiàn)在正在下雨嗎?(Yes or No):"cin >> flag;if ( toupper(flag) = 'Y') cout << "現(xiàn)在正在下雨。"break;if ( toupper(flag) = 'N') cout << "現(xiàn)在沒有下雨。"break;2-29 編寫一個(gè)完整的程序,運(yùn)行時(shí)向用戶提問"你考試考了多少分?(0100)&qu

27、ot;,接收輸入后判斷其等級(jí),顯示出來。規(guī)則如下:#include <iostream.h>void main()int i,score;cout << "你考試考了多少分?(0100):"cin >> score;if (score>100 | score<0)cout << "分?jǐn)?shù)值必須在0到100之間!"else i = score/10;switch (i) case 10:case 9:cout << "你的成績(jī)?yōu)閮?yōu)!"break;case 8:cout

28、 << "你的成績(jī)?yōu)榱迹?quot;break;case 7:case 6:cout << "你的成績(jī)?yōu)橹校?quot;break;default:cout << "你的成績(jī)?yōu)椴睿?quot; 7-5 定義一個(gè)Shape基類,在此基礎(chǔ)上派生出Rectangle和Circle,二者都有GetArea()函數(shù)計(jì)算對(duì)象的面積。使用Rectangle類創(chuàng)建一個(gè)派生類Square。(第一個(gè)程序待調(diào)試)?#include <iostream.h>class Shape public:Shape()Shape()virtual f

29、loat GetArea() return -1; ;class Circle : public Shape public:Circle(float radius):itsRadius(radius)Circle()float GetArea() return 3.14 * itsRadius * itsRadius; private:float itsRadius;class Rectangle : public Shape public:Rectangle(float len, float width): itsLength(len), itsWidth(width);Rectangle(

30、);virtual float GetArea() return itsLength * itsWidth; virtual float GetLength() return itsLength; virtual float GetWidth() return itsWidth; private:float itsWidth;float itsLength;class Square : public Rectangle public:Square(float len);Square();Square:Square(float len):Rectangle(len,len)void main()

31、 Shape * sp; sp = new Circle(5);cout << "The area of the Circle is " << sp->GetArea () << endl;delete sp;sp = new Rectangle(4,6);cout << "The area of the Rectangle is " << sp->GetArea() << endl;delete sp;sp = new Square(5);cout << &

32、quot;The area of the Square is " << sp->GetArea() << endl;delete sp;程序二:#include<iostream>using namespace std;#define PI 3.14class Shape/基類 public:Shape()Shape()virtual float GetArea()=0;class Circle:public Shape private:float itsRadius;public:Circle(float radius):itsRadius

33、(radius)Circle()float GetArea()return PI*itsRadius*itsRadius;class Rectangel:public Shape private:float itsLength,itsWidth;public:Rectangel()Rectangel(float len,float width):itsLength(len),itsWidth(width)Rectangel() virtual float GetArea()return itsLength*itsWidth;class Square:public Rectangel priva

34、te:float Widelength;public:Square(float wl):Widelength(wl)Square()virtual float GetArea()return Widelength*Widelength;int main() Square sq(3);cout<<"正方形面積:"<<sq.GetArea()<<endl;return 0;7-6 定義一個(gè)哺乳動(dòng)物Mammal類,再由此派生出狗Dog類,定義一個(gè)Dog類的對(duì)象,觀察基類與派生類的構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用順序。#include <iostr

35、eam.h>class mammal public:mammal()cout<<"it is a mammal"<<endl;void age()cout<<"mammal age"<<endl;void weight()cout<<"mammal weight"<<endl;mammal()cout<<"destory mammal"<<endl;class dog:public mammalpublic:dog

36、()cout<<"it is a dog"<<endl;void weight()cout<<"dogs weight"<<endl;dog()cout<<"destory dog class"<<endl;void main()dog dg;7-7 定義一個(gè)基類,構(gòu)造其派生類,在構(gòu)造函數(shù)中輸出提示信息,觀察構(gòu)造函數(shù)的執(zhí)行情況。#include <iostream.h>class BaseClass public:BaseClass();BaseCla

37、ss:BaseClass() cout << "構(gòu)造基類對(duì)象!" << endl;class DerivedClass : public BaseClass public:DerivedClass(); ;DerivedClass:DerivedClass() cout << "構(gòu)造派生類對(duì)象!" << endl;void main() DerivedClass d;7-8 定義一個(gè)Document類,有name成員變量,從Document派生出Book類,增加PageCount變量。#include &l

38、t;iostream.h>#include <string.h>class Document public:Document();Document( char *name );char *Name; / Document name.void PrintNameOf(); / Print name.;Document:Document( char *name ) Name = new char strlen( name ) + 1 ;strcpy( Name, name );void Document:PrintNameOf() cout << Name <&

39、lt; endl;class Book : public Document public:Book( char *name, long pagecount );void PrintNameOf();private:long PageCount;Book:Book( char *name, long pagecount ):Document(name) PageCount = pagecount;void Book:PrintNameOf() cout << "Name of book: "Document:PrintNameOf();void main() Do

40、cument a("Document1");Book b("Book1",100);b.PrintNameOf();7-10 定義object類,有weight屬性及相應(yīng)的操作函數(shù),由此派生出box類,增加Height和width屬性及相應(yīng)的操作函數(shù),聲明一個(gè)box對(duì)象,觀察構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用順序。#include <iostream.h>class object private:int Weight;public:object() cout << "構(gòu)造object對(duì)象" << endl;Weight = 0;int GetW

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論