精选优质文档-----倾情为你奉上Problem A: 平面上的点——Point类 (IV)Description在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定现在我们封装一个“Point类”来实现平面上的点的操作根据“append.cc”,完成Point类的构造方法和show()、showCounter()、showSumOfPoint()方法;实现showPoint()函数 接口描述:showPoint()函数:按输出格式输出Point对象,调用Point::show()方法实现Point::show()方法:按输出格式输出Point对象Point::showCounter()方法:按格式输出当前程序中Point对象的计数Point::showSumOfPoint()方法:按格式输出程序运行至当前存在过的Point对象总数Input输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内Output对每个Point对象,调用show()方法输出其值,或者用showPoint()函数来输出(通过参数传入的)Point对象的值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。
每个坐标的输出精度为最长16位调用用showCounter()方法和showSumOfPoint()输出Point对象的计数统计,输出格式见sampleC语言的输入输出被禁用Sample Input1,23,32,1Sample OutputPoint : (1, 2)Current : 2 points.Point : (3, 3)Current : 2 points.Point : (2, 1)Current : 2 points.In total : 4 points.Current : 3 points.Point : (0, 0)Point : (1, 1)Point : (0, 0)In total : 6 points.#include#includeusing namespace std;class Point{private: double x_,y_; static int sum,num;public: Point():x_(0),y_(0){num++;sum++;} Point(double xx):x_(xx),y_(1){num++;sum++;} Point(double xx,double yy):x_(xx),y_(yy){num++;sum++;} Point(const Point&p){x_=p.x_;y_=p.y_;num++;sum++;} ~Point(){num--;} void show(){cout<>a>>c>>b) { Point p(a, b); p.show(); p.showCounter(); } q.showSumOfPoint(); Point q1(q), q2(1); Point::showCounter(); showPoint(q1, q2, q); Point::showSumOfPoint();}Problem A: 复数类的构造、析构和输出Description封装一个复数类CPLX,用来处理复数功能和运算,支持以下操作:1. CPLX::CPLX(double, double)构造:参数为实部、虚部,用传入的参数初始化对象,产生一行以“CREATE()”开始的输出,并输出复数的实部和虚部;2. CPLX::~CPLX()析构:产生一行以“RELEASE()”开始的输出,并输出复数的实部和虚部;3. CPLX::print():产生一行以“PRINT()”开始的输出,并以格式“(a, bi)”的形式输出复数;-----------------------------------------------------------------------------你设计一个CPLX类,使得main()函数能够运行并得到正确的输出。
调用格式见append.ccInput输入的第一个整数n,表示用n组测试样例每组测试输入两个实数,分别为实部和虚部Output每组测试数据对应一组输出,两组输出之间用若干“===”分割,详细格式见sampleSample Input52 310 00 1001 -1-7 -7Sample Output=========================CREATE(): 2 3PRINT(): (2, 3i)RELEASE(): 2 3=========================CREATE(): 10 0PRINT(): (10, 0i)RELEASE(): 10 0=========================CREATE(): 0 100PRINT(): (0, 100i)RELEASE(): 0 100=========================CREATE(): 1 -1PRINT(): (1, -1i)RELEASE(): 1 -1=========================CREATE(): -7 -7PRINT(): (-7, -7i)RELEASE(): -7 -7=========================#include#includeusing namespace std;class CPLX{private: double x,y;public : CPLX(double xx,double yy){ x=xx;y=yy; cout<<"CREATE(): "<> cases; for(int i = 1; i <= cases; ++i) { double a, b; cin >> a >> b; cout << "=========================" << endl; CPLX cplx(a, b); cplx.print(); } cout << "=========================" << endl;} Problem B: 复数类的成员访问Description封装一个复数类CPLX,用来处理复数功能和运算,支持以下操作:1. CPLX::CPLX()构造:初始化一个实部、虚部均为0的复数,产生一行以“CREATE()”开始的输出,并输出复数的实部和虚部;2. CPLX::~CPLX()析构:产生一行以“RELEASE()”开始的输出,并输出复数的实部和虚部;3. CPLX::real():返回实部; CPLX::imag():返回虚部。
4. CPLX::real(double):传参修改实部; CPLX::imag(double):传参修改虚部;-----------------------------------------------------------------------------你设计一个CPLX类,使得main()函数能够运行并得到正确的输出调用格式见append.ccInput输入的第一个整数n,表示用n组测试样例每组测试输入两个实数,分别为实部和虚部Output每组测试数据对应一组输出,两组输出之间用若干“===”分割,详细格式见sampleSample Input52 310 00 1001 -1-7 -7Sample Output=========================CREATE(): 0 0Complex real part is 2, imaginary part is 3.RELEASE(): 2 3=========================CREATE(): 0 0Complex real part is 10, imaginary part is 0.RELEASE(): 10 0=========================CREATE(): 0 0Complex real part is 0, imaginary part is 100.RELEASE(): 0 100=========================CREATE(): 0 0Complex real part is 1, imaginary part is -1.RELEASE(): 1 -1=========================CREATE(): 0 0Complex real part is -7, imaginary part is -7.RELEASE(): -7 -7=========================#include#includeusing namespace std;class CPLX{private: double x,y;public : CPLX(double xx=0,double yy=0){this->x=xx;this->y=yy; cout<<"CREATE(): "<> cases; for(int i = 1; i <= cases; ++i) { double a, b; cin >> a >> b; cout << "=========================" << endl; CPLX cplx; cplx.real(a); cplx.imag(b); cout << "Complex real part is " << cplx.real() <<", imaginary part is " << cplx.imag() << "." << endl; } cout << "=========================" << endl;}Problem A: 立体空间中的点(I)Description设计一个平面上的点Point类和3维的点Point_3D类,满足Point_3D类继承自Point类,用于读取输入的数据,输出所构造的两种点的坐标。
设计Point类需支持一下操作:Point::Point()无参构造Point::Point(double,double)两个坐标参数构造Point::showPoint()按格式输出Point对象设计Point_3D类需支持一下操作:Point_3D::Point_3D()无参构造Point_3D::Point_3D(double,double,double)三个坐标参数构造Point_3D::showPoint()按格式输出Point_3D对象你设计Point类和Point_3D类,使得main()函数能够正确运行函数调用格式见append.ccappend.cc中已给出main()函数Input输入的第一个整数n,表示有n组测试数据,后面的输入每行为一组测试数据每组测试数据的第一行是一个整数m,m有两种取值:2、3;m为2时,后面有两个浮点数x、y,表示一个平面上的点的坐标(x,y);m为3时后面有3个浮点数x、y、z,表示一个3维的点的坐标(x,y,z)Output每组测试数据对应一行输出若输入为平面上的点,则输出:“2D Point (x,y)”,x和y为输入的坐标值若输入为3维的点,则输出:“3D Point (x,y,y)”,x、y和z为输入的坐标值。
Sample Input53 1 2 33 0 0 02 -1 13 -1 -1 -12 0 0Sample Output3D Point (1,2,3)3D Point (0,0,0)2D Point (-1,1)3D Point (-1,-1,-1)2D Point (0,0)#includeusing namespace std;class Point{protected:int x,y;public:Point():x(0),y(0){}Point(double x1,double y1):x(x1),y(y1){} void showPoint(){cout<<"2D Point ("<>cases; for(int i = 1; i <= cases; i++) { double x, y, z; int point_type; cin>>point_type; if(point_type == 2) { cin>>x>>y; Point p(x, y); p.showPoint(); } if(point_type == 3) { cin>>x>>y>>z; Point_3D p(x, y, z); p.showPoint(); } }}Problem A: 立体空间中的点(IIDescription设计一个平面上的点Point类和3维的点Point_3D类,满足Point_3D类继承自Point类,用于读取输入的数据,输出所构造的两种点的坐标。
并统计输入的两种点的个数设计Point类需支持一下操作:Point::Point()无参构造Point::Point(double,double)两个坐标参数构造Point::x()返回x坐标Point::y()返回y坐标Point::x(int)修改x坐标并返回Point::y(int)修改y坐标并返回Point::showPoint()按格式输出Point对象Point::showNumber()返回Point对象总数的静态函数设计Point_3D类需支持一下操作:Point_3D::Point_3D()无参构造Point_3D::Point_3D(double,double,double)三个坐标参数构造Point_3D::z()返回z坐标Point_3D::z(int)修改z坐标并返回Point_3D::showPoint()按格式输出Point_3D对象Point_3D::setPoint(double,double,double)根据三个坐标参数修改Point_3D对象的坐标Point_3D::showNumber()返回Point_3D对象总数的静态函数你设计Point类和Point_3D类,使得main()函数能够正确运行。
函数调用格式见append.ccappend.cc中已给出main()函数Input输入的第一个整数n,表示有n组测试数据,后面的输入每行为一组测试数据每组测试数据的第一行是一个整数m,m有两种取值:2、3;m为2时,后面有两个浮点数x、y,表示一个平面上的点的坐标(x,y);m为3时后面有3个浮点数x、y、z,表示一个3维的点的坐标(x,y,z)Output开始部分为由main()函数产生的固定输出,用于测试对象的某些方法的调用情况输出“Test data output :”之后为测试数据对应的输出:每组测试数据对应一行输出若输入为平面上的点,则输出:“2D Point (x,y)”,x和y为输入的坐标值若输入为3维的点,则输出:“3D Point (x,y,y)”,x、y和z为输入的坐标值最后,分别输出总共输入的平面上的点数和3维的点数Sample Input53 1 2 33 0 0 02 -1 13 -1 -1 -12 0 0Sample OutputInvariable test output :3D Point (-100,0,100)Point (0,100,100)Test data output :3D Point (1,2,3)3D Point (0,0,0)2D Point (-1,1)3D Point (-1,-1,-1)2D Point (0,0)Number of 2D Points : 2Number of 3D Points : 3#includeusing namespace std;class Point{protected:int X,Y;static int n; static int m; //n,m被初始化为0public:Point():X(0),Y(0){n++;} //无参构造Point(double x1,double y1):X(x1),Y(y1){n++;}int x(){return X;}int y(){return Y;}int x(int x1){X=x1;return X;}int y(int y1){Y=y1;return Y;}static int showNumber(){return n;}void showPoint(){cout<<"2D Point ("<>cases; for(int i = 1; i <= cases; i++) { double x, y, z; int point_type; cin>>point_type; if(point_type == 2) { cin>>x>>y; Point p(x, y); p.showPoint(); } if(point_type == 3) { cin>>x>>y>>z; Point_3D p(x, y, z); p.showPoint(); } } cout<<"Number of 2D Points : "<
定义Base类的子类Derived,包括1个int类型的属性, 以及满足输出格式要求的构造函数、拷贝构造函数和析构函数Input第1行N>0表示测试用例个数每个测试包括2个int类型的整数Output见样例Sample Input110 20Sample OutputBase = 10 is created.Base = 10 is copied.Base = 10 is created.Derived = 20 is created.Base = 10 is copied.Derived = 20 is copied.Derived = 20 is erased.Base = 10 is erased.Derived = 20 is erased.Base = 10 is erased.Base = 10 is erased.Base = 10 is erased.#includeusing namespace std;class Base{public: int a; Base(int t):a(t){cout<<"Base = "<>cases; for (int i = 0; i < cases; i++) { cin>>data1>>data2; Base base1(data1), base2(base1); Derived derived1(data1, data2), derived2(derived1); }}Problem A: 动物类-抽象类Description每种动物都有自己的叫声,如狗的叫声是"汪汪汪",猫的叫声是"喵喵喵",老鼠的叫声是"吱吱吱"。
构造类Animal,Dog,Cat,Mouse,他们都有成员数据name和sex,表示名字和性别一个成员函数cry(),输出他们的叫声,在main函数中采用多态性调用他们Input动物的姓名和性别Output动物的信息Sample InputJerry mJemmy fTom mDroopy mSample Output我叫Jerry,是一只男老鼠,我的叫声是:吱吱吱!我叫Jemmy,是一只女老鼠,我的叫声是:吱吱吱!我叫Tom,是一只男猫,我的叫声是:喵喵喵!我叫Droopy,是一条男狗,我的叫声是:汪汪汪!#includeusing namespace std;class Animal{public: string name; char sex; virtual void cry()=0;//纯虚函数};//分号必不可少class Mouse: public Animal{public: string name; char sex; Mouse(string n,char m){name=n;sex=m;if(sex=='m') cout<<"我叫"<>nam>>s; Animal *p; Mouse m1(nam, s); p=&m1; p->cry(); cin>>nam>>s; Mouse m2(nam, s); p=&m2; p->cry(); cin>>nam>>s; Cat c1(nam, s); p=&c1; p->cry(); cin>>nam>>s; Dog d1(nam, s); p=&d1; p->cry(); return 0;}Problem B: 平面上的点——Point类 (II)Description在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。
现在我们封装一个“Point类”来实现平面上的点的操作根据“append.cc”,完成Point类的构造方法和show()方法,输出各Point对象的构造和析构次序接口描述:Point::show()方法:按输出格式输出Point对象Input输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内Output输出每个Point对象的构造和析构行为对每个Point对象,调用show()方法输出其值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格每个坐标的输出精度为最长16位输出格式见sampleC语言的输入输出被禁用Sample Input1,23,32,1Sample OutputPoint : (0, 0) is created.Point : (1, 2) is created.Point : (1, 2)Point : (1, 2) is erased.Point : (3, 3) is created.Point : (3, 3)Point : (3, 3) is erased.Point : (2, 1) is created.Point : (2, 1)Point : (2, 1) is erased.Point : (0, 0) is copied.Point : (1, 1) is created.Point : (0, 0)Point : (1, 1)Point : (0, 0)Point : (1, 1) is erased.Point : (0, 0) is erased.Point : (0, 0) is erased.#include#includeusing namespace std;class Point {private: double x_,y_;public:Point(double xx,double yy){ x_=xx; y_=yy; cout<<"Point : ("<>a>>c>>b) { Point p(a, b); p.show(); } Point q1(q), q2(1); q1.show(); q2.show(); q.show();}Problem C: 平面上的点——Point类 (III)Description在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。
现在我们封装一个“Point类”来实现平面上的点的操作根据“append.cc”,完成Point类的构造方法和show()方法,输出各Point对象的构造和析构次序实现showPoint()函数接口描述:showPoint()函数按输出格式输出Point对象,调用Point::show()方法实现Point::show()方法:按输出格式输出Point对象Input输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内Output输出每个Point对象的构造和析构行为showPoint()函数用来输出(通过参数传入的)Point对象的值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格每个坐标的输出精度为最长16位输出格式见sampleC语言的输入输出被禁用Sample Input1,23,32,1Sample OutputPoint : (0, 0) is created.Point : (1, 2) is created.Point : (1, 2) is copied.Point : (1, 2)Point : (1, 2) is erased.Point : (1, 2) is erased.Point : (3, 3) is created.Point : (3, 3) is copied.Point : (3, 3)Point : (3, 3) is erased.Point : (3, 3) is erased.Point : (2, 1) is created.Point : (2, 1) is copied.Point : (2, 1)Point : (2, 1) is erased.Point : (2, 1) is erased.Point : (0, 0) is copied.Point : (1, 1) is created.Point : (0, 0) is copied.Point : (1, 1) is copied.Point : (0, 0) is copied.Point : (0, 0)Point : (1, 1)Point : (0, 0)Point : (0, 0) is erased.Point : (1, 1) is erased.Point : (0, 0) is erased.Point : (1, 1) is erased.Point : (0, 0) is erased.Point : (0, 0) is erased.#include#includeusing namespace std;class Point{private: double x,y;public: Point(double xx=0) { x=y=xx; cout<<"Point : ("<>a>>c>>b) { Point p(a, b); showPoint(p); } Point q1(q), q2(1); showPoint(q1, q2, q);}Problem A: 编写函数:Swap (I) (Append Code)Description编写用来交换两个数的函数,使得“Append Code”中的main()函数能正确运行。
用C实现三个函数int_swap()、dbl_swap()、SWAP(),其中SWAP()是个带参宏用C++实现两个函数,都以swap()命名以上函数的调用格式见“Append Code”这里不给出函数原型,它们的参数请通过main()函数自行确定Input输入为4行,每行2个数Output输出为4行,每行2个数每行输出的两数为每行输入的逆序Sample Input12 579 -3-12 43 5Sample Output57 12-3 94 -125 3#include using namespace std;void swap (int &a,int &b){ int temp; temp=a; a=b; b=temp;}void swap(int *a,int *b){ int temp; temp=*a; *a=*b; *b=temp;}void swap (double &a,double &b){ double temp; temp=a; a=b; b=temp;}void swap(double *a,double *b){ double temp; temp=*a; *a=*b; *b=temp;}int main(){ int x1, y1; cin>>x1>>y1; swap(&x1, &y1); cout<>x1>>y1; swap(x1, y1); cout<>x2>>y2; swap(&x2, &y2); cout<>x2>>y2; swap(x2, y2); cout<