PTA 6-1 类的继承和派生

本题实现类的额继承和派生。已经给出相关的数据结构和类的声明,要求实现类的相关函数,为保证比较的结果,所有的数据均以int类型(除了PI取3.14外),程序运行时,输入一个点p的坐标x和y,接着是圆的中心坐标和半径,接着是三角形的三个顶点,程序运行显示圆的面积和周长,点p是否位于园内,三角形的面积和周长,以及p是否位于三角形内。

函数接口定义:

在这里给出了数据结构和类的声明:
#include <iostream>
#include <math.h>
using namespace std;
const double PI = 3.14;
struct Point{//点的定义
    int x,y;
};
class Shape
{
public:
    int area(){return 0;}//计算图形面积
    int length(){return 0;}//计算图形的周长
    void show(){}//显示相关信息
    bool IsInside(Point p){return false;}//点p是否位于图形内,如果是,返回true
};
class Circle:public Shape{
    Point center;//圆心
    int radius;//半径
public:
    Circle(Point,int);
    int area();
    int length();
    void show();
    bool IsInside(Point p);
};

class TriAngle:public Shape{
    Point a,b,c;//三角形的三个顶点
public:
    TriAngle(Point,Point,Point);
    int area();
    int length();
    void show();
    bool IsInside(Point p);
};

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:
int main()
{
    Point p,center,a,b,c;
    int r;
    cin>>p.x>>p.y;
    cin>>center.x>>center.y;
    cin>>r;
    cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y;    
    Circle c1(center,r);
    c1.show();
    if(c1.IsInside(p))
        cout<<"p inside circle"<<endl;
    else
        cout<<"p not inside circle"<<endl;
    TriAngle tri(a,b,c);
    tri.show();
    if(tri.IsInside(p))
        cout<<"p inside triangle"<<endl;
    else
        cout<<"p not inside triangle"<<endl;       
    return 0;
}
/* 请在这里填写答案 */

代码实现如下:

Circle::Circle(Point p1 ,int  r1) //类内函数类外定义记得加类名前缀,此处占位参数,记得补上形参
{
    center=p1;
    radius=r1;
}
 int Circle::area()               //计算面积  
 {
      int area=PI*radius*radius;
      return area;
 }
 int Circle::length()            //计算周长
 {
       int length=2*PI*radius;
       return length;
 }
void Circle::show()
{
    cout<<"Circle:area="<<Circle::area()<<",length="<<Circle::length()<<endl;
}
bool  Circle::IsInside(Point p)      //判断是否在圆内
 {
       double dis1=sqrt(pow(p.x-center.x,2)+pow(p.y-center.y,2));
        if(dis1<=radius)
        {
            return true;
        }
        else return false;
 }
TriAngle::TriAngle(Point p1 ,Point  p2 ,Point p3 )
{
      a=p1;
      b=p2;
      c=p3;
}
    int TriAngle::area()   //当三个点A、B、C的坐标分别为A(x1,y1)、B(x2,y2)、C(x3、y3)时,
                           //三角形面积计算公式:area=√s(s−a)(s−b)(s−c),其中s=(a+b+c)/2。
{                          //pow函数求平方,sqrt函数求开方
           double  c1=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));//c边   
           double b1=sqrt(pow(a.x-c.x,2)+pow(a.y-c.y,2));//b边
           double a1=sqrt(pow(c.x-b.x,2)+pow(c.y-b.y,2)); //a边
           double ps=(a1+b1+c1)/2;     
           int areas=sqrt(ps*(ps-a1)*(ps-b1)*(ps-c1));   //海伦公式的运用
           return areas;
}
int TriAngle::length()
{
         int  d1=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
         int d2=sqrt(pow(a.x-c.x,2)+pow(a.y-c.y,2));
         int d3=sqrt(pow(c.x-b.x,2)+pow(c.y-b.y,2));
         return  d1+d2+d3;
}
void TriAngle::show()
{
   cout<<"TriAngle:area="<<TriAngle::area()<<",length="<<TriAngle::length()<<endl;
}
bool TriAngle::IsInside(Point p)    //判断点是否在三角形内,通过面积法去判断,
{
        double d1=sqrt(pow(a.x-p.x,2)+pow(a.y-p.y,2));  //ap边
        double d3=sqrt(pow(p.x-c.x,2)+pow(p.y-c.y,2));   //cp边
        double d2=sqrt(pow(p.x-b.x,2)+pow(p.y-b.y,2));  //bp边
        double  c1=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));  //c
        double b1=sqrt(pow(a.x-c.x,2)+pow(a.y-c.y,2));//b
        double a1=sqrt(pow(c.x-b.x,2)+pow(c.y-b.y,2)); //a
        double p1=(d1+d2+c1)/2;
        double p2=(d1+d3+b1)/2;
        double p3=(d3+d2+a1)/2;
        double ps=(a1+b1+c1)/2;
        int area1=sqrt(p1*(p1-d1)*(p1-d2)*(p1-c1));
        int area2=sqrt(p2*(p2-d1)*(p2-b1)*(p2-d3));
        int area3=sqrt(p3*(p3-a1)*(p3-d2)*(p3-d3));
        int areas=sqrt(ps*(ps-a1)*(ps-b1)*(ps-c1));
        if(areas>=(area1+area2+area3))//因为double int型转换间有数据丢失,所以此处是>=而非==
            {
                return true;
            }
           else return false;
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值