




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、 C+課后習題程序3-14.用遞歸的方法編寫程序求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函數定義 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函數中x的值 int n; /級數n cout<<"請輸入legendre polynomials函數中x的值:n" cin>>a; cout<<"請輸入所要求的多項式級數:n" cin>>n; p(a,n); cout<<"n階勒讓德多項式的值為(當x="<<a<<"時):n" cout<<p(a,n)<<endl; /輸出多項式的
3、值 4-8 定義一個Dog 類,包含的age、weight等屬性,以及對這些屬性操作的方法。實現并測試這個類。 (第一個程序待調試)?#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 設計并測試一個名為Rectangle的矩形類,其屬性為矩形的左下角與右上角兩個點的坐標,能計算矩形的面積。#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 定義一個矩形類,有長、寬兩個屬性,有成員函數計算矩形的面積 #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 << "請輸入矩形的長度:"cin >> length;cout << "請輸入矩形的寬度:"cin >> width;Rectangl
12、e r(length, width);cout << "長為" << length << "寬為" << width << "的矩形的面積為:" << r.GetArea () << endl;4-12 定義一個"數據類型" datatype類,能處理包含字符型、整型、浮點型三種類型的數據,給出其構造函數。 #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 << "浮點型: " << f << endl;break;void main() datatype A('c'), B(12), C(1.44F);A.print();B.print();C.print();4-13 定義一個Circle類,有數據成員半徑Radius,成員函
15、數GetArea(),計算圓的面積,構造一個Circle的對象進行測試。#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 << "請輸入圓的半徑:"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<<"請輸入半徑:"<<endl; cin>>radius; Circle pool(radius); FenceCost=pool.getArea(); cout<<"圓的面積為:"<<FenceCost<<endl;4-14 定義一個tree類,有成員ages,成員函數grow(int years)對ages加上y
18、ears,age()顯示tree對象的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兩個類,二者都有weight屬性,定義二者的一個友元函數totalWeight(),計算二者的重量和。#include<iostream>using namespace std;class Car;class Boat private: int Boatwe;public: Boat() /無參數構造函數 Boatwe=300; friend int totalWeight(Boat &x,Car &y);class Car private: int Carwe
20、;public: Car( ) /無參數構造函數 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 編寫函數判斷一個數是否是質數,在主程序中實現輸入、輸出。#include <iostream
21、.h>#include <math.h>int prime(int i); /判一個數是否是質數的函數void main() int i; cout << "請輸入一個整數:" cin >> i; if (prime(i) cout << i << "是質數." << endl;elsecout << i << "不是質數." << 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 編寫函數求兩個整數的最大公約數和最小公倍數#include <iostream.h>#include <math.h>int fn1(int i,int j); /求最大公約數的函數void main() int i,j,x,y;cout << "請輸入一個正整數:"cin >> i ;cout << "
23、;請輸入另一個正整數:"cin >> j ;x = fn1(i,j);y = i * j / x;cout << i << "和" << j << "的最大公約數是:" << x << endl;cout << i << "和" << j << "的最小公倍數是:" << 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 在主程序中提示輸入整數n,編寫函數用遞歸的方法求1 + 2 + + n的值。#include <iostream.h>#include <math.h>int fn1(int i);void main() int i;cout << "請輸入一個正整數:"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 編寫一個完整的程序,實現功能:向用戶提問"現在正在下雨嗎?",提示用戶輸入Y或N。若輸入為Y,顯示"現在正在下雨。"; 若輸入為N,顯示"現在沒有下雨。";否則繼續提問"現在正在下雨嗎?"#include <iostream.h>#include <
26、stdlib.h>void main() char flag;while(1) cout << "現在正在下雨嗎?(Yes or No):"cin >> flag;if ( toupper(flag) = 'Y') cout << "現在正在下雨。"break;if ( toupper(flag) = 'N') cout << "現在沒有下雨。"break;2-29 編寫一個完整的程序,運行時向用戶提問"你考試考了多少分?(0100)&qu
27、ot;,接收輸入后判斷其等級,顯示出來。規則如下:#include <iostream.h>void main()int i,score;cout << "你考試考了多少分?(0100):"cin >> score;if (score>100 | score<0)cout << "分數值必須在0到100之間!"else i = score/10;switch (i) case 10:case 9:cout << "你的成績為優!"break;case 8:cout
28、 << "你的成績為良!"break;case 7:case 6:cout << "你的成績為中!"break;default:cout << "你的成績為差!" 7-5 定義一個Shape基類,在此基礎上派生出Rectangle和Circle,二者都有GetArea()函數計算對象的面積。使用Rectangle類創建一個派生類Square。(第一個程序待調試)?#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 定義一個哺乳動物Mammal類,再由此派生出狗Dog類,定義一個Dog類的對象,觀察基類與派生類的構造函數與析構函數的調用順序。#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 定義一個基類,構造其派生類,在構造函數中輸出提示信息,觀察構造函數的執行情況。#include <iostream.h>class BaseClass public:BaseClass();BaseCla
37、ss:BaseClass() cout << "構造基類對象!" << endl;class DerivedClass : public BaseClass public:DerivedClass(); ;DerivedClass:DerivedClass() cout << "構造派生類對象!" << endl;void main() DerivedClass d;7-8 定義一個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屬性及相應的操作函數,由此派生出box類,增加Height和width屬性及相應的操作函數,聲明一個box對象,觀察構造函數與析構函數的調用順序。#include <iostream.h>class object private:int Weight;public:object() cout << "構造object對象" << endl;Weight = 0;int GetW
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 瓣膜置換的圍術期護理
- 人教A版 (2019)選擇性必修 第一冊3.2 雙曲線獲獎教案
- 2024中鋁共享服務(天津)有限公司校園招聘1人筆試參考題庫附帶答案詳解
- 鳳仙花的一生(教學設計)-2024-2025學年科學三年級下冊人教鄂教版
- 人教部編版一年級下冊20 咕咚第2課時教學設計
- 人教版(2024)八年級上冊(2024)第4節 眼睛和眼鏡教案
- 2024中建一局二級公司總工程師公開競聘1人筆試參考題庫附帶答案詳解
- 釘釘使用詳盡培訓
- 2024中國郵政福建建省分公司校園招聘預筆試參考題庫附帶答案詳解
- 人美版三年級下冊第3課 豎彎鉤教案及反思
- 顯示屏出廠合格證
- (中職)電子技術基礎與技能(電子信息類)教案
- 三晶變頻器說明書SAJ系列簡約
- 混凝土模板支撐工程專項施工方案(140頁)
- MATLAB_第6講_GUI界面設計
- 第三章煤層氣的儲層壓力及賦存狀態
- 高中英語北師大版(2019)必修第一冊 Unit3Lesson1SpringFestival
- 《公輸》(共44張PPT)
- 鴿巢原理例1、例2
- 飛剪機傳動裝置的設計機械CAD圖紙
- 阿里巴巴OfferLetter
評論
0/150
提交評論