c++第三次实验作业

这篇博客介绍了如何使用C++实现类的继承和派生,具体应用在计算几何中,包括圆的面积、周长判断点是否在圆内,以及三角形的面积、周长和判断点是否在三角形内的功能。通过示例代码展示了Circle和Triangle类的定义及成员函数的实现,包括构造函数、计算面积和周长的方法以及判断点是否在图形内的函数。
摘要由CSDN通过智能技术生成

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;
};
int distance(Point p1,Point p2);//计算两个点之间的距离
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 p, int r) { center = p, radius = r; }
int Circle::area() {
    int area = radius * radius * PI;
    return area;
}
int Circle::length() {
    int length = PI * 2 * radius;
    return length;
}
void Circle::show() {
    cout << "Circle:area=" << area() << ",length=" << length()<<'\n';
}
int distance(Point p1, Point p2){
    int dis = sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    return dis;
}
bool Circle::IsInside(Point p) {
    if (distance(p, center) <= radius) {
        return true;
    }
    else {
        return false;
    }
}
TriAngle::TriAngle(Point t1,Point t2,Point t3):a(t1),b(t2),c(t3){}
int TriAngle::area() {
    int area1 = 0.5*(a.x * (b.y - c.y) +b.x*(c.y-a.y)+c.x*(a.y-b.y) );
    if (area1 >= 0) {
        return area1;
    }
    else {
         area1 = (-1) * area1;
        return area1;
    }
}
int TriAngle::length() {
    int length = distance(a, b) + distance(b, c) + distance(c, a);
    return length;
}
void TriAngle::show() {
    cout << "TriAngle:area=" << TriAngle::area() << ",length=" << TriAngle::length() << endl;
}
bool TriAngle::IsInside(Point p) {
    double s1 = 0.5* (p.x * (b.y - c.y) + b.x * (c.y - p.y) + c.x * (p.y - b.y));
    if (s1 < 0) {
        s1 = s1 * (-1);
    }
    else {
        s1 = s1;
    }
    double  s2 =0.5 * (a.x * (p.y - c.y) + p.x * (c.y - a.y) + c.x * (a.y - p.y));
    if (s2< 0) {
        s2 = s2 * (-1);
    }
    else {
        s2 = s2;
    }
    double s3 = 0.5* (a.x * (b.y - p.y) + b.x * (p.y - a.y) + p.x * (a.y - b.y));
    if (s3 < 0) {
        s3 = s3 * (-1);
    }
    else {
        s3 = s3;
    }
    int sum = s1 + s2 + s3;
    if (TriAngle::area()==sum) {
        return true;
    }
    else {
        return false;
    }
}

输入样例:
10 10 10 10 5 10 1 1 10 15 15
输出结果:
在这里插入图片描述

不足之处,望指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值