2017C++基础——网课笔记(27到31)

二十七. 判断两个立方体是否相等

#include <iostream>

using namespace std;

class Cube
{
public:
    void setABC(int a, int b, int c)
    {
        m_a = a;
        m_b = b;
        m_c = c;
    }

    int getArea()
    {
        return ((m_a * m_b)*2 + (m_a * m_c)*2 + (m_b * m_c)*2);
    }

    int getVolume()
    {
        return m_a * m_b * m_c;
    }

    int getA()
    {
        return m_a;
    }

    int getB()
    {
        return m_b;
    }

    int getC()
    {
        return m_c;
    }

    bool compareCube(Cube & another)
    {
        if(this->m_a == another.m_a &&
           this->m_b == another.m_b &&
           this->m_c == another.m_c
        )
            return true;
        else
            return false;
    }
private:
    int m_a;
    int m_b;
    int m_c;
};

//全局函数
bool judgeCube(Cube &c1, Cube &c2)
{
    if(c1.getA() == c2.getA() &&
       c1.getB() == c2.getB() &&
       c1.getC() == c2.getC()
    )
        return true;
    else
        return false;
}


int main()
{
    Cube c1;
    c1.setABC(10,20,30);

    Cube c2;
    c2.setABC(10,20,30);

    cout<<"C1 的体积是"<<c1.getVolume()<<endl;
    cout<<"C1 的面积是"<<c1.getArea()<<endl;

    if (judgeCube(c1,c2))
        cout<<"两个立方体相等"<<endl;
    else
        cout<<"两个立方体不相等"<<endl;

    cout<<"----------------------------------------"<<endl;

    if (c1.compareCube(c2))
        cout<<"两个立方体相等"<<endl;
    else
        cout<<"两个立方体不相等"<<endl;

    return 0;
}

二十八. 判断点是否在圆的内部

总共有五个文件 point.h , point.cpp, circle.h, circle.cpp, main.cpp

第一个文件是point.h

#ifndef POINT_H
#define POINT_H

//点类
//类名一般都首字母大写
class Point
{
public:
    void setXY(int x, int y);
    int getX();
    int getY();
private:
    int m_x;
    int m_y;
};

#endif // POINT_H

第二个文件是point. cpp

#include "point.h"


void Point::setXY(int x, int y)
{
    m_x = x;
    m_y = y;
}

int Point::getX()
{
    return m_x;
}

int Point::getY()
{
    return m_y;
}

第三个文件 circle.h

#ifndef CIRCLE_H
#define CIRCLE_H
#include "point.h"

class Circle
{
public:
    void setXY(int x, int y);
    void setR(int r);

    //提供一个判断点是否在圆内的方法
    //true在内部,false在外部
    bool judgePoint(Point & p);

private:
    int x0;
    int y0;
    int m_r;
};

#endif // CIRCLE_H

第四个文件circle.cpp

#include "circle.h"


void Circle::setXY(int x, int y)
{
    x0 = x;
    y0 = y;
}

void Circle::setR(int r)
{
    m_r =r;
}

//提供一个判断点是否在圆内的方法
//true在内部,false在外部
bool Circle::judgePoint(Point & p)
{
    int dd;

    dd = (p.getX()-x0)*(p.getX()-x0)+ (p.getY()- y0)*(p.getY()-y0);

    if (dd <= m_r * m_r)
        return true;
    else
        return false;
}

第五个文件 main.cpp

#include <iostream>
#include "point.h"
#include "circle.h"

using namespace std;




int main()
{
    Circle c;
    c.setXY(2,2);
    c.setR(4);

    Point p;
    p.setXY(8,8);

    if(c.judgePoint(p) == true)
        cout<<"点在圆内"<<endl;
    else
        cout<<"点在圆外"<<endl;


    return 0;
}

二十九. 作业和今日回顾(略)

三十. 昨日回顾(略)

三十一. 判断两个圆是否相交

#include <iostream>
#include <cmath>

using namespace std;

class Point
{
public:
    void setXY(int x, int y)
    {
        m_x = x;
        m_y = y;
    }

    //计算两点距离的方法
    double pointDistance(Point & another)
    {
        int d_x = m_x - another.m_x;
        int d_y = m_y - another.m_y;

        double dis =  sqrt(d_x*d_x + d_y*d_y);
        return dis;
    }

private:
    int m_x;
    int m_y;
};

class Circle
{
public:
    void setR(int r)
    {
        m_r = r;
    }

    void setXY(int x, int y)
    {
        p0.setXY(x,y);
    }

    //判断两圆是否相交
    bool isIntersection(Circle & another)
    {
        //两个半径之和
        int rr;
        rr = m_r + another.m_r;

        //两个圆心之间的距离
        double dis;
        dis = p0.pointDistance(another.p0);

        if(dis<=rr)
            return true;
        else
            return false;
    }



private:
    int m_r;
    Point p0;
};



int main()
{
    Circle c1,c2;

    int x,y,r;

    cout<<"请输入第1个圆的半径"<<endl;
    cin>>r;
    c1.setR(r);
    cout<<"请输入第1个圆的x"<<endl;
    cin>>x;
    cout<<"请输入第1个圆的y"<<endl;
    cin>>y;
    c1.setXY(x,y);

    cout<<"请输入第2个圆的半径"<<endl;
    cin>>r;
    c2.setR(r);
    cout<<"请输入第2个圆的x"<<endl;
    cin>>x;
    cout<<"请输入第1个圆的y"<<endl;
    cin>>y;
    c2.setXY(x,y);

    if(c1.isIntersection(c2) == true)
        cout<<"相交"<<endl;
    else
        cout<<"不相交"<<endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/80659981
今日推荐