三角形类

#include<iostream>
#include<Cmath>
#include<cstdlib>
using namespace std;
class Triangle
{
public:
    void setABC(double x, double y, double z);//置三边的值,注意要能成三角形
    void getABC(double *x, double *y, double *z);//取三边的值
    double perimeter(void);//计算三角形的周长
    double area(void);//计算并返回三角形的面积
private:
    double a,b,c; //三边为私有成员数据
};
int main()
{
    Triangle tri1;	//定义三角形类的一个实例(对象)
    tri1.setABC(4,5,6);	//为三边置初值
    double x,y,z;
    tri1.getABC (&x,&y,&z);   //将三边的值为x,y,z赋值
    cout<<"三条边为:"<<x<<'\t'<<y<<'\t'<<z<<endl;
    cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;
    return 0;
}
//请在下面定义Triangle类中的各个成员函数
void Triangle::setABC(double x, double y, double z)
{
    if(x+y>z&&x+z>y&&y+z>x)
    {
        a=x;
        b=y;
        c=z;
    }
    else
    {
        cout<<"不能构成三角形"<<endl;
        exit(0);
    }
}

void Triangle::getABC(double *x, double *y, double *z)
{
    *x=a;
    *y=b;
    *z=c;
}

double Triangle::perimeter(void)
{
    return a+b+c;
}

double Triangle::area(void)
{
    double p=(a+b+c)/2;
    return sqrt(p*(p-a)*(p-b)*(p-c));
}

运行结果:


### C++实现三角形分类 以下是基于C++编写的三角形分类算法,该程序通过定义`Point`类和`Triangle`类来完成对三角形的判定。具体功能包括判断三点是否构成三角形以及进一步区分其类型(如等腰、等边、直角或等腰直角三角形)。为了提高精度,避免浮点误差的影响,不建议直接使用`==`进行比较。 #### 定义 `Point` 类 `Point` 类用于表示平面上的一个点,包含两个成员变量:`x` 和 `y` 表示坐标位置。 ```cpp class Point { public: double x, y; Point(double _x = 0.0, double _y = 0.0) : x(_x), y(_y) {} // 计算两点之间的距离平方 double distanceSquared(const Point& other) const { return (this->x - other.x) * (this->x - other.x) + (this->y - other.y) * (this->y - other.y); } }; ``` #### 定义 `Triangle` 类 `Triangle` 类负责接收三个点作为参数并提供方法来检测这些点是否能够形成有效三角形,同时支持多种类型的分类逻辑。 ```cpp #include <iostream> #include <cmath> #include <algorithm> using namespace std; // 辅助函数:判断两数是否接近相等 bool nearlyEqual(double a, double b, double epsilon = 1e-9) { return abs(a - b) <= epsilon; } class Triangle { private: Point p1, p2, p3; // 顶点 double d1, d2, d3; // 边长平方 public: Triangle(Point _p1, Point _p2, Point _p3) : p1(_p1), p2(_p2), p3(_p3) { calculateDistances(); } void calculateDistances() { d1 = p1.distanceSquared(p2); // 边 p1-p2 的长度平方 d2 = p2.distanceSquared(p3); // 边 p2-p3 的长度平方 d3 = p3.distanceSquared(p1); // 边 p3-p1 的长度平方 } bool isValid() const { // 判断是否为合法三角形 return !(nearlyEqual(d1 + d2, d3) || nearlyEqual(d1 + d3, d2) || nearlyEqual(d2 + d3, d1)); } string getType() const { // 获取三角形的具体类型 if (!isValid()) return "Not a triangle"; int equalSidesCount = 0; if (nearlyEqual(d1, d2)) ++equalSidesCount; if (nearlyEqual(d1, d3)) ++equalSidesCount; if (nearlyEqual(d2, d3)) ++equalSidesCount; if (equalSidesCount >= 2) { // 至少有两条边相等的情况 if (equalSidesCount == 3) return "Equilateral"; // 所有三边均相等 -> 等边三角形 else { // 检查是否存在直角条件 vector<double> sides{d1, d2, d3}; sort(sides.begin(), sides.end()); if (nearlyEqual(sides[0] + sides[1], sides[2])) return "Isosceles Right"; return "Isosceles"; } } else { // 非等腰情况下的直角检查 vector<double> sides{d1, d2, d3}; sort(sides.begin(), sides.end()); if (nearlyEqual(sides[0] + sides[1], sides[2])) return "Right"; return "Scalene"; } } }; int main() { // 测试数据 Point points1[] = {Point(0, 0), Point(0, 2), Point(sqrt(5), 1)}; // Isosceles Point points2[] = {Point(0, 0), Point(0, 2), Point(sqrt(3), 1)}; // Equilateral Point points3[] = {Point(1, 0), Point(0, 0), Point(0, 2)}; // Right Point points4[] = {Point(0, 0), Point(0, 2), Point(sqrt(2), 1)}; // Isosceles Right cout << "Test Case 1: " << Triangle(points1[0], points1[1], points1[2]).getType() << endl; cout << "Test Case 2: " << Triangle(points2[0], points2[1], points2[2]).getType() << endl; cout << "Test Case 3: " << Triangle(points3[0], points3[1], points3[2]).getType() << endl; cout << "Test Case 4: " << Triangle(points4[0], points4[1], points4[2]).getType() << endl; return 0; } ``` 此代码实现了完整的三角形分类功能,并考虑到了数值计算中的浮点误差问题[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值