收藏 分享(赏)

自考c++程序设计 课后答案.doc

上传人:cw6mk8 文档编号:9458229 上传时间:2019-08-08 格式:DOC 页数:21 大小:156KB
下载 相关 举报
自考c++程序设计 课后答案.doc_第1页
第1页 / 共21页
自考c++程序设计 课后答案.doc_第2页
第2页 / 共21页
自考c++程序设计 课后答案.doc_第3页
第3页 / 共21页
自考c++程序设计 课后答案.doc_第4页
第4页 / 共21页
自考c++程序设计 课后答案.doc_第5页
第5页 / 共21页
点击查看更多>>
资源描述

1、- 1 -第一章一、选择题 1.B; (typedef ,typeid ,typename,都为保留字); 2.C; (标识符,应该以字母或,下划线开头); 3.C; (标识符中有的特殊符号,只能有下划线); 二、填空题 1. cin,cout 2. new,delete3. int a(55); 三、改错题 1.没有定义变量 num; 2.不能给变量 x,声明指向常量的指针 const int *p= 如果吧 x 定义为常量 const, *p 不能当作“左值” 。 3.p 为常量指针,不能吧 p 作为“左值”,p=void main()char a=A,b=B;int ascii_1=53

2、,ascii_2=54;/ASCII 码中的,5 和 6coutusing namespace std;void main()int *p;p = new int100;for(int i = 0;i #include /用于数组排列的头文件using namespace std;void main()float *p;p = new float15;cout*(p+i);for(i = 0;i #include #include using namespace std;void main()int a=1,2,3,4,5,6,7,8,b8;cout#include using namespa

3、ce std;void main()/使用 string 类定义字符串,完成字符串连接string str1(“C+“),str2(“程序设计“);string str3;str3 = str1+str2;/连接方式 1cout#include #include #include using namespace std;void main()string str1(“We are here!“);couty) ? (x) : (y) ; 3.函数 change 的参数定义成了常量,只能使用参数,而无权修改他。 void change (string 四、编程题 1.编写一个求方程 ax2 +

4、bx + c = 0 的根 的程序,用 3个函数分别求当 b2-4ac 大于零、等于零、和小于零时的方程的根。要求从主函数输入 a,b,c 的值并输出结果。#include #include 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);coutabc;cout 0)equation_1 (a, b, c);if(temp

5、= 0)equation_2 (a, b, c);if(temp using namespace std;char up (char c)if(c = 97 double result = 1.0;for(i=0;ir;coutn;coutusing namespace std;void print_triangle(char c, int n)int i, j;for(i=0; i#include using namespace std;int strlen(char *str)int len = 0;while(strlen != 0)len+;return len;void revers

6、(char *b)char c; int j, len; len=strlen(b); j=len/2-1; while(j=0) c=*(b+j); *(b+j)=*(b+len-j-1); *(b+len-j-1)=c; j-; blen=0;void main()char str=“1234567890“;coutusing namespace std;template void sort(T a, T b, T c)T array3,temp;- 5 -int i,j;array0 = a;array1 = b;array2 = c;for(i=0;iarrayj+1)temp = a

7、rrayj;arrayj = arrayj+1;arrayj+1 = temp;coutusing namespace std;template T sum (T a,int n)int i;T s=0;for(i=0;iusing namespace std;template T sum (T a, int n)int i;T s=0;for(i=0;iusing namespace std;class baseprivate : /私有数据成员int a, b;public :void init(int x, int y)/公有函数a = x;b = y;void print()- 6 -

8、cout 0)return area;elsereturn -area;void main()Point p1(-15, 56), p2(89, -10);/定义两个点Rectangle r1(p1, p2);/用两个点做参数,声明一个矩形对象 r1Rectangle r2(1, 5, 5, 1);/用两队左边,声明一个矩形对象 r2cout#include class Lineprivate:int x1, y1, x2, y2;public :Line();Line(int =0, int =0, int =0, int=0 );void printPoint();double getLe

9、ngth();inline Line:Line(int a, int b, int c, int d)x1 = a;y1 = b;x2 = c;y2 = d;inline void Line:printPoint()- 8 -coutusing namespace std;class testint x;public :test(int a)x = a;int GetX()return x;void main()int i;/填空一,声明变量 itest *p, a23 = 1, 2, 3, 4, 5, 6;for( p= iGetX() using namespace std;class C

10、omplexprivate:double real, image;public :Complex()Complex(double a,double b)real = a;image = b;void setRI(double a, double b)real = a;image = b;double getReal()return real;double getImage()return image;void print()if(image0)coutusing namespace std;void main()int i, n, temp=0;coutn;double *array = ne

11、w doublen; /用指针,动态申请数组大小couttemp;*(array+i) = temp;/给数组元素赋值coutusing namespace std;class Dogprivate:static int dogs;/静态数据成员,记录 Dog的个体数目public :Dog()void setDogs(int a)dogs = a;static int getDogs()return dogs;int Dog : dogs = 25;/初始化静态数据成员void main()coutusing namespace std;class Basic/基类protected:dou

12、ble r;public :Basic() r = 0; Basic(double a):r(a);class Circular : public Basic/从基类派生圆类protected:double area;public :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

13、 a, double b) : Circular(a)h = b;cubage = getArea() * h;double getCubage()/返回圆柱体积函数return cubage;void main()Circular circular(45);Column column(12, 10);cout #include using namespace std;class Point/点类protected:double x, y;public :Point()Point(double a, double b)x = a; y = b;double getX()return x;dou

14、ble getY()return y;class Lineprotected:Point p1, p2;/Point 对象做成员double length, angle;public:Line(double a, double b, double c, double d):p1(a, b), p2(c, d)/用两对坐标初始化线段init();Line(Point a, Point b)/用两个点的对象初始化线段- 11 -p1 = a; p2 = b;init();void init()/计算线段长度,以及和 x 轴的夹角的度数double x1 = p1.getX(), y1 = p1.g

15、etY();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()coutprintXY();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.print

16、Angle();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 #include using namespace std;class Point/点类pro

17、tected:int x, y;public :Point();class Circular : public Point/圆类,继承点类protected:double r, area;- 12 -public :Circular(int a, int b)x = a;y = b;r = sqrt( x * x + y * y );area = 3.1415926 * r * r;void printPoint()cout#include using namespace std;class Line/线段基类protected:double sizeA;public :Line()couts

18、izeA;Line(double a)sizeA = a;double getLength()return sizeA;class Triangle : public Line/三角形类protected:double sizeB, sizeC;public :Triangle()coutsizeB;sizeC = sqrt( sizeB * sizeB + sizeA * sizeA );void printSize()coutgetLength() printSize();*/Rectangle *r = new Rectangle();r-printSize();第七章一、单项选择 1.

19、A; 2.A; 3.B; 4.D; 5.D 二、填空题 1.rbegin(), insert(iterator it, const T3.typedef vector:reverse_iterator reverse_iterator 三、改错题 1.第六行的返回类型 T getx()return x;2.类 Point 的构造方法中的参数类型是 int,所以在 Line构造方法中的 a,b 应该是 int 型;四、编程题 1.使用类模板演示复制兼容性规则。#include using namespace std;template class Pointprotected:T x, y;pub

20、lic :Point(T a, T b)x = a;y = b;void show()coutshow();Rectangle * pb = /子类指针 pbpb-show();a = b; /派生类对象的属性值,更新父类对象的属性值a.show();/134 页,例 6.3 赋值兼容规则的例子2.设计一个点的类模板,分别使用继承、包含的方法设计线段类模板,要求演示构造函数和复制构造函数的设计方法,并用主程序验证之。#include using namespace std;template class Point- 14 -public :T x, y;Point(T a=0, T b=0)x

21、 = a;y = b;void show()cout#include #include using namespace std;void main()int a = 1,3,5,7,9,2,4,6,8,10;sort(a, a+10);/先对数组进行升序排序copy(a, a+10, ostream_iterator(cout,“ “);coutusing namespace std;class baseint i;int j;public:base(int I, int J) : i(I),j(J)display();int getI() constreturn i;int getJ() c

22、onstreturn j;void display() constcout#include using namespace std;void main()cout.precision(6);cout#include using namespace std;class Studentprivate :string name;float score;public :Student()Student(string n, float s)name = n;score = s;string getName()return name;float getScore()return score;void ma

23、in()Student s1(“liming“, 98);Student s2(“sdfh“, 90);Student s3(“vn.fy“, 80);Student s4(“cnbtrt“, 70);Student s5(“ryuety“, 48);cout.width(15); cout #include using namespace std;void main()char *p = “C+程序设计 “;ofstream myFile(“Worl9_5_2.txt“);myFile#include #include #include #include #include using nam

24、espace std;class Triangledouble 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)- 17 -sizeB = b;void setSizeC(double c)sizeC = c;void se

25、t(vector ;/*/* 成员函数:set/* 参 数 :向量对象的引用/* 返回值 :无/* 功能 :为向量赋值并将向量存入文件/*void Triangle : set(vector double a, b, c;while(1)couta;if(a = -1)/结束符为-1ofstream writeFile;char fileName20;coutfileName;cout b;cout c;if( a0 t.setSizeB(b);t.setSizeC(c);t.setArea();v.push_back(t);elsecout#include #include #include

26、 #include #include using namespace std;class Triangledouble 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)- 18 -sizeA = a;void setSizeB(double b)sizeB = b;void setSize

27、C(double c)sizeC = c;void set(vector ;/*/* 成员函数:set/* 参 数 :向量对象的引用/* 返回值 :无/* 功能 :为向量赋值并将向量存入文件/*void Triangle : set(vector double a, b, c;while(1)couta;if(a = -1)/结束符为-1ofstream writeFile;char fileName20;cout fileName;coutb;coutc;if( a0 t.setSizeB(b);t.setSizeC(c);t.setArea();v.push_back(t);elsecou

28、tc;if(txt1.eof()txt1.close;return;cout#include using namespace std;void main()char a100;ofstream writeFile(“text.txt“);int i;while(1)cina;if(a0 = $)return;i = 0;while(ai != 0)if( ai=65 /*/* 声明 Point 类/*template class PointT x, y;public:Point(T a= 0, T b= 0) : x(a), y(b)Point(Point y = a.y;void Displ

29、ay();T Distance(Point T getX()return x;T getY()return y;/*/*成员函数: Point : Display()/*功能 :打印点坐标template void Point : Display()cout#include using namespace std;/*/*声明 Point 类/*template class Pointprotected:T x, y;public:Point(T a = 0, T b = 0): x(a), y(b)Point(Point y = a.y;virtual void Display()coutDisplay();cout“基类对象引用派生类的对象“ endl;Point d.Display();

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报