C++ 完善求三维空间内的表面积和体积
//加入了对长方体,立方体,球体,圆柱及三棱柱的体积与表面积计算
#include"iostream"
#include"cmath"
using namespace std;class line//线
{
public:line(float len);virtual float area() = 0;virtual float volume() = 0;
protected:float m_len;
};
line::line(float len):m_len(len)
{}//类作为纯虚函数,无法被实例化class rec :public line//方形
{
public:rec(float len, float width);float area();
protected:float m_width;
};
rec::rec(float len, float width):line(len), m_width(width)
{}
float rec::area()
{return m_len * m_width;
}class cuboid :public rec//长方体
{
public:cuboid(float len, float width, float height);float area();float volume();
protected:float m_height;
};
cuboid::cuboid(float len, float width, float height) :rec(len, width), m_height(height)
{}
float cuboid::area()
{return 2 * (m_len * m_width + m_len * m_height + m_height * m_width);}
float cuboid::volume()
{return m_len * m_height * m_width;
}class cube :public cuboid//立方体
{
public :cube(float len);
};
cube::cube(float len) :cuboid(len, len, len)
{}class cir :public line//圆
{
public:cir(float len);float area();
};
cir::cir(float len) :line(len)
{}
float cir::area()
{float q = acos(-1);return m_len * m_len * q;
}class glo :public cir//球体
{
public:glo(float len);float area();float volume();
};
glo::glo(float len) :cir(len)
{}
float glo::area()
{float q = acos(-1);return 4 * m_len * m_len * q;
}
float glo::volume()
{float q = acos(-1);return 4 * q * m_len * m_len * m_len/3;
}class cyl :public cir//圆柱
{
public:cyl(float len,float height);float area();float volume();
protected:float m_height;
};
cyl::cyl(float len, float height) :cir(len), m_height(height)
{}
float cyl::area()
{float q = acos(-1);return m_len * m_len * q * 2 + 2 * q * m_len * m_height;
}
float cyl::volume()
{float q = acos(-1);return m_len * m_len * q * m_height;
}class tri :public line//三角形
{
public:tri(float len, float len2, float len3);float area();
protected:float m_len2;float m_len3;
};
tri::tri(float len, float len2, float len3) : line(len), m_len2(len2),m_len3(len3)
{}
float tri::area()
{float q = (m_len + m_len2 + m_len3) * (m_len + m_len2 - m_len3) * (m_len - m_len2 + m_len3) * (-m_len + m_len2 + m_len3);return sqrt(q)/4;
}class tria :public tri//直三棱柱
{
public:tria(float len, float len2, float len3,float height);float area();float volume();
protected:float m_height;
};
tria::tria(float len, float len2, float len3, float height) :tri(len, len2, len3), m_height(height)
{}
float tria::area()
{float q = (m_len + m_len2 + m_len3) * (m_len + m_len2 - m_len3) * (m_len - m_len2 + m_len3) * (-m_len + m_len2 + m_len3);return sqrt(q)/2+ (m_len + m_len2 + m_len3) * m_height;
}
float tria::volume()
{float q = (m_len + m_len2 + m_len3) * (m_len + m_len2 - m_len3) * (m_len - m_len2 + m_len3) * (-m_len + m_len2 + m_len3);return sqrt(q) * m_height/4;
}int main()
{line* p = new cuboid(10, 20 ,30);cout << "长方体表面积: " << p->area() << endl;cout << "长方体体积: " << p->volume() << endl;cout << endl;p = new cube(15);cout << "正方体表面积: " << p->area() << endl;cout << "正方体体积: " << p->volume() << endl;cout << endl;p = new glo(10);cout << "球表面积: " << p->area() << endl;cout << "球体积: " << p->volume() << endl;cout << endl;p = new cyl(10, 10);cout << "圆柱表面积: " << p->area() << endl;cout << "圆柱体积: " << p->volume() << endl;cout << endl;p = new tria(3,4,5,100);cout << "三棱柱表面积: " << p->area() << endl;cout << "三棱柱体积: " << p->volume() << endl;cout << endl;return 0;
}
运行结果:
长方体表面积: 2200
长方体体积: 6000正方体表面积: 1350
正方体体积: 3375球表面积: 1256.64
球体积: 4188.79圆柱表面积: 1256.64
圆柱体积: 3141.59三棱柱表面积: 1212
三棱柱体积: 600
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
