http://wenku.baidu.com/link?url=uhOTl-VbZxh5ULw0nmP1BWMM-meHJ_vICCtIJxO1sT_kESHONuFD2dVihQxfI94k-pgHE7Gw0z0OGmlfPvBSEFF4a8ubHQIyAVlEXdw5X1O
2024-11-05 06:35:34
#include <iostream>
#include <cmath>
using namespace std;
double const PI = 3.14159265358979;
class Point {
private :
double x;
double y;
public :
Point(double nx = 0, double ny = 0) : x(nx),y(ny) {}
double Getx() const { return x; }
double Gety() const { return y; }
void Setx(double nx) { x = nx; }
void Sety(double ny) { y = ny; }
friend double Disdance(const Point &a, const Point &b) {
double mt1 = (a.x - b.x) * (a.x - b.x);
double mt2 = (a.y - b.y) * (a.y - b.y);
return sqrt(mt1 + mt2);
}
void Show() const {
cout << "(" << x << "," << y << ")";
}
};
class Orthogon {
private :
double length; // 矩形的长度
double width; // 矩形的宽度
public :
Orthogon(double len = 0, double w = 0) : length(len),width(w) {}
void Set(double len, double w) {length = len; width = w; }
double GetGirth() const { return 2*(length + width); }
double GetBasalArea() const { return length * width; }
void Show() const {
cout << "矩形底边长: " << length << endl;
cout << "矩形底边宽: " << width << endl;
cout << "矩形底面周长: " << GetGirth() << endl;
cout << "矩形底面面积: " << GetBasalArea() << endl;
}
};
class Circle {
private :
Point centre;
double radius;
public :
Circle() { centre = Point(0,0); radius = 0; }
Circle(const Point ¢er,double r = 0) : centre(center),radius(r) {}
void Set(const Point &pcen, double r) {centre = pcen; radius = r; }
double GetGirth() const { return 2 * PI * radius; }
double GetBasalArea() const { return PI * radius * radius; }
void Show() const {
cout << "圆心坐标: "; centre.Show();cout << endl;
cout << "半径: " << radius << endl;
cout << "矩形底面周长: " << GetGirth() << endl;
cout << "矩形底面面积: " << GetBasalArea() << endl;
}
};
class Triangle {
private :
Point A;
Point B;
Point C;
public :
Triangle() { A = 0; B = 0; C = 0; }
Triangle(const Point &pa, const Point &pb, const Point &pc) {
A = pa; B = pb; C = pc;
}
void Set(const Point &pa, const Point &pb, const Point &pc) {
A = pa; B = pb; C = pc;
}
double GetAB() const { return Disdance(A,B); }
double GetBC() const { return Disdance(B,C); }
double GetCA() const { return Disdance(C,A); }
double GetGirth() const { return GetAB() + GetBC() + GetCA(); }
double GetBasalArea() const {
double a,b,c,p;
a = GetBC(); b = GetCA(); c = GetAB();
p = (a + b + c) / 2.0;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
void Show() const {
cout << "顶点A: ";A.Show();cout << ", ";
cout << "顶点B: ";B.Show();cout << ", ";
cout << "顶点C: ";C.Show();cout << endl;
cout << "边长: " << GetAB() << ", " << GetBC() << ", " << GetCA() << endl;
cout << "周长: " << GetGirth() << endl;
cout << "面积: " << GetBasalArea() << endl;
}
};
template<typename Type>
class Podetium {
private :
Type base;
double high;
public :
Podetium(const Type &bs, double h) : base(bs),high(h) {}
double GetVolume() const { return high * base.GetBasalArea(); }
double GetSurfaceArea() const {
return base.GetGirth() * high + 2 * base.GetBasalArea();
}
void Show() const {
base.Show();
cout << "柱体高: " << high << endl;
cout << "柱体表面积: " << GetSurfaceArea() << endl;
cout << "柱体体积: " << GetVolume() << endl;
}
};
int main() {
Point center(2,3); // 圆心坐标
Point po[3]; // 三角形顶点
Orthogon orth; // 声明矩形变量
Circle cle; // 声明圆变量
Triangle trg; // 声明三角形变量
double x,y,len,w,r,h;
cout << "圆柱的半径:"; cin >> r;
cout << "圆柱的高:"; cin >> h;
cle.Set(center,r); // 构造圆
Podetium<Circle> pdtc(cle,h); // 构造圆柱体
pdtc.Show(); // 显示圆柱信息
cout << "长方体底边长:"; cin >> len;
cout << "长方体底边宽:"; cin >> w;
cout << "长方体的高:"; cin >> h;
orth.Set(len,w);
Podetium<Orthogon> pdto(orth,h);
pdto.Show();
for(char c = 'A'; c <= 'C'; ++c) {
cout << "三棱柱 " << c << " 点坐标:";
cin >> x >> y;
po[c - 'A'].Setx(x);
po[c - 'A'].Sety(y);
}
cout << "三棱柱的高:";
cin >> h;
trg.Set(po[0],po[1],po[2]);
Podetium<Triangle> pdtt(trg,h);
pdtt.Show();
return 0;
}
2024-11-05 09:13:39