三角形类

本文详细阐述了如何使用面向对象编程设计一个三角形类,包括成员函数的定义和实现,解决三角形三边长度的有效性验证,并计算周长和面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
* 作    者: 丁露
* 完成日期:2014 年3月18日
* 版 本 号:v1.0
* 问题描述:面向对象。
* 样例输入:
* 样例输出:
* 问题分析:设计一个三角形类,完成各成员函数的定义。
*/

#include<iostream>
#include<Cmath>
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;
}
void Triangle::getABC(double *x, double *y, double *z)
{
    *x=a;
    *y=b;
    *z=c;
}
double Triangle::perimeter(void)
{
    double l;
    return(l=a+b+c);
}
double Triangle::area(void)
{
    double s,p;
    p=(a+b+c)/2;
    s=sqrt(p*(p-a)*(p-b)*(p-c));
    return s;

}

一开始怎么都检查不出来错误,但结果就是不对,原来是赋值时写反了,哎,一遇到指针就不会啊,又要继续看书!
### 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、付费专栏及课程。

余额充值