| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 951 人关注过本帖
标题:坐标有关程序请大侠指教
只看楼主 加入收藏
yi157
Rank: 1
等 级:新手上路
帖 子:151
专家分:0
注 册:2008-6-3
收藏
 问题点数:0 回复次数:8 
坐标有关程序请大侠指教
这有个程序看不懂,尤其是//???部分.望大侠赐教
//提示用户输入两个框架(frame)左上角和右下角的坐标,并判断两个框架是否重叠.如果重叠,
//则将面积较大的那个框架的4个坐标值各缩进1.每个坐标点(x,y)用一个结构类型来描述

//程序:

#include (iostream.h)
struct spoint
{
    int x,y;
};
struct sframe
{
    spoint topleft,bttomright;
};
void main()
{
    sframe frame1,frame2;
    bool overlapped;
    long area1,area2;

    cout<<"enter topleft coordinates for the first frame:";
    cin>>frame1.topleft.x>>frame1.topleft.y;
    cout<<"enter bottom right coordinates for the first frame:";
    cin>>frame1.bottomright.x>>frame1.bottomright.y;
    
    cout<<"enter topleft coordinates for the second frame:";
    cin>>frame2.topleft.x>>frame2.topleft.y;
    cout<<"enter botoomright coordinates for the second frame:";
    cin>>frame2.bottomright.x>>frame2.bottomright.y;

    //check if the frames are overlapped
    overlapped=true;//assume they are overlapped

    if(frame2.topleft.x>frame1.bottomright.x)////////////?????????
        overlapped=false;//////////////////////////???????以及下面
    if(frame2.bottomright.x<frame1.topleft.x)
        overlapped=false;
    if(frame2.bottomright.y>frame1.topleft.y)
        overlapped=false;
    if(frame2.topleft.y<frame1.bottomright.y)
        overlapped=false;
    if(voerlapped)
    {
        cout<<"the two frames are overlaped.">>endl;
        area1=(frame1.bottomright.x-frame1.topleft.x)*(frame1.topleft.y-
            frame1.bottomright.y);
        area2=(frame2.bottomright.x-frame2.topleft.x)*(frame2.topleft.y-
            frame2.bottomright.y);
        if(areal>area2)
        {
            frame1.topleft.x+=1;
            frame1.bottomright.x-=1;
            frame1.topleft.y-=1;
            frame.bottomright.y+=1;
        }
        else
        {
            frame2.topleft.x+=1;
            frame2.bottomright.x-=1;
            frame2.topleft.y-=1;
            frame2.bottomright.y+=1;
        }
    }
    else
        cout<<"the two frames are not overlapped."<<endl;
    cout<<"frame1:("<<frame1.topleft.x<<","<<frame1.topleft.y<<")",("<<
        frame1.bottomright.x<<","<<frame1.bottomright.y<<")"<<endl;
    cout<<"frame2:("<<frame2.topleft.x<<","<<frame2.topleft.y<<")",("<<
        frame2.bottomright.x<<","<<frame2.bottomright.y<<")"<<endl;
}


//程序运行结果:
//enter top left coordinates for the first frame:10 30
//enter bottom right coordinates for the first frame:20 20
//enter top left coordinates for the second frame:15 45
//enter botoomright coordinates for the second frame:35 25
//the two frames are overlaped.
//frame1:(10,30),(20),20)
//frame2:(16,44),(34,26)
//press any key to continue
搜索更多相关主题的帖子: 坐标 指教 
2008-10-23 10:53
keanbin
Rank: 1
等 级:新手上路
威 望:1
帖 子:26
专家分:0
注 册:2007-10-11
收藏
得分:0 
//??? C++中的//作用是把一行给屏蔽掉,只要是用来解释代码的作用,就是说//后面的代码或者是文字,C++不会去运行它,与程序无关。所以上面的程序可以改为:

#include (iostream.h)
struct spoint
{
    int x,y;
};
struct sframe
{
    spoint topleft,bttomright;
};
void main()
{
    sframe frame1,frame2;
    bool overlapped;
    long area1,area2;

    cout<<"enter topleft coordinates for the first frame:";
    cin>>frame1.topleft.x>>frame1.topleft.y;
    cout<<"enter bottom right coordinates for the first frame:";
    cin>>frame1.bottomright.x>>frame1.bottomright.y;
   
    cout<<"enter topleft coordinates for the second frame:";
    cin>>frame2.topleft.x>>frame2.topleft.y;
    cout<<"enter botoomright coordinates for the second frame:";
    cin>>frame2.bottomright.x>>frame2.bottomright.y;

     overlapped=true;

    if(frame2.topleft.x>frame1.bottomright.x)
        overlapped=false;
    if(frame2.bottomright.x<frame1.topleft.x)
        overlapped=false;
    if(frame2.bottomright.y>frame1.topleft.y)
        overlapped=false;
    if(frame2.topleft.y<frame1.bottomright.y)
        overlapped=false;
    if(voerlapped)
    {
        cout<<"the two frames are overlaped.">>endl;
        area1=(frame1.bottomright.x-frame1.topleft.x)*(frame1.topleft.y-
            frame1.bottomright.y);
        area2=(frame2.bottomright.x-frame2.topleft.x)*(frame2.topleft.y-
            frame2.bottomright.y);
        if(areal>area2)
        {
            frame1.topleft.x+=1;
            frame1.bottomright.x-=1;
            frame1.topleft.y-=1;
            frame.bottomright.y+=1;
        }
        else
        {
            frame2.topleft.x+=1;
            frame2.bottomright.x-=1;
            frame2.topleft.y-=1;
            frame2.bottomright.y+=1;
        }
    }
    else
        cout<<"the two frames are not overlapped."<<endl;
    cout<<"frame1:("<<frame1.topleft.x<<","<<frame1.topleft.y<<")",("<<
        frame1.bottomright.x<<","<<frame1.bottomright.y<<")"<<endl;
    cout<<"frame2:("<<frame2.topleft.x<<","<<frame2.topleft.y<<")",("<<
        frame2.bottomright.x<<","<<frame2.bottomright.y<<")"<<endl;
}

就是把//以及后面的东西删掉以后的程序的运行和你给的程序的效果是一样的!!
2008-10-23 11:01
keanbin
Rank: 1
等 级:新手上路
威 望:1
帖 子:26
专家分:0
注 册:2007-10-11
收藏
得分:0 
你如果还有哪些不懂的再问!!!
2008-10-23 11:02
yi157
Rank: 1
等 级:新手上路
帖 子:151
专家分:0
注 册:2008-6-3
收藏
得分:0 
[bo][un]keanbin[/un] 在 2008-10-23 11:02 的发言:[/bo]

你如果还有哪些不懂的再问!!!

您误会了,我的意思是//??所在行的可执行代码是什么含义,请指教
2008-10-23 11:10
keanbin
Rank: 1
等 级:新手上路
威 望:1
帖 子:26
专家分:0
注 册:2007-10-11
收藏
得分:0 
回复 4# yi157 的帖子
  我知道了  我帮你看下。
不过这个程序你有运行看看了吗?
上面的错误很多啊!!!!
2008-10-23 12:12
keanbin
Rank: 1
等 级:新手上路
威 望:1
帖 子:26
专家分:0
注 册:2007-10-11
收藏
得分:0 
帮你把程序的错误改好了:(都是些粗心大意的错误)
if(frame2.topleft.x>frame1.bottomright.x)
        overlapped=false;
    if(frame2.bottomright.x<frame1.topleft.x)
        overlapped=false;
    if(frame2.bottomright.y>frame1.topleft.y)
        overlapped=false;
    if(frame2.topleft.y<frame1.bottomright.y)
        overlapped=false;
你是想问上面的这段程序是什么意思是不是?
这段程序是用来判断两个框架是否重叠的。
第一个IF的意思是:
如果第二个框架左上角的X坐标大于第一个框架右下角的X坐标,就不重叠,为什么呢?你想想看,当第二个框架左上角的X坐标大于第一个框架右下角的X坐标时,整个第二个框架不就是在第一个框架的右边吗?
第二个IF的意思是:
如果第二个框架右下角的X坐标小于第一个框架左上角的X坐标,就不重叠。因为此时,整个第二个框架在第一个框架的左边。
第三个IF的意思是:
如果第二个框架右下角的y坐标大于于第一个框架左上角的y坐标,就不重叠。因为此时,整个第二个框架在第一个框架的上边。
第四个IF的意思不用说了, 你也应该知道了吧,不知道的话,自己想一下!!多想想能开阔你的思维,印象也深刻。

帮你改好的程序:
#include <iostream.h>
struct spoint
{
    int x,y;
};
struct sframe
{
    spoint topleft,bottomright;
};
void main()
{
    sframe frame1,frame2;
    bool overlapped;
    long area1,area2;

    cout<<"enter topleft coordinates for the first frame:";
    cin>>frame1.topleft.x>>frame1.topleft.y;
    cout<<"enter bottom right coordinates for the first frame:";
    cin>>frame1.bottomright.x>>frame1.bottomright.y;
   
    cout<<"enter topleft coordinates for the second frame:";
    cin>>frame2.topleft.x>>frame2.topleft.y;
    cout<<"enter botoomright coordinates for the second frame:";
    cin>>frame2.bottomright.x>>frame2.bottomright.y;

    //check if the frames are overlapped
    overlapped=true;//assume they are overlapped

    if(frame2.topleft.x>frame1.bottomright.x)////////////?????????
        overlapped=false;//////////////////////////???????以及下面
    if(frame2.bottomright.x<frame1.topleft.x)
        overlapped=false;
    if(frame2.bottomright.y>frame1.topleft.y)
        overlapped=false;
    if(frame2.topleft.y<frame1.bottomright.y)
        overlapped=false;
    if(overlapped)
    {
        cout<<"the two frames are overlaped."<<endl;
        area1=(frame1.bottomright.x-frame1.topleft.x)*(frame1.topleft.y-
            frame1.bottomright.y);
        area2=(frame2.bottomright.x-frame2.topleft.x)*(frame2.topleft.y-
            frame2.bottomright.y);
        if(area1>area2)
        {
            frame1.topleft.x+=1;
            frame1.bottomright.x-=1;
            frame1.topleft.y-=1;
            frame1.bottomright.y+=1;
        }
        else
        {
            frame2.topleft.x+=1;
            frame2.bottomright.x-=1;
            frame2.topleft.y-=1;
            frame2.bottomright.y+=1;
        }
    }
    else
        cout<<"the two frames are not overlapped."<<endl;
    cout<<"frame1:("<<frame1.topleft.x<<","<<frame1.topleft.y<<"),("<<
        frame1.bottomright.x<<","<<frame1.bottomright.y<<")"<<endl;
    cout<<"frame2:("<<frame2.topleft.x<<","<<frame2.topleft.y<<"),("<<
        frame2.bottomright.x<<","<<frame2.bottomright.y<<")"<<endl;
}
2008-10-23 13:57
lmyouya
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-10-17
收藏
得分:0 
, 完全不懂!

吖吖走了
2008-10-23 14:25
yi157
Rank: 1
等 级:新手上路
帖 子:151
专家分:0
注 册:2008-6-3
收藏
得分:0 
[bo][un]keanbin[/un] 在 2008-10-23 13:57 的发言:[/bo]

帮你把程序的错误改好了:(都是些粗心大意的错误)
if(frame2.topleft.x>frame1.bottomright.x)
        overlapped=false;
    if(frame2.bottomright.x<frame1.topleft.x)
        overlapped=false;
    if(frame2.bottomright.y>frame1.topleft.y)
        overlapped=false;
    if(frame2.topleft.y<frame1.bottomright.y)
        overlapped=false;
你是想问上面的这段程序是什么意思是不是?
这段程序是用来判断两个框架是否重叠的。
第一个IF的意思是:
如果第二个框架左上角的X坐标大于第一个框架右下角的X坐标,就不重叠,为什么呢?你想想看,当第二个框架左上角的X坐标大于第一个框架右下角的X坐标时,整个第二个框架不就是在第一个框架的右边吗?
第二个IF的意思是:
如果第二个框架右下角的X坐标小于第一个框架左上角的X坐标,就不重叠。因为此时,整个第二个框架在第一个框架的左边。
第三个IF的意思是:
如果第二个框架右下角的y坐标大于于第一个框架左上角的y坐标,就不重叠。因为此时,整个第二个框架在第一个框架的上边。
第四个IF的意思不用说了, 你也应该知道了吧,不知道的话,自己想一下!!多想想能开阔你的思维,印象也深刻。

帮你改好的程序:
#include <iostream.h>
struct spoint
{
    int x,y;
};
struct sframe
{
    spoint topleft,bottomright;
};
void main()
{
    sframe frame1,frame2;
    bool overlapped;
    long area1,area2;

    cout<<"enter topleft coordinates for the first frame:";
    cin>>frame1.topleft.x>>frame1.topleft.y;
    cout<<"enter bottom right coordinates for the first frame:";
    cin>>frame1.bottomright.x>>frame1.bottomright.y;
   
    cout<<"enter topleft coordinates for the second frame:";
    cin>>frame2.topleft.x>>frame2.topleft.y;
    cout<<"enter botoomright coordinates for the second frame:";
    cin>>frame2.bottomright.x>>frame2.bottomright.y;

    //check if the frames are overlapped
    overlapped=true;//assume they are overlapped

    if(frame2.topleft.x>frame1.bottomright.x)////////////?????????
        overlapped=false;//////////////////////////???????以及下面
    if(frame2.bottomright.x<frame1.topleft.x)
        overlapped=false;
    if(frame2.bottomright.y>frame1.topleft.y)
        overlapped=false;
    if(frame2.topleft.y<frame1.bottomright.y)
        overlapped=false;
    if(overlapped)
    {
        cout<<"the two frames are overlaped."<<endl;
        area1=(frame1.bottomright.x-frame1.topleft.x)*(frame1.topleft.y-
            frame1.bottomright.y);
        area2=(frame2.bottomright.x-frame2.topleft.x)*(frame2.topleft.y-
            frame2.bottomright.y);
        if(area1>area2)
        {
            frame1.topleft.x+=1;
            frame1.bottomright.x-=1;
            frame1.topleft.y-=1;
            frame1.bottomright.y+=1;
        }
        else
        {
            frame2.topleft.x+=1;
            frame2.bottomright.x-=1;
            frame2.topleft.y-=1;
            frame2.bottomright.y+=1;
        }
    }
    else
        cout<<"the two frames are not overlapped."<<endl;
    cout<<"frame1:("<<frame1.topleft.x<<","<<frame1.topleft.y<<"),("<<
        frame1.bottomright.x<<","<<frame1.bottomright.y<<")"<<endl;
    cout<<"frame2:("<<frame2.topleft.x<<","<<frame2.topleft.y<<"),("<<
        frame2.bottomright.x<<","<<frame2.bottomright.y<<")"<<endl;
}

是的,开始是没编译,非常感谢你的分析
我现在正想在思考中..
2008-10-23 22:11
yi157
Rank: 1
等 级:新手上路
帖 子:151
专家分:0
注 册:2008-6-3
收藏
得分:0 
[bo][un]keanbin[/un] 在 2008-10-23 12:12 的发言:[/bo]

  我知道了  我帮你看下。
不过这个程序你有运行看看了吗?
上面的错误很多啊!!!!

题目:提示用户输入两个框架(frame)左上角和右下角的坐标,并判断两个框架是否重叠.如果重叠...
还有这里我一直都搞不懂,不过你这么一分析,是不是判断框架交叉而不是重叠?
2008-10-24 17:03
快速回复:坐标有关程序请大侠指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021378 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved