在三角形类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;
}