C++程序設計課后習題和答案

//出自51博客:www.Amanda0928.51.com

第一章

一、選擇題

1.B; (typedef ,typeid ,typename,都為保留字);

2.C; (標識符,應該以字母或,下劃線開頭);

3.C; (標識符中有的特殊符號,只能有下劃線);

二、填空題

1. cin,cout

2. new,delete

3. int a(55);

三、改錯題

1.沒有定義變量num;

2.不能給變量x,聲明指向常量的指針const int *p=&x; 如果吧x定義為常量const,*p不能當作“左值”。

3.p為常量指針,不能吧p作為“左值”,p=&y,錯誤。

四、編程題

1. 分別用字符和ASCII碼形式輸出整數值65和66.

#include < iostream >

using namespace std;

void main()

{

}

2.編寫一個int型變量分配100個整形空間的程序。

#include < iostream >

using namespace std;

void main() char a='A',b='B'; int ascii_1=53,ascii_2=54;//ASCII碼中的,5和6 cout<<

{

}

3.編寫完整的程序,它讀入15個float值,用指針把它們存放在一個存儲快里,然后輸出這些值和以及最小值。

#include < iostream >

#include < algorithm > //用于數組排列的頭文件

using namespace std;

void main()

{

}

4.聲明如下數組: float *p; p = new float[15]; cout<<

int a[] = {1 ,2 ,3, 4, 5, 6, 7, 8};

先查找4的位置,講數組a復制給數組b,然后將數組a的內容反轉,再查找4的位置,最后分別輸出數組a和b的內容。

#include < iostream>

#include < algorithm>

#include < functional>

using namespace std;

void main()

{

}

int a[]={1,2,3,4,5,6,7,8},b[8]; cout<<

[color=#FF0000]第二章 [/color]

一、單項選擇

1.D; 2.D;

二、作圖題

1. 已知一個學生類具有性別和年齡兩個屬性,男學生張明的年齡為12歲,女學生李紅的年齡為11歲。給出這個學生類的類圖和它們的對象圖。

(類)Student (對象)張明 (對象)李紅

string sex; sex(男); sex(女);

int age; age(12); age(11);

方法? 方法? 方法?

2. 一個圓具有圓心坐標和半徑兩個屬性,并且能夠給出圓面積,請畫出這個圓類的類圖。 (類) Circularity (類)Point

Point p; float x;

float radii; float y;

float getX();

float getAcreage(); float getY();

3. 畫出一個班級類的類圖,為它設計必要的屬性以表示這個類的特征。

(類) PubClass

string no;//編號

int num;//人數

?

4. 畫出一種電話卡的類圖,為它設計必要的屬性。

(類) Card

long no;//編號

float balance;//余額

5. 為上題的電話卡設計必要的成員函數,以便提供基本服務。

(類) Card

long no;//編號

float balance;//余額

float getBalance();//顯示余額

三、編程題

1.使用多種方法編寫將兩個字符串連接在一起的程序。

#include < iostream >

#include < string >

using namespace std;

void main()

{

//使用string類定義字符串,完成字符串連接 string str1(

}

i++; k++; } i=0; while(c2[i]!='\0') { } cout<< c3<< endl; c3[k]=c2[i]; i++; k++;

2.已知一個string的對象str的內容為“We are here!”,使用多種方法輸出“h”。

#include < iostream >

#include < functional >

#include < algorithm >

#include < string >

using namespace std;

void main()

{

string str1(

cout<< str1[7]<< endl;//通過數組

}

[color=#F70909] string str2=str1.substr(7,1);//通過得到子字符串 cout<< str2<< endl; char *p=find(str1.begin(),str1.end(),'h');//通過find函數 if(p) cout<<*p<< endl;

第三章 [/color]

一、填空題

1.函數原型聲明;

2.inline

3.傳值,傳引用

4.函數func返回引用

5.int *fun(char,int);

二、單項選擇題

1.A; 2.C; 3.D;

三、改錯題

1.y = x * x - T; 錯誤,T是類型,不是變量,不能參加運算;

2.y沒有類型。

T max(T x, T y)

{

return (x>y) ? (x) : (y) ;

}

3.函數change 的參數定義成了常量,只能使用參數,而無權修改他。

void change (string & s)

{

s = s +

}

四、編程題

1.編寫一個求方程ax2 + bx + c = 0的根 的程序,用3個函數分別求當b2-4ac大于零、等于零、和小于零時的方程的根。要求從主函數輸入a,b,c的值并輸出結果。

#include < iostream.h >

#include < math.h >

void equation_1 (int a, int b, int c)

{

double x1, x2, temp; temp = b*b - 4 * a * c; x1 = (-b + sqrt(temp) ) / (2 * a * 1.0); x2 = (-b - sqrt(temp) ) / (2 * a * 1.0); cout<<

cout<<

}

void equation_2 (int a, int b, int c)

{

}

void equation_3 (int a, int b, int c)

{

double temp, real1, real2, image1, image2; temp = - (b*b - 4 * a * c); real1 = -b / (2 * a *1.0); real2 = real1; image1 = sqrt(temp); image2 = - image1; cout<<

}

void main()

{

int a, b, c;

}

2.定義函數up(ch),如字符變量ch是小寫字母就轉換成大寫字母并通過up返回,否則字符ch不改變。要求在短小而完全的程序中顯示這個程序是怎樣被調用的。

double temp; cout<<

#include < iostream >

using namespace std;

char up (char c)

{

}

void main()

{

}

3.編寫主程序條用帶實數r和整數n兩個參數的函數并輸出r的n次冪。

#include < iostream.h >

#include < math.h >

double power(double a, int b)

{

int i; double result = 1.0; for(i=0;i< b;i++) result = result * a; return result; int i; char c[15] = {'A','v','e','t','E','T','%','&','4','Y','e','i','@','9','^'}; for(i = 0 ; i < 15 ; i++) cout<< up(c[i])<<

}

void main()

{

}

double r; int n; cout<<

4.編寫有字符型參數C和整形參數N的函數,讓他們顯示出由字符C組成的三角形。其方式為第1行有1個字符C,第2行有2個字符C ,等等。

#include < iostream >

using namespace std;

void print_triangle(char c, int n)

{

int i, j; for(i=0; i< n; i++) { for(j=0; j<=i; j++) { cout<< c; } cout<< endl;

}

}

void main()

{

print_triangle('a',10);

}

5.編寫一個ieqiu字符串長度的函數,strlen(),再用strlen()函數編寫一個函數revers(s)的倒序遞歸程序,使字符串s逆序。

#include < iostream >

#include < string >

using namespace std;

int strlen(char *str)

{

}

{

char c;

int j, len;

len=strlen(b); int len = 0; while(str[len] != '\0') { } return len; len++; void revers(char *b)

j=len/2-1;

while(j>=0)

{

c=*(b+j);

*(b+j)=*(b+len-j-1);

*(b+len-j-1)=c;

j--;

}

void main()

{

}

6.用函數模板實現3個數值中按最小值到最大值排序的程序。

#include < iostream >

using namespace std;

template

void sort(T a, T b, T c)

{

T array[3],temp; int i,j; array[0] = a; array[1] = b; array[2] = c; for(i=0;i<3;i++) { for(j=0;j<2;j++) if(array[j]>array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; char str[]={

}

void main()

{

sort(5,1,9);

}

7.利用函數模板設計一個求數組元素中和的函數,并檢驗之。

#include < iostream >

using namespace std;

template

T sum (T a[],int n)

{

}

void main ()

{

int a[5]={1,2,3,4,5};

int s = sum(a,5);

} cout<< s<< endl; int i; T s=0; for(i=0;i< n;i++) s = s + a[i]; return s;

8.重載上題中的函數模板,使他能夠進行兩個數組的求和。

#include < iostream >

using namespace std;

template

T sum (T a[], int n)

{

int i; T s=0; for(i=0;i< n;i++) s = s + a[i]; return s;

}

template //重載上面的模板

T sum (T a[], int n, T b[], int m)

{

return sum(a,n)+sum(b,m);

}

void main ()

{

int a[5]={1,2,3,4,5};

int b[10]={1,2,3,4,5,6,7,8,9,10};

int s1 = sum(a, 5);

int s2 = sum(b, 10);

int s3= sum(a, 5, b, 10);

cout<< s1<< endl;

cout<< s2<< endl;

cout<< s3<< endl;

}

[color=#EE1111]第四章[/color]

一、填空題

1.數據成員、成員函數;

2.類、重載、1;

3.fun:fun(fun &)、fun:fun(const fun &);

二、單項選擇題

1.C。2.C。3.沒又答案,應該是A::~A(void)、或A::~A()。4.B。

三、改錯題

1.return m;---錯誤,沒又定義變量m;

2.A.init(24,56);---錯誤,應該先定義A對象:Point A;

四、完成程序題

1.

#include < iostream >

using namespace std;

class base

{

private : //私有數據成員

int a, b;

public :

void init(int x, int y)//公有函數

{ 5.C。 6.C。7.D

}; b = y; } void print() { } cout<<

void main()

{

}

2.

#include

using namespace std;

class Point

{

private : public : int m, n; Point(int, int);//整型變量,為參數的構造函數 Point(Point&);//復制構造函數的原型 print() { cout<<

};

Point::Point(int a, int b)

{

m = a; n = b;

}

Point::Point(Point & t)//復制構造函數的定義

{

m = t.m;

n = t.n;

}

void main()

}

五、程序分析題

1.沒有結果,因為沒有main函數

如果加main函數

void main()

{

base b(10, 20); Point a(10,89); Point b(a); a.print(); b.print();

}

輸出:

初始化...10,20

Destory...10,20

2.

輸出:

55

六、編程題

1.設計一個點類Point,再設計一個矩形類,矩形類使用Point類的兩個坐標點作為矩形的對角頂點。并可以輸出4個坐標值和面積。使用測試程序驗證程序。

#include

using namespace std;

class Point //點類

{

private: int x, y;//私有成員變量,坐標 public : Point()//無參數的構造方法,對xy初始化 { } x = 0; y = 0;

Point(int a, int b)//又參數的構造方法,對xy賦值 { x = a; y = b; } void setXY(int a, int b)//設置坐標的函數 { x = a; y = b; } int getX()//得到x的方法 { return x; } int getY()//得到有的函數 { } return y;

};

class Rectangle

{

//矩形類 private: Point point1, point2, point3, point4;//私有成員變量,4個點的對象

public :

Rectangle();//類Point的無參構造函數已經對每個對象做初始化啦,這里不用對每個點多初始化了

角頂點

Rectangle(Point one, Point two)//用點對象做初始化的,構造函數,1和4為對{ point1 = one; point4 = two;

init();

}

Rectangle(int x1, int y1, int x2, int y2)//用兩對坐標做初始化,構造函數,1和4為對角頂點

{ point1.setXY(x1, y1); point4.setXY(x2, y2); init(); } void init()//給另外兩個點做初始化的函數 { point2.setXY(point4.getX(), point1.getY() );

} void printPoint()//打印四個點的函數 { cout<<

};

void main()

{

Point p1(-15, 56), p2(89, -10);//定義兩個點

Rectangle r1(p1, p2);//用兩個點做參數,聲明一個矩形對象r1

}

2.使用內聯函數設計一個類,用來表示直角坐標系中的任意一條直線并輸出它的屬性。

#include < iostream.h >

#include < math.h >

class Line Rectangle r2(1, 5, 5, 1);//用兩隊左邊,聲明一個矩形對象r2 cout<<

};

inline Line::Line(int a, int b, int c, int d)

{

x1 = a; y1 = b; x2 = c; y2 = d; private: int x1, y1, x2, y2; public : Line(); Line(int =0, int =0, int =0, int=0 ); void printPoint(); double getLength();

}

inline void Line::printPoint()

{

cout<<

cout<<

}

inline double Line::getLength()

{

double length;

length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ); return length;

}

void main()

{

}

Line line(10,80,-10,12); line.printPoint(); cout<< line.getLength() << endl;

[color=#FF0000]第五章 [/color]

一、填空題

1.常成員函數;

2.常量成員函數;

3.const

二、單項選擇題

1.B; 2.A; 3.C; 4.A;

三、改錯題

1.static int getn(){return number;}錯誤

靜態成員函數,只允許訪問靜態成員變量,number不是靜態成員變量

2.void main()

{

test *two[2] = {new test(4, 5), test(6 ,8)}; for( i=0; i<2; i++) delete two[i];

}

四、完成程序題

#include < iostream >

using namespace std;

class test

{

int x; public : test(int a) { x = a; } int GetX() { } return x;

};

void main()

{

} int i;//填空一,聲明變量i test *p, a[2][3] = {{1, 2, 3}, {4, 5, 6}}; for( p=&a[0][0], i=0; i<=6; i++, p++)//填空2,初始化p,i { } if((p-a[0])%3 == 0) cout<< endl; cout<< p->GetX() <<

五、編程題

1.聲明復數的類,complex,使用友元函數add實現復數加法。

#include < iostream >

using namespace std;

class Complex

{

};

Complex add(Complex c1, Complex c2)//定義友元函數 private: double real, image; public : Complex(){} Complex(double a,double b) { } { real = a; image = b; } double getReal() { return real; real = a; image = b; void setRI(double a, double b) } double getImage() { } return image; void print() { if(image>0) cout<<

}

void main()

{

Complex c1(19, 0.864), c2, c3; c2.setRI(90,125.012); c3 = add(c1, c2); cout<<

}

2.例子5.8,114頁例子不錯;

3.編寫一個程序,該程序建立一個動態數組,為動態數組的元素賦值,顯示動態數組的值并刪除動態數組。

#include < iostream >

using namespace std;

void main()

{

}

int i, n, temp=0; cout<<

4.定義一個Dog類,它用靜態數據成員Dogs記錄Dog的個體數目,靜態成員函數GetDogs用來存取Dogs。設計并測試這個類。

#include < iostream >

using namespace std;

class Dog

{

private: static int dogs;//靜態數據成員,記錄Dog的個體數目 public : Dog(){} void setDogs(int a) { } dogs = a; static int getDogs() { return dogs; }

};

int Dog :: dogs = 25;//初始化靜態數據成員

void main()

{

cout<<

}

Dog a, b; cout<<

[color=#FF0000]

第六章[/color]

一、填空題

1.單一繼承; 2.private protected public

二、單項選擇

1.D;2.A;3.C;4.D;

三、改錯題

1.類derived和base中均沒變量b,derived的構造函數中的m(b)錯誤;

2.Derived類中重載show()方法

void Show()

{

Base1::Show();Base2::Show();

}

四、編程題

1.設計一個基類,從基類派生圓柱,設計成員函數輸出它們的面積和體積;

#include < iostream >

using namespace std;

class Basic//基類

{

protected: double r; public : Basic(){ r = 0; }

Basic(double a):r(a){}

};

class Circular : public Basic//從基類派生圓類

{

protected: public double area; : Circular(double a) { r = a; area = area = 3.1415926 * r * r; } double getArea()//返回圓面積 { } return area;

};

class Column : public Circular//從圓類派生圓柱類

{

protected: double h; double cubage; public : Column(double a, double b) : Circular(a) { } h = b; cubage = getArea() * h;

double getCubage()//返回圓柱體積函數 { } return cubage;

};

{

} void main() Circular circular(45); Column column(12, 10); cout<<

3.定義一個線段類作為矩形的基類,基類有起點和終點坐標,有輸出左邊和長度以及線段和x軸的夾角的成員函數。矩線段對象的兩個坐標作為自己一條邊的位置,它具有另外一條邊,能輸出矩形的4個頂點坐標。給出類的定義并用程序驗證它們的功能。

#include < iostream >

#include < cmath >

using namespace std;

class Point//點類

{

};

class Line

{

protected: Point p1, p2;//Point對象做成員 double length, angle; public: Line(double a, double b, double c, double d):p1(a, b), p2(c, d)//用兩對坐標初始化protected: double x, y; public : Point(){} Point(double a, double b) { } x = a; y = b; double getX() {return x;} double getY() {return y;}

線段

{ init(); } Line(Point a, Point b)//用兩個點的對象初始化線段 { p1 = a; p2 = b; init(); } void init()//計算線段長度,以及和x軸的夾角的度數 { double x1 = p1.getX(), y1 = p1.getY(); double x2 = p2.getX(), y2 = p2.getY(); length = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); angle = atan( (y2-y1) / (x2-x1) ); angle = angle *180/3.141592653; } void printXY() { cout<<

};

class Rectangle : public Line

{

protected: Line *line; public: Rectangle(double a, double b, double c, double d, double e, double f, double g, double h):Line(a,b,c,d)

{

line = new Line(e,f,g,h); } Rectangle(Point a, Point b, Point c, Point d) : Line(a, b)//4個點對象,初始化 { line = new Line(c, d);

} void printPoint() { cout<<

};

void main()

{

Point p1(0, 0), p2(4, 3), p3(12, 89), p4(10, -50); Line l1(0,0,4,3); l1.printXY(); l1.printLength(); l1.printAngle(); Line l2(p1, p2); l2.printXY(); l2.printLength(); l2.printAngle(); Rectangle r1(12,45,89,10,10,23,56,1); r1.printPoint(); Rectangle r2(p1, p2, p3, p4); r2.printPoint();

}

4.基類是使用極坐標的點類,從它派生一個圓類,圓類用點類的左邊作圓心,圓周通過極坐標原點,圓類有輸出圓心直、圓半徑和面積的成員函數。完成類的設計并驗證之。

#include < iostream >

#include < cmath >

using namespace std;

class Point//點類

{

protected:

int x, y; Point(){} public :

};

class Circular : public Point//圓類,繼承點類

{

protected: double r, area; public :

Circular(int a, int b) { } x = a; y = b; r = sqrt( x * x + y * y ); area = 3.1415926 * r * r; void printPoint() { cout<<

};

void main()

{

}

5.設計一個線段基類,當創建五參數對象時,才要求用戶輸入長度。同樣,其派生的直角三角形類也是在產生對象時要求輸入兩個直角邊的長度。直角三角形在派生矩形類,矩形類的參數也由鍵盤輸入。設計這些類并測試他們的功能。

#include < iostream >

#include < cmath >

using namespace std;

class Line//線段基類

{ Circular c(10,25); c.printPoint(); c.printRadius(); c.printArea();

double sizeA; public : Line() { cout<<

};

class Triangle : public Line//三角形類

{

protected: double sizeB, sizeC; public : Triangle() { cout<<

};

class Rectangle : public Triangle//矩形類

{

protected: double sizeD; Rectangle() { sizeC = sizeA; public :

}; } void printSize() { cout<<

void main()

{

/* Line *l = new Line();

cout<<

*/

}

/*Triangle *t = new Triangle(); t->printSize();*/ Rectangle *r = new Rectangle(); r->printSize();

[color=#FF0000]第七章 [/color]

一、單項選擇

1.A; 2.A; 3.B; 4.D; 5.D

二、填空題

1.rbegin(), insert(iterator it, const T& );

2.size(), 2;

3.typedef vector< 數據類型 >::reverse_iterator reverse_iterator

三、改錯題

1.第六行的返回類型

T getx()

{ return x;

}

2.類Point的構造方法中的參數類型是int,所以在Line構造方法中的a,b應該是int型;

四、編程題

1.使用類模板演示復制兼容性規則。

#include < iostream>

using namespace std;

template

class Point

{

protected: T x, y; public : Point(T a, T b) { x = a; y = b; } void show() { } cout<<

};

template

class Rectangle : public Point

{

private: T h, w; public : Rectangle(T a, T b, T c, T d) : Point(a, b)

{ } h = c; w = d; void show() { } cout<<

};

{

void main() Point a(3, 4); Rectangle b(5.1, 6.2, 7.3, 8.4); a.show();

b.show(); Point & ra = b;//子類對象 初始化父類的引用 ra.show(); Point * p = &b;//子類對象的地址,賦給指向父類的指針 p->show(); Rectangle * pb = &b;//子類指針pb pb->show(); a = b; //派生類對象的屬性值,更新父類對象的屬性值

a.show();

}

//134頁,例6.3 賦值兼容規則的例子

2.設計一個點的類模板,分別使用繼承、包含的方法設計線段類模板,要求演示構造函數和復制構造函數的設計方法,并用主程序驗證之。

#include < iostream>

using namespace std;

template class Point

{

public :

T x, y; Point(T a=0, T b=0) { x = a; y = b; } void show() { } cout<<

};

template class Line_1 : public Point // 繼承Point類模板, 的線段類模板

{

protected: T x1, y1; public : Line_1(T a, T b, T c, T d) : Point(a, b)

}; x1 = c; y1 = d; } Line_1(const Line_1 & );//復制構造函數 void show() { } cout<<

template Line_1 :: Line_1(const Line_1 & t) : Point(t.x, t.y)

{

x1 = t.x1; y1 = t.y1;

}

template class Line_2 //包含point類模板,的線段類

{

protected:

Point p1, p2; : Line_2(T a, T b, T c, T d) { p1.x = a; p1.y = b; p2.x = c; p2.y = d; public } Line_2(const Line_2 &);//復制構造函數 void show() { } cout<<

};

template Line_2 :: Line_2(const Line_2 & t)

{

}

void main()

{

Line_1 L1(1,2,3,4); cout<<

}

Line_1 L2(L1); //用現有的對象,初始化新對象 cout<<

3.已知有一個整型數組a,其內容為1 3 5 7 9 2 4 6 8 10.先對數組進行升序排列,再使用它產生向量b,然后再在向量的尾部追加11,并按降序排列輸出向量的內容和capacity()的內容。 #include < iostream >

#include < vector >

#include < algorithm >

using namespace std;

void main()

{

}

[color=#F70909]第八章[/color]

一、單項選擇題

1.A; 2.B;

二、分析程序題

1.

2. int a[] = {1,3,5,7,9,2,4,6,8,10}; sort(a, a+10);//先對數組進行升序排序 copy(a, a+10, ostream_iterator(cout,

三、查錯題

print函數的參數應該是引用

void print(base & p)

{

p.show();

}

四、完成程序題

#include < iostream >

using namespace std;

class base

{

int i;

int j; public: base(int I, int J) : i(I),j(J) { display(); } int getI() const { return i; } int getJ() const { return j; } void display() const { }

cout<<

};

class derived : public base

{

int k; public:

}; { display(); } void display() const { } cout<<

void main()

{

}

base b3(8, 9); derived d1(10, 20, 5);

[color=#FF0000]

第九章[/color]

一、單項選擇

1.B; 2.A; 3.C; 4.B 5.D 6.B

二、填空題

1.輸出數據按輸出域右邊對齊輸出

2.cin.ignore(3)

3.ofstream fout(

三、分析程序題

1.

2.

四、完成程序題

1.

#include < iostream >

#include < iomanip >

using namespace std;

void main()

{

cout.precision(6);

cout<< scientific << showpos;

cout<< -25.89F <<

cout<< 25.89f << endl;

}

2.

class Fun

{

};

void main()

{

Fun fun;

cout<< setfill('*') << setw(10) <<12345<<

} cout<< fun << setw(10) << 54321 << endl; friend ostream & operator<<(ostream & os, Fun f) { } os.setf(ios::left); return os;;

五、編程題

1.利用流格式控制,進行成績和名字的輸出,要求名字左對齊,分數右對齊。

#include < iostream >

#include < string >

using namespace std;

class Student

{

private : string name; float score; public : Student(){} Student(string n, float s)

{ name = n; score = s; } string getName() { return name; } float getScore() { } return score;

};

void main()

{

} Student s1(

2.編寫一個產生文本文件的程序。

#include < iostream>

#include < fstream >

using namespace std;

void main()

{

} char *p = {

3.編寫一個程序,要求輸入三角形的3條邊,然后判斷是否合理,如果不合理,給出信息并要求重新輸入;如果合理,計算其面積并將結果存入文件中。

//我調試這個程序的時候,發現必須關掉卡巴斯基才可以,不知道為什么

#include < iostream >

#include < fstream >

#include < cmath >

#include < vector >

#include < iomanip >

#include < string >

using namespace std;

class Triangle

{

double sizeA, sizeB, sizeC, area; public: Triangle(){} void setArea() { double p = (sizeA + sizeB + sizeC) *0.5; area = sqrt( p * (p - sizeA) * (p - sizeB) * (p - sizeC) ); } void setSizeA(double a) { } void setSizeB(double b) { sizeB = b; } void setSizeC(double c) { sizeC = c; } void set(vector &); sizeA = a;

};

//***************************************

//* 成員函數:set

//* 參 數 :向量對象的引用

//* 返回值 :無

//* 功能 :為向量賦值并將向量存入文件

//***************************************

void Triangle :: set(vector & v )

{

Triangle t; double a, b, c; while(1) { cout<<

cin>>a; if(a == -1)//結束符為-1 { ofstream writeFile; char fileName[20]; cout<<

writeFile<< v[i].sizeA <<

writeFile.close();

cout<<

}

void main()

{

vector tri; Triangle triangle; } return; } cout<<

triangle.set(tri);

}

4.改寫上題的程序,使程序反復計算,直到輸入結束符號為止。要求在停止計算后,詢問要保存的文件名,然后講結果一次寫入制定文件中。

//需要關掉卡巴斯基

#include < iostream >

#include < fstream >

#include < cmath >

#include < vector>

#include < iomanip >

#include < string >

using namespace std;

class Triangle

{

double sizeA, sizeB, sizeC, area; public: Triangle(){} void setArea() { } double p = (sizeA + sizeB + sizeC) *0.5; area = sqrt( p * (p - sizeA) * (p - sizeB) * (p - sizeC) ); void setSizeA(double a) { sizeA = a; } void setSizeB(double b) { sizeB = b; } void setSizeC(double c) { }

void set(vector &); sizeC = c;

};

//***************************************

//* 成員函數:set

//* 參 數 :向量對象的引用

//* 返回值 :無

//* 功能 :為向量賦值并將向量存入文件

//***************************************

void Triangle :: set(vector & v )

{

Triangle t;

double a, b, c;

while(1) { cout<<

writeFile.close();

cout<<

return; } cout<<

cout<<

}

}

void main()

{

}

5.從文件TEST中讀出字符并寫入TEST1里,要求均附加錯誤檢查。

#include

#include

using namespace std;

void main()

{

} ifstream txt1(

6.從鍵盤輸入一個字符串,將其中的大寫字母全部轉換成小寫字母,然后存入到文件名為“text”的磁盤文件中保存。輸入的字符串以“$”結束。

//需要關掉卡巴斯基

#include < iostream >

#include < fstream >

using namespace std;

void main()

{

}

char a[100]; ofstream writeFile(

[color=#FF0000]第十章[/color]

一、單項選擇題

1.D; 2.A; 3.B; 4.D

二、填空題

1.過程抽象和數據抽象

2.對象

3.問題域、系統邊界、系統責任

4.我覺得應該是,類的成員有(數據成員)和(成員函數)兩打類。

5.

四、編程題

1.取消設計實例中的Cow屬性,練習使用模板實現包含的設計方法。 #include < iostream >

#include < cmath >

using namespace std;

//********************

//* 聲明Point類

//********************

template

class Point

{

T x, y; public: Point(T a= 0, T b= 0) : x(a), y(b){} Point(Point & a) { x = a.x; y = a.y; } void Display(); T Distance(Point &); T getX(){return x;}

T getY(){return y;}

};

//***************

//*成員函數:Point :: Display()

//*功能 :打印點坐標

template

void Point :: Display()

{

cout<< x <<

}

//**************************

//*成員函數:Point :: Distance

//*參數 :Point對象的引用

//*返回值 :兩點間距離

//*返回類型:T

template

T Point :: Distance(Point & a)

{

return sqrt( (x - a.x)*(x - a.x) + (y - a.y) * (y - a.y) );

}

//**********************

//* 聲明Line

//**********************

template

class Line

{

};

//*************************

//*成員函數:Line::Dispaly

//*參數 :無

//*功能 :打印線段每個點的坐標

template

void Line :: Display()

{

a.Display();

} b.Display(); Point a, b; public: Line(Point & a1, Point & a2) : a(a1), b(a2){} Line(Line & s) : a(s.a), b(s.b){} void Display(); T Distance(); T getArea();

//*************************

//*成員函數:Line::Distance

//*參數 :無

//*返回值 :線段長度

template

T Line :: Distance()

{

} T x = a.getX() - b.getX(); T y = a.getY() - b.getY(); return sqrt( x * x + y * y );

void main()

{

Point a;

Point b(7.8, 9.8); Point c(34.5, 67.8); a = c; a.Display(); b.Display(); cout<<

}

2.取消設計實例中的Cow屬性,練習使用模板實現繼承的設計方法。 #include < iostream >

#include < cmath >

using namespace std;

//*********************

//*聲明Point類

//********************

template

class Point

{

protected:

T x, y; public: Point(T a = 0, T b = 0): x(a), y(b){} Point(Point &a) { } virtual void Display() { } T Distance(Point &); T getX(){return x;} T getY(){return y;} cout<<

//****************************

//*成員函數: Point::Distance

//*參數 : Point對象的引用

//*返回值 : 兩點間距離

template

T Point :: Distance(Point &a)

{

}

//***********************

//*聲明Line類

//***********************

template

class Line : public Point

{

double x2, y2;

public: Line(T, T, T, T ); Line(Line & ); void Display(); T Distance(); T getX2(){return x2;} T getY2(){return y2;} friend void Disp(Line & t){cout< return sqrt( (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y) );

Line :: Line(T a1, T a2, T a3, T a4):Point(a1, a2), x2(a3), y2(a4){}

template

Line :: Line(Line &s) : Point(s.x, s.y), x2(s.x2), y2(s.y2){}

//***************************

//*成員函數:Line::Distance()

//*返回值 :線段長度

template

T Line::Distance()

{

}

//***************************

//*成員函數:Line::Display() T x = x2 - x; T y = y2 - y; return sqrt(x*x + y*y);

//*功能 :打印線段兩個端點坐標

template

void Line::Display()

{

cout<<

//****************************

//*友元函數:operator<<

//*返回值 :ostream &

//*功能 :重載“<< ”

template

ostream & operator<<(ostream & stream, Line obj)

{

stream<<

stream<< obj.getX() <<

}

void main()

{

Pointa;

Line s(7.8, 9.8, 34.5, 67.8); Disp(s); Line s1(s); cout<<

} cout<<

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,572評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,071評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,409評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,569評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,360評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,895評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,979評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,123評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,643評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,559評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,742評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,250評論 5 356
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,981評論 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,363評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,622評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,354評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,707評論 2 370

推薦閱讀更多精彩內容