【问题描述】
使用类的组合方式,实现点、线段、三角形类,请补充线段类与三角形类的代码。
【输入形式】
3个点的X坐标、Y坐标
【输出形式】
前两个点构成的线段长度、若三个点可以构成三角形,则输出三角形的周长和面积,否则输出“It is not a triangle!”
【样例输入1】
-1 -1 3 4 2 -5
【样例输出1】
6.40312
20.4585
15.5
【样例输入2】
1 2 3 4 5 6
【样例输出2】
2.82843
It is not a triangle!
#include<math.h>
#include <iostream>
using namespace std;
//点类
class Point
{
public:
Point();
Point(double x, double y);
double getX();
double getY();
private:
double mX,mY;
};
Point::Point()
{
}
double Point::getX()
{
return mX;
}
double Point::getY()
{
return mY;
}
Point::Point(double x,double y):mX(x),mY(y)
{
}
//线段类
class Line
{
public:
Line();
Line(Point a, Point b);
double GetDistance(); //返回线段的长度
private:
Point pts,pte;
};
Line::Line(Point a,Point b){
pts = a;
pte = b;
}
double Line::GetDistance(){
double a;
a = sqrt(pow(pts.getX()-pte.getX(),2) + pow(pts.getY()-pte.getY(),2));
return a;
}
//三角形类
class Triangle
{
public:
Triangle(Point a,Point b,Point c);
Triangle();
double getGirth(); //三角形周长
double getArea(); //三角形面积
int IsATriangle(); //判断三个点是否能构成三角形
private:
Point pa,pb,pc;
Line la,lb,lc;
};
Triangle::Triangle(Point a,Point b,Point c):la(a,b),lb(a,c),lc(b,c){
}
double Triangle::getGirth(){
return la.GetDistance()+lb.GetDistance()+lc.GetDistance();
}
double Triangle::getArea(){
double halfGirth;
halfGirth = (la.GetDistance()+lb.GetDistance()+lc.GetDistance())/2;
return sqrt(halfGirth*(halfGirth-la.GetDistance())
*(halfGirth-lb.GetDistance())*(halfGirth-lc.GetDistance()));;
}
int Triangle::IsATriangle(){
double a,b,c;
a = la.GetDistance();
b = lb.GetDistance();
c = lc.GetDistance();
if ((a+b > c)&(a+c > b) &(b+c > a))
return 1;
else
return 0;
}
int main()
{
double x1, x2, x3, y1, y2, y3;
cin >> x1>> y1>> x2>> y2>> x3>> y3;
Point p1(x1, y1), p2(x2, y2), p3(x3, y3);
Line line1(p1,p2);
cout<<line1.GetDistance()<<endl;//线段长度
Triangle t(p1, p2, p3);
if(t.IsATriangle()==1)
{
cout << t.getGirth() << endl;//三角形的周长
cout << t.getArea() << endl;//三角形的面积
}
else
{
cout<<"It is not a triangle!"<<endl;
}
return 0;
}
共有测试数据:3
平均占用内存:1.399K 平均运行时间:0.00351S
测试数据 | 评判结果 |
测试数据1 | 完全正确 |
测试数据2 | 完全正确 |
测试数据3 | 完全正确 |
附赠一个小题目
对时钟类进行运算符重载 【问题描述】对时钟类进行重载 |
#include <iostream>
using namespace std;
class Clock //时钟类声明
{
public: //外部接口
Clock(int NewH=0, int NewM=0, int NewS=0);
void ShowTime();
Clock operator ++(); //前置单目运算符重载
//后置单目运算符重载
Clock operator ++(int);
//+运算符重载
Clock operator +(Clock c);
private: //私有数据成员
int Hour, Minute, Second;
};
Clock::Clock(int h,int m,int s){
Hour = h; Minute = m ; Second = s;
}
Clock Clock::operator++(){
Second++;
return *this;
}
Clock Clock::operator++(int){
Second++;
if(Second>=60){
Second = 0;
Minute++;
if(Minute>=60){
Minute = 0;
Hour++;
if(Hour>=24){
Hour = 0;
}
}
}
return *this;
}
Clock Clock::operator+(Clock c){
Clock temp;
temp.Hour = Hour+c.Hour;
temp.Minute = Minute+c.Minute;
temp.Second = Second+c.Second;
return temp;
}
void Clock::ShowTime()
{
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
int main()
{
Clock c1(23,59,59),c2(5,12,10),c3;
c1.ShowTime();
c1++;
c1.ShowTime();
c3=++c2;
c3.ShowTime();
c3=c2+c1;
c3.ShowTime();
return 0;
}