结构类型struct(c++)

struct name
{
  //代码段
};

struct是数据类型的关键字   字段访问修饰符主要取值public和private(默认) public表示可以通过该类型的变量访问该字段, private表示不能不能通过该类型的变量访问该字段。

题目C:判断矩形是否重叠(结构)

题目描述:

用具有x,y两个整型变量成员的结构类型SPoint来表示坐标点。用SRect结构类型来描述矩形,其中包含p1和p2两个SPoint成员分别表示矩形对角线上的两个点。

编写判断两个矩形是否重叠的函数。

输入:

判断次数

矩形1的对角线顶点坐标x1、y1、x2、y2

矩形2的对角线顶点坐标x1、y1、x2、y2

......

输出:

是否重叠

两个矩形的边均与x轴或y轴平行,即轴对齐的矩形。

输入样例:

3
1 5 2 9
1 3 2 4
5 6 7 8
5 7 7 7
2 5 1 0
9 4 2 9

输出样例:

not overlapped
overlapped
overlapped
 

#include <iostream>
using namespace std;

struct spoint
{
    int x,y;
    spoint(int x1, int y1): x(x1), y(y1){}
};

struct srect
{
    spoint p1;
    spoint p2;
    srect(spoint q1,spoint q2):p1(q1),p2(q2){}
};

bool isOverlap(const srect &a, const srect &b)
{
    if(max(a.p1.x, a.p2.x) < min(b.p1.x,b.p2.x) || max(a.p1.y,a.p2.y) < min(b.p1.y,b.p2.y)
        || min(a.p1.x, a.p2.x) > max(b.p1.x,b.p2.x) || min(a.p1.y, a.p2.y) > max(b.p1.y, b.p2.y))
    {
        return true;
    }
    else
    {
        return false;
    }
}


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x1,y1,x2,y2;
        int x3,y3,x4,y4;
        cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
        spoint w(x1,y1);
        spoint e(x2,y2);
        spoint g(x3,y3);
        spoint h(x4,y4);
        srect p(w,e);
        srect q(g,h);
        if(isOverlap(p,q)==1)
        {
            cout<<"not overlapped"<<endl;
        }
        else
        {
            cout<<"overlapped\n";
        }
    }
}

题目D:判断点线位置(结构)

题目描述:用具有x,y两个整型变量成员的结构类型SPoint来表示坐标点。用SLine结构类型来描述线段,其中包含p1和p2两个SPoint成员。

编写函数direction(const SLine &ab, const SPoint &c),利用向量ab与ac叉乘的值判断点c与直线ab的位置关系。

输入:判断次数

线的两点坐标x1、y1、x2、y2

点坐标x、y

......

输出:

位置关系

输入样例:

3
1 5 2 9
1 3
5 6 7 8
6 7
2 3 1 0
3 3

输出样例:

clockwise
intersect
anti clockwise
 

向量a(x1,y1)与向量b(x2,y2)的叉乘定义为a.x*b.y-a.y*b.x,若结果小于0,表示向量b在向量a的顺时针方向;若结果大于0,表示向量b在向量a的逆时针方向;若等于0,表示向量a与向量b平行。

#include <iostream>
using namespace std;

struct spoint
{
    int x,y;
    spoint(int x1, int y1): x(x1), y(y1){}
};

struct sline
{
    spoint p1;
    spoint p2;
    sline(spoint q1,spoint q2):p1(q1),p2(q2){}
};

int direction(const sline &ab,const spoint &c)
{
    spoint a(ab.p2.x-ab.p1.x,ab.p2.y-ab.p1.y);
    spoint b(c.x-ab.p1.x,c.y-ab.p1.y);
    if((a.x*b.y-a.y*b.x)<0)
    {
        return -1;
    }
    else if((a.x*b.y-a.y*b.x)>0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x1,y1,x2,y2;
        int x3,y3;
        cin>>x1>>y1>>x2>>y2>>x3>>y3;
        spoint w(x1,y1);
        spoint e(x2,y2);
        spoint g(x3,y3);
        sline d(w,e);
        if(direction(d, g)==-1)
        {
            cout<<"clockwise"<<endl;
        }
        else if(direction(d, g)==1)
        {
            cout<<"anti clockwise\n";
        }
        else
        {
            cout<<"intersect"<<endl;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值