在三角形类tri实现三个函数,settri功能是输入三个顶点坐标,test功能是判断是否能构成三角形,judge功能是判断组成的是等腰、等边、直角还是一般三角形(若能构成三角形)。适当添加构造函数

在三角形类tri实现三个函数,settri功能是输入三个顶点坐标,test功能是判断是否构成三角形,judge功能是判断组成的是等腰、等边、直角还是一般三角形(若能构成三角形)。适当添加构造函数、析构函数和其它函数。实现并测试这个类可以用友元函数或友元类来实现。要求使用多文件结构

//point.h
#include <iostream>
class point {
    friend class tri;
private:
    float x, y;
public:
    point(float a, float b);
    point();
    void set(float a, float b);
    float getx();
    float gety();
};
//TRI.h
#include <iostream>
#include"point.h"
class tri
{
private:
    point A, B, C; //A,B,C为三角形三个顶点
    double L1, L2, L3;
public:
    void settri();
    //用于输入三个顶点坐标
    int test();  
    //用于判断是否构成三角形
    int judge(); 
    //用于判断三角形的种类(等腰、等边、直角、一般三角形)(若能构成三角形)
};
//point.cpp
#include"point.h"
#include<iostream>
using namespace std;
point::point(float a, float b){	x = a;	y = b;}

point::point() { x = 0;y = 0;}

void point::set(float a, float b){	 x = a; y = b;}

float point::getx() { return x; }

float point::gety() { return y; }
//tri.cpp
#include"TRI.h"
#include<iostream>
#include<math.h>
using namespace std;
void tri::settri()
{
	cin >> A.x >> A.y;
	cin >> B.x >> B.y;
	cin >> C.x >> C.y;

}
//用于判断是否构成三角形
int tri::test()
{
	L1 = sqrt(pow(A.getx() - B.getx(), 2) + pow(A.gety() - B.gety(), 2));
	L2 = sqrt(pow(A.getx() - C.getx(), 2) + pow(A.gety() - C.gety(), 2));
	L3 = sqrt(pow(C.getx() - B.getx(), 2) + pow(C.gety() - B.gety(), 2));
	if (L1 + L2 > L3 || L1 + L3 > L2 || L2 + L3 > L1)
	{
		cout << "构成三角形" << endl;
		return 1;
	}
	else
	{
		cout << "不构成三角形" << endl;
		return 0;
	}
}
int tri::judge()
{
	if (L1 == L2 || L1 == L3 || L2 == L3)
	{
		cout << "等腰三角形" << endl;
		return 1;
	}
	else if (L1 == L2 == L3)
	{
		cout << "等边三角形" << endl; return 2;
	}
	else if (pow(L1, 2) + pow(L2, 2) == pow(L3, 2) || pow(L3, 2) + pow(L2, 2) == pow(L1, 2) || pow(L1, 2) + pow(L3, 2) == pow(L2, 2))
	{
		cout << "直角三角形" << endl; return 3;
	}
	else
	{
		cout << "一般三角形" << endl; return 0;
	}

}
//main函数
#include<iostream>
#include<iostream>
#include<math.h>
using namespace std;
#include"TRI.h"
int main()
{
	cout << "请按照x1 y1 x2 y2 x3 y3的方式输入坐标:" << endl;
	tri T1;
	T1.settri();
	if (T1.test() ==1)
		T1.judge();
	else return 0;

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值