博客> c++程序设计课后答案参考
c++程序设计课后答案参考
2017-12-13 20:55 评论:0 阅读:133 郭鹏飞

//出自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 xss=removed>

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 c3[k]=c2[i];>

include < functional>

include < algorithm>

include < string>

using namespace std;

void main()

{

string str1(

cout<< str1 color=#F70909] str2=str1.substr(7,1);//通过得到子字符串 p=find(str1.begin(),str1.end(), xss=removed>y) ? (x) : (y) ;

}

3.函数change 的参数定义成了常量,只能使用参数,而无权修改他。

void change (string & s)

{

s = s +

}

四、编程题

1.编写一个求方程ax2 + bx + c = 0的根 的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。要求从主函数输入a,b,c的值并输出结果。

include < iostream>

include < math.h >

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

{

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

cout xss=removed xss=removed xss=removed xss=removed xss=removed>

using namespace std;

char up (char c)

{

}

void main()

{

}

3.编写主程序条用带实数r和整数n两个参数的函数并输出r的n次幂。

include < iostream>

include < math.h >

double power(double a, int b)

{

int i; double result = 1.0; for(i=0;i< b xss=removed xss=removed xss=removed>

using namespace std;

void print_triangle(char c, int n)

{

int i, j; for(i=0; i< n for(j=0; j<=i;>

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 for(j=0;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 s=0; for(i=0;i xss=removed>

using namespace std;

template

T sum (T a[], int n)

{

int i; T s=0; for(i=0;i< n xss=removed a[5]={1,2,3,4,5}; b[10]={1,2,3,4,5,6,7,8,9,10}; xss=removed xss=removed xss=removed color=#EE1111]第四章[/color]>

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 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>

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 xss=removed color=#FF0000]第五章 xss=removed i=0;>

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>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>

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>

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 xss=removed xss=removed>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 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>

include < vector>

include < algorithm>

using namespace std;

void main()

{

}

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

一、单项选择题

1.A; 2.B;

二、分析程序题

1.

  1. 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 color=#FF0000]>

include < iomanip>

using namespace std;

void main()

{

cout.precision(6);

cout<< scientific>

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>

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>

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 xss=removed xss=removed xss=removed>

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(xx + 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<<

收藏
0
sina weixin mail 回到顶部