C++测绘程序设计课后练习题3编程
- 一、前言
- 一、具体内容
- 练习题3:18、设计一个圆类circle,其成员变量为半径r,成员函数用于计算圆的周长与面积,为圆编写不带参数和带一个参数的构造函数。要求:成员变量是私有的,成员函数是公有的,π定义为全局变量。编写main函数测试circle类的应用功能。
- 19、以18题的circle类为基类,设计圆柱体类,要求利用圆类的成员变量与成员函数实现圆柱体的表面积与体积计算功能。
- 20、为19题圆柱体类设计构造函数与析构函数,要求一个构造函数不带参数、另一个构造函数带两个参数。
- 21、以18题的circle类为基础,设计同心直管类,要求可以计算管的内壁面积、外壁面积、所用材料的体积以及管内部空间体积。要求:基于两个circle子对象进行本类设计,成员变量为私有成员、成员函数为公有成员,为本类编写构造函数。设计main函数测试该类。
- 22、设计一个长方形类,定义私有的double类型的成员变量表示长方形的长和宽,成员函数用来计算长方形的周长与面积。要求:为长方形类定义两个构造函数,一个为不带参数的构造函数,将长方形的长和宽分别设为0;另一个构造函数带两个参数,将长方形初始化为用户需要的具体状态。
- 23、以22题的长方形类为基类,设计长方体类,要求在长方体类中用长方形的周长与面积计算函数实现长方体的表面积与体积计算功能。要求:在长方体类中定义一个成员变量表示长方体的高,长和宽使用长方形类中的两个成员变量;为长方体类编写构造函数。
- 24、以23题的长方体类为基类,创建正方体类。思考,正方体应当以什么样的方式继承长方体类,为正方体类编写构造函数。
- 25、以23题定义的长方体类为基础,创建等厚度长方体容器类,要求可以计算该容器的外表面积、内表面积、容器的容积以及容器自身所用材料的体积。要求,将容器的长宽高以及厚度声明为该类的私有成员。
- 26、为以上定义的长方体类、正方体类、圆柱体类定义一个抽象类——立体类。在立体类中,定义两个公有的纯虚函数分别表示立体的表面积与体积计算功能。声明长方体类等为立体类的直接派生类,以纯虚函数的格式重新定义其表面积与体积的计算函数。
- 27、以26题程序为基础,在main()函数中定义一个立体类的动态指针数组,分别定义圆柱体、长方体与正方体对象若干个。将立体指针数组中的每个指针分别指向这些立体C++对象,然后通过一个循环语句计算并输出所有立体对象的表面积与体积。
- 28、定义成员变量为double类型数据的一个三维向量类,通过运算符重载方法分别定义三维向量的加、减、叉乘等运算符计算功能,编写main()函数测试这些运算符的计算功能。
- 29、以19题的圆柱体类为基础,以建立子对象方式设计等圆柱体封闭容器类,设容器的外半径为R、高为H、厚度为d,要求设计成员函数计算容器所用材料的体积以及内部空间体积。为本类设计构造函数,要求成员变量为私有成员、成员函数为公有成员。设计main()函数测试该类。
- 30、修改例3.5题的折线类为闭合图形类,要求计算闭合图形的周长。
- 31、以19题的圆柱体类为基础,设计一个哑铃类(由3个圆柱体构成),要求哑铃的手柄以一个子对象表示,其余部分用圆柱类做基类表示。为哑铃类编写构造函数,编写成员函数计算哑铃的体积和表面积。
一、前言
C++测绘程序设计(长安大学编)练习题3第18-31题代码,仅供参考。
一、具体内容
练习题3:18、设计一个圆类circle,其成员变量为半径r,成员函数用于计算圆的周长与面积,为圆编写不带参数和带一个参数的构造函数。要求:成员变量是私有的,成员函数是公有的,π定义为全局变量。编写main函数测试circle类的应用功能。
#include<iostream>
using namespace std;
double PI = 3.14159265; //全局变量PI
class circle //圆类circle
{
private:
double r; //私有成员变量半径r
public: //公有成员函数
double Ccalculate() { return 2 * PI * r; } //计算圆的周长
double Scalculate() { return PI * r * r; } //计算圆的面积
circle() { r = 0; } //不带参数的构造函数
circle(double R) { r = R; } //带一个参数的构造函数
};
int main()
{
circle A, B(3), * C;//用circle类定义对象和对象指针
cout << "圆A的周长和面积分别为:" << A.Ccalculate() <<" "<< A.Scalculate() << endl;
cout << "圆B的周长和面积分别为:" << B.Ccalculate() << " " << B.Scalculate() << endl;
C = &B;//C指向B
cout << "圆C的周长和面积分别为:" << C->Ccalculate() << " " << C->Scalculate() << endl;//对象指针访问用"->"
}
19、以18题的circle类为基类,设计圆柱体类,要求利用圆类的成员变量与成员函数实现圆柱体的表面积与体积计算功能。
#include<iostream>
using namespace std;
double PI = 3.14159265;
class circle
{
protected:
double r;
public:
double Ccalculate(){ return 2 * PI * r; }
double Scalculate(){ return PI * r * r; }
circle() {r = 0; }
circle(double R) {r = R; }
};
class cyliner :public circle
{
private:
double h;
public:
cyliner() { h = 0; }
cyliner(double R,double H):circle(R)
{
h = H;
}
double getV()
{
return circle::Scalculate() * h;
}
double getS()
{
return circle::Scalculate() * 2 + circle::Ccalculate() * h;
}
};
int main()
{
cyliner c1;
cyliner c2(3, 4);
cyliner* c3;
cout << "圆柱体c1的体积和面积分别为:" << c1.getV() << " " << c1.getS() << endl;
cout << "圆柱体c2的体积和面积分别为:" << c2.getV() << " " << c2.getS() << endl;
c3 = &c2;
cout << "圆柱体c3的体积和面积分别为:" << c3->getV() << " " << c3->getS() << endl;
return 0;
}
20、为19题圆柱体类设计构造函数与析构函数,要求一个构造函数不带参数、另一个构造函数带两个参数。
#include<iostream>
using namespace std;
double PI = 3.14159265;
class circle
{
protected:
double r;
public:
double Ccalculate() { return 2 * PI * r; }
double Scalculate() { return PI * r * r; }
circle() { r = 0; }
circle(double R) { r = R; }
};
class cyliner :public circle
{
private:
double h;
public:
cyliner() { h = 0; }//不带参数构造函数
cyliner(double R, double H) :circle(R) { h = H; }//带参数构造函数
double getV() { return circle::Scalculate() * h; }
double getS() { return circle::Scalculate() * 2 + circle::Ccalculate() * h; }
~cyliner() { cout << "Cylinder类析构完成!" << endl; }//析构函数
};
int main()
{
cyliner c1;
cyliner c2(3, 4);
cyliner* c3;
cout << "圆柱体c1的体积和面积分别为:" << c1.getV() << " " << c1.getS() << endl;
cout << "圆柱体c2的体积和面积分别为:" << c2.getV() << " " << c2.getS() << endl;
c3 = &c2;
cout << "圆柱体c3的体积和面积分别为:" << c3->getV() << " " << c3->getS() << endl;
return 0;
}
21、以18题的circle类为基础,设计同心直管类,要求可以计算管的内壁面积、外壁面积、所用材料的体积以及管内部空间体积。要求:基于两个circle子对象进行本类设计,成员变量为私有成员、成员函数为公有成员,为本类编写构造函数。设计main函数测试该类。
#include<iostream>
using namespace std;
double PI = 3.14159265;
class circle
{
protected:
double r;
public:
double Ccalculate() { return 2 * PI * r; }
double Scalculate() { return PI * r * r; }
circle() { r = 0; }
circle(double R) { r = R; }
};
class tube
{
private:
circle incircle;//内圆
circle outcircle;//外圆
double hight;//同心直管长
public:
tube() { hight = 0; }
tube(double inr, double outr, double H) :incircle(inr), outcircle(outr), hight(H) {}
double getinnerS() { return incircle.Ccalculate() * hight; }
double getouterS() { return outcircle.Ccalculate() * hight; }
double getinnerV() { return incircle.Scalculate() * hight; }
double getmaterialV() { return outcircle.Scalculate() * hight - incircle.Scalculate() * hight; }
};
int main()
{
tube A;
tube B(2, 3, 4);
tube* C;
C = &B;
cout << "同心直管A的内壁面积、外壁面积、材料体积、管内部空间体积分别为:" << A.getinnerS() << " " << A.getouterS()
<<" "<<A.getinnerV()<<" " <<A.getmaterialV()<< endl;
cout << "同心直管B的内壁面积、外壁面积、材料体积、管内部空间体积分别为:" << B.getinnerS() << " " << B.getouterS()
<< " " << B.getinnerV() << " " << B.getmaterialV() << endl;
cout << "同心直管C的内壁面积、外壁面积、材料体积、管内部空间体积分别为:" << C->getinnerS() << " " << C->getouterS()
<< " " << C->getinnerV() << " " << C->getmaterialV() << endl;
}
22、设计一个长方形类,定义私有的double类型的成员变量表示长方形的长和宽,成员函数用来计算长方形的周长与面积。要求:为长方形类定义两个构造函数,一个为不带参数的构造函数,将长方形的长和宽分别设为0;另一个构造函数带两个参数,将长方形初始化为用户需要的具体状态。
#include<iostream>
using namespace std;
class rectangle
{
private:
double a;
double b;
public:
rectangle() { a = 0; b = 0; }
rectangle(double A, double B) { a = A; b = B; }
double getC() { return 2 * (a + b); }
double getS() { return a * b; }
};
int main()
{
rectangle r1;
rectangle r2(3, 4);
rectangle* r3;
r3 = &r2;
cout << "长方形r1周长面积分别为" << r1.getC() << " " << r1.getS() << endl;
cout << "长方形r2周长面积分别为" << r2.getC() << " " << r2.getS() << endl;
cout << "长方形r3周长面积分别为" << r3->getC() << " " << r3->getS() << endl;
}
23、以22题的长方形类为基类,设计长方体类,要求在长方体类中用长方形的周长与面积计算函数实现长方体的表面积与体积计算功能。要求:在长方体类中定义一个成员变量表示长方体的高,长和宽使用长方形类中的两个成员变量;为长方体类编写构造函数。
#include<iostream>
using namespace std;
class rectangle
{
protected:
double a;
double b;
public:
rectangle() { a = 0; b = 0; }
rectangle(double A, double B) { a = A; b = B; }
double getC() { return 2 * (a + b); }
double getS() { return a * b; }
};
class cuboid :public rectangle
{
private:
double c;
public:
cuboid() { c = 0; }
cuboid(double A,double B,double C):rectangle(A,B) { c = C; }
double SurfaceArea() { return 2 * rectangle::getS() + a * c * 2 + b * c * 2; }
double getV() { return rectangle::getS() * c; }
};
int main()
{
cuboid c1;
cuboid c2(6,5,4);
cuboid* c3;
c3 = &c2;
cout << "长方体c1体积面积分别为" << c1.getV() << " " << c1.SurfaceArea() << endl;
cout << "长方体c2体积面积分别为" << c2.getV() << " " << c2.SurfaceArea() << endl;
cout << "长方体c3体积面积分别为" << c3->getV() << " " << c3->SurfaceArea() << endl;
}
24、以23题的长方体类为基类,创建正方体类。思考,正方体应当以什么样的方式继承长方体类,为正方体类编写构造函数。
#include<iostream>
using namespace std;
class rectangle
{
protected:
double a;
double b;
public:
rectangle() { a = 0; b = 0; }
rectangle(double A, double B) { a = A; b = B; }
double getC() { return 2 * (a + b); }
double getS() { return a * b; }
};
class cuboid :public rectangle
{
protected:
double c;
public:
cuboid() { c = 0; }
cuboid(double A, double B, double C) :rectangle(A, B) { c = C; }
double SurfaceArea() { return 2 * rectangle::getS() + a * c * 2 + b * c * 2; }
double getV() { return rectangle::getS() * c; }
};
class cube :public cuboid
{
public:
cube() {}
cube(double len) :cuboid(len, len, len){}
};
int main()
{
cube c1;
cube c2(4);
cube* c3;
c3 = &c2;
cout << "正方体c1体积面积分别为" << c1.getV() << " " << c1.SurfaceArea() << endl;
cout << "正方体c2体积面积分别为" << c2.getV() << " " << c2.SurfaceArea() << endl;
cout << "正方体c3体积面积分别为" << c3->getV() << " " << c3->SurfaceArea() << endl;
}
25、以23题定义的长方体类为基础,创建等厚度长方体容器类,要求可以计算该容器的外表面积、内表面积、容器的容积以及容器自身所用材料的体积。要求,将容器的长宽高以及厚度声明为该类的私有成员。
#include<iostream>
using namespace std;
class rectangle
{
protected:
double a;
double b;
public:
rectangle() { a = 0; b = 0; }
rectangle(double A, double B) { a = A; b = B; }
double getC() { return 2 * (a + b); }
double getS() { return a * b; }
};
class cuboid :public rectangle
{
private:
double c;
public:
cuboid() { c = 0; }
cuboid(double A, double B, double C) :rectangle(A, B) { c = C; }
double SurfaceArea() { return 2 * rectangle::getS() + a * c * 2 + b * c * 2; }
double getV() { return rectangle::getS() * c; }
};
class cuboidContainer
{
private:
double length;
double wide;
double hight;
double thickness;
cuboid c;
public:
cuboidContainer(){ length = 0; wide = 0; hight = 0; thickness = 0; }
cuboidContainer(double A, double B, double C, double thick) :c(A, B, C) { length = A; wide = B; hight = C; thickness = thick; }
double getoutS() { return c.SurfaceArea(); }
double getinS();
double getContainerV();
double getContainermaterial() { return c.getV() - getContainerV(); }
};
double cuboidContainer::getinS()
{
double inlength = length - 2 * thickness;
double inwide = wide - 2 * thickness;
double inhight = hight - 2 * thickness;
return inlength * inwide * 2 + inlength * inhight * 2 + inwide * inhight * 2;
}
double cuboidContainer::getContainerV()
{
double inlength = length - 2 * thickness;
double inwide = wide - 2 * thickness;
double inhight = hight - 2 * thickness;
return inlength * inwide * inhight;
}
int main()
{
cuboidContainer r1;
cuboidContainer r2(6, 5, 4, 0.2);
cuboidContainer* r3;
r3 = &r2;
cout << "长方体容器r1外表面积、内表面积、容器的容积、容器材料体积分别为"
<< r1.getoutS() << " " << r1.getinS() <<" "<<r1.getContainerV()<<" "<<r1.getContainermaterial()<< endl;
cout << "长方体容器r2外表面积、内表面积、容器的容积、容器材料体积分别为"
<< r2.getoutS() << " " << r2.getinS() << " " << r2.getContainerV() << " " << r2.getContainermaterial() << endl;
cout << "长方体容器r3外表面积、内表面积、容器的容积、容器材料体积分别为"
<< r3->getoutS() << " " << r3->getinS() << " " << r3->getContainerV() << " " << r3->getContainermaterial() << endl;
}
26、为以上定义的长方体类、正方体类、圆柱体类定义一个抽象类——立体类。在立体类中,定义两个公有的纯虚函数分别表示立体的表面积与体积计算功能。声明长方体类等为立体类的直接派生类,以纯虚函数的格式重新定义其表面积与体积的计算函数。
#include <iostream>
using namespace std;
class Solid
{
public:
virtual double getSurfaceS() = 0;
virtual double getV() = 0;
};
class Cuboid : public Solid
{
protected:
double length;
double width;
double height;
public:
Cuboid() { length = 0; width = 0; height = 0.0; }
Cuboid(double len, double wid, double hei) { length = len; width = wid; height = hei; }
double getSurfaceS() { return 2 * (length * width + length * height + width * height); }
double getV() { return length * width * height; }
};
int main() {
Cuboid c1(0.0, 0.0, 0.0);
Cuboid c2(11.1, 22.2, 33.3);
Cuboid* c3;
c3 = &c2;
cout << "c1表面积: " << c1.getSurfaceS() << " " << "体积: " << c1.getV() << endl;
cout << "c2表面积: " << c2.getSurfaceS() << " " << "体积: " << c2.getV() << endl;
cout << "c3表面积: " << c3->getSurfaceS() <<" " << "体积: " << c3->getV() << endl;
return 0;
}
27、以26题程序为基础,在main()函数中定义一个立体类的动态指针数组,分别定义圆柱体、长方体与正方体对象若干个。将立体指针数组中的每个指针分别指向这些立体C++对象,然后通过一个循环语句计算并输出所有立体对象的表面积与体积。
#include <iostream>
using namespace std;
double PI = 3.14159265;
class Solid
{
public:
virtual double getSurfaceArea() = 0;
virtual double getVolume() = 0;
};
class Rectangle : public Solid
{
protected:
double length;
double width;
public:
Rectangle() { length = 0.0; width = 0.0; }
Rectangle(double len, double wid) { length = len; width = wid; }
double getPerimeter() { return 2 * (length + width); }
double getArea() { return length * width; }
};
class Cuboid : public Rectangle
{
protected:
double height;
public:
Cuboid() { height = 0.0; }
Cuboid(double len, double wid, double hei) : Rectangle(len, wid) { height = hei; }
double getSurfaceArea() { return 2 * (length * width + length * height + width * height); }
double getVolume() { return length * width * height; }
};
class Cube : public Cuboid
{
public:
Cube() {}
Cube(double len) : Cuboid(len, len, len) {}
};
class Cylinder : public Solid
{
private:
double radius;
double height;
public:
Cylinder() { radius = 0.0; height = 0.0; }
Cylinder(double r, double h) { radius = r; height = h; }
double getSurfaceArea() { return 2 * PI * radius * (radius + height); }
double getVolume() { return PI * radius * radius * height; }
};
int main() {
Solid* solids[5]; // 定义指针数组
solids[0] = new Cuboid(10.0, 20.0, 30.0);
solids[1] = new Cube(5.0);
solids[2] = new Cylinder(2.0, 5.0);
solids[3] = new Cuboid(15.0, 25.0, 35.0);
solids[4] = new Cube(10.0);
for (int i = 0; i < 5; i++)
{
cout << "表面积: " << solids[i]->getSurfaceArea() << endl;
cout << "体积: " << solids[i]->getVolume() << endl;
delete solids[i]; // 释放内存
}
return 0;
}
28、定义成员变量为double类型数据的一个三维向量类,通过运算符重载方法分别定义三维向量的加、减、叉乘等运算符计算功能,编写main()函数测试这些运算符的计算功能。
#include <iostream>
using namespace std;
class Vector3D {
private:
double x, y, z;
public:
Vector3D(double _x = 0, double _y = 0, double _z = 0) : x(_x), y(_y), z(_z) {}
// 重载加号运算符
Vector3D operator+(const Vector3D& v) const { return Vector3D(x + v.x, y + v.y, z + v.z); }
// 重载减号运算符
Vector3D operator-(const Vector3D& v) const { return Vector3D(x - v.x, y - v.y, z - v.z); }
// 重载叉乘运算符
Vector3D operator*(const Vector3D& v) const { return Vector3D(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); }
// 重载输出运算符
friend ostream& operator<<(ostream& os, const Vector3D& v)
{
os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return os;
}
};
int main() {
Vector3D v1(1, 2, 3);
Vector3D v2(4, 5, 6);
cout << "v1 = " << v1 << endl;
cout << "v2 = " << v2 << endl;
Vector3D v3 = v1 + v2;
cout << "v1 + v2 = " << v3 << endl;
Vector3D v4 = v1 - v2;
cout << "v1 - v2 = " << v4 << endl;
Vector3D v5 = v1 * v2;
cout << "v1 x v2 = " << v5 << endl;
return 0;
}
29、以19题的圆柱体类为基础,以建立子对象方式设计等圆柱体封闭容器类,设容器的外半径为R、高为H、厚度为d,要求设计成员函数计算容器所用材料的体积以及内部空间体积。为本类设计构造函数,要求成员变量为私有成员、成员函数为公有成员。设计main()函数测试该类。
#include <iostream>
using namespace std;
const double PI = 3.1415926;
class Circle
{
protected:
double r;
public:
Circle() { r = 0.0; }
Circle(double radius) { r = radius; }
double getPerimeter() { return 2 * PI * r; }
double getArea() { return PI * r * r; }
};
class Cylinder : public Circle
{
private:
double h;
public:
Cylinder() { h = 0.0; }
Cylinder(double radius, double height) : Circle(radius) { h = height; }
double getSurfaceArea() { return 2 * Circle::getArea() + Circle::getPerimeter() * h; }
double getVolume() { return Circle::getArea() * h; }
};
class ClosedCylinder
{
private:
double R, H, d;
Cylinder innerCylinder;
Cylinder outerCylinder;
public:
ClosedCylinder(double r, double h, double thickness)
{
R = r + thickness;
H = h;
d = thickness;
innerCylinder = Cylinder(r, h);
outerCylinder = Cylinder(R, h);
}
double getMaterialVolume()
{
double outerVolume = outerCylinder.getVolume();
double innerVolume = innerCylinder.getVolume();
return outerVolume - innerVolume;
}
double getInnerVolume() { return innerCylinder.getVolume(); }
};
int main()
{
ClosedCylinder cc(2.0, 3.0, 0.5);
cout << "Material volume: " << cc.getMaterialVolume() << endl;
cout << "Inner volume: " << cc.getInnerVolume() << endl;
return 0;
}
30、修改例3.5题的折线类为闭合图形类,要求计算闭合图形的周长。
#include<Angle.h>
#include<string>
using namespace std;
class Point
{
public:
//声明构造函数
Point(string nam, double x, double y); // 构造函数
Point(Point& A); // 拷贝构造函数
Point(); // 缺省构造函数
~Point() {} // 析构函数
void SetName(string name); // 设置点名
void SetData(double x, double y); // 设置坐标
void ShowPoint(); // 显示点信息
double dist(); // 计算到原点的距离
friend void main(); // 在main函数中需要访问Point类对象的坐标X、Y
friend class Line; // 在Line类中需要访问Point对象的私有成员
private:
string name; // 点名
double X; // X坐标
double Y; // Y坐标
};
Point::Point(string nam, double x, double y)
{
SetName(nam);
SetData(x, y);
}
Point::Point(Point& A)
{
name = A.name;
X = A.X;
Y = A.Y;
}
Point::Point()
{
name = "";
X = Y = 0;
}
void Point::SetData(double x, double y)
{
X = x;
Y = y;
}
void Point::ShowPoint()
{
cout.width(6);
cout << name << " X=" << X << " Y=" << Y << endl;
}
void Point::SetName(string nam)
{
name = nam;
}
double Point::dist()
{
double dis = sqrt(X * X + Y * Y);
return dis;
}
class Line
{
public:
Line() {}
Line(Point& start, Point& end); // 两点设一边
void setLine(Point p1,Point p2); // 两点四坐标设一边
void Set_Range_Name(string startp, string endp); // 设置边名
void ShowRange(); // 输出一条边的信息
Angle A0(); // 计算边的坐标方位角
double Dist(); // 计算边的边长
private:
Point Startp; // 边的起点
Point Endp; // 边的终点
};
Line::Line(Point& st, Point& end)
{
Startp = st;
Endp = end;
}
void Line ::setLine(Point p1, Point p2)
{
Startp.X = p1.X;
Startp.Y = p1.Y;
Endp.X = p2.X;
Endp.Y = p2.Y;
}
double Line::Dist()
{
double dx, dy, S;
dx = Endp.X - Startp.X;
dy = Endp.Y - Startp.Y;
S = sqrt(dx * dx + dy * dy);
return S;
}
Angle Line::A0()
{
double dx, dy, S;
dx = Endp.X - Startp.X;
dy = Endp.Y - Startp.Y;
S = Dist();
if (S == 0) return 0;
Angle A = acos(dx / S);
if (dy < 0) A = 2 * 3.141592653589793 - A;
return A;
}
void Line::ShowRange()
{
ofstream ou("OUT.txt", ios::app); ou.precision(10);
cout.precision(10);
cout << " 起点:" << Startp.name << " X=" << Startp.X << " Y=" << Startp.Y << endl;
cout << " 终点:" << Endp.name << " X=" << Endp.X << " Y=" << Endp.Y << endl;
cout << " 边长:" << Dist() << " 坐标方位角:" << A0() << endl << endl;
ou << " 起点:" << Startp.name << " X=" << Startp.X << " Y=" << Startp.Y << endl;
ou << " 终点:" << Endp.name << " X=" << Endp.X << " Y=" << Endp.Y << endl;
ou << " 边长:" << Dist() << " 坐标方位角:" << A0() << endl << endl;
ou.close();
}
void Line::Set_Range_Name(string startp, string endp)
{
Startp.name = startp;
Endp.name = endp;
}
string stringAddition( string str1, string str2)
{
return str1 + str2; // 使用 + 运算符进行字符串连接
}
void main()
{
int n = 2;
cout << "请输入边的个数:" << endl;
cin >> n;
if (n < 3)
{
cout << "不构成闭合图形" << endl;
exit (-1); //点个数少于3个直接退出程序
}
double C = 0; //周长初始化为0
Point* points = new Point[n]; //点数组
Line* L = new Line[n]; //线段数组
int k = 0;
for (int i = 0; i < n; i++)
{
int q = i;
string str;
str = to_string(q) ;
points[i].SetName(stringAddition("X" ,str)); //字符串连接给点命名X0、X1、X2、X3...
cout << "请依次输入第"<<i+1<<"个点坐标:"; //屏幕手动输入各点坐标
double x, y;
cin >> x >> y;
points[i].SetData(x, y); //将各点坐标存入点类数组
}
for (int i = 0; i < n; i++)
{
int j = i+1;
if (j >= n || j > n) //闭合图形最后一个点与第一个点连成线段时的处理
{
L[k].setLine(points[n-1], points[0]);
L[k].Set_Range_Name(points[n-1].name, points[0].name);
L[k].ShowRange();
C += L[k].Dist();
break; //n条线段处理完成时退出循环
}
L[k].setLine(points[i], points[j]);
L[k].Set_Range_Name(points[i].name , points[j].name );
L[k].ShowRange();
C += L[k].Dist();
k++;
}
cout << "闭合图形周长为:" << C; //屏幕输出闭合图形周长
delete[]points;
delete[]L; //释放内存空间
}
31、以19题的圆柱体类为基础,设计一个哑铃类(由3个圆柱体构成),要求哑铃的手柄以一个子对象表示,其余部分用圆柱类做基类表示。为哑铃类编写构造函数,编写成员函数计算哑铃的体积和表面积。
#include <iostream>
using namespace std;
const double PI = 3.1415926;
class Circle
{
protected:
double r;
public:
Circle() { r = 0.0; }
Circle(double radius) { r = radius; }
double getPerimeter() { return 2 * PI * r; }
double getArea() { return PI * r * r; }
};
class Cylinder : public Circle
{
private:
double h;
public:
Cylinder() { h = 0.0; }
Cylinder(double radius, double height) : Circle(radius) { h = height; }
double getSurfaceArea() { return 2 * Circle::getArea() + Circle::getPerimeter() * h; }
double getVolume() { return Circle::getArea() * h; }
};
class Dumbbell
{
private:
double r, h, handleLength;
Cylinder leftCylinder, rightCylinder, handle;
public:
Dumbbell(double radius, double height, double handleLength)
{
r = radius;
h = height;
this->handleLength = handleLength;
leftCylinder = Cylinder(r, h);
rightCylinder = Cylinder(r, h);
handle = Cylinder(r / 2, handleLength);
}
double getVolume() { return leftCylinder.getVolume() + rightCylinder.getVolume() + handle.getVolume(); }
double getSurfaceArea() { return leftCylinder.getSurfaceArea() + rightCylinder.getSurfaceArea() + 2 * handle.getSurfaceArea(); }
};
int main() {
Dumbbell d(2.0, 3.0, 5.0);
cout << "Dumbbell volume: " << d.getVolume() << endl;
cout << "Dumbbell surface area: " << d.getSurfaceArea() << endl;
return 0;
}