| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 859 人关注过本帖
标题:结构体定义的问题
取消只看楼主 加入收藏
suv121
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2014-4-25
结帖率:0
收藏
已结贴  问题点数:10 回复次数:3 
结构体定义的问题
#include<math.h>
#include<stdio.h>
//定义点结构体 //
struct point
{
    float x;
    float y;
};
/*定义矩形结构体*/
struct rect
{
    struct point pt1;
    struct point pt2;
    struct point pt3;
    struct point pt4;
};
/*声明函数*/
float GetDis(struct point,struct point);
struct point makepoint(float x,float y);
//定义构造点函数//
struct point makepoint(float x,float y)
{
    struct point temp;
    temp.x=x;
    temp.y=y;
    return temp;
}
//定义构造矩形函数//
struct rect makerect(struct point pt1,struct point pt2,struct point pt3,struct point pt4,struct rect *screen)//struct rect *screen ?????//
{
    (*screen).pt1=pt1;
    (*screen).pt2=pt2;
    (*screen).pt3=pt3;
    (*screen).pt4=pt4;
}  
//定义判断函数//
int PtRect(struct point p,struct rect r)
{
    return p.x>=r.pt1.x && p.x<=r.pt3.x && p.y>=r.pt1.y && p.y<=r.pt3.y;

}

main()
{
    struct point pt1=makepoint(0,0);
    struct point pt2=makepoint(50,0);
    struct point pt3=makepoint(50,10);
    struct point pt4=makepoint(0,10);
   
    struct rect screen;
    makerect(pt1,pt2,pt3,pt4,&screen);//???????//
    struct point middle=makepoint((screen.pt1.x+screen.pt2.x)/2,(screen.pt2.y+screen.pt4.y)/2);
    printf("\n the middle point is(%f,%f)\n",middle.x,middle.y);
    printf("%f\n",screen.pt1.x);
    printf("%f\n",screen.pt1.y);
    printf("%f\n",screen.pt2.x);
    printf("%f\n",screen.pt2.y);
    printf("%f\n",screen.pt3.x);
    printf("%f\n",screen.pt3.y);
    printf("%f\n",screen.pt4.x);
    printf("%f\n",screen.pt4.y);
   
    if (PtRect(middle,screen))
    printf("the middle point is in screen!");
    else
    printf("the middle point is not in screen");
   
    getchar();
    getchar();
}
结构体在定义时和引用是格式为什么不一致,前面定义时是:
struct rect makerect(struct point pt1,struct point pt2,struct point pt3,struct point pt4,struct rect *screen)
后面引用时是:
makerect(pt1,pt2,pt3,pt4,&screen)
请问这是为什么?
搜索更多相关主题的帖子: include return 结构体 
2014-06-19 16:46
suv121
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2014-4-25
收藏
得分:0 
回复 4 楼 TonyDeng
struct rect makerect(struct point pt1,struct point pt2,struct point pt3,struct point pt4,struct rect screen)//定义//
{
    screen.pt1=pt1;
    screen.pt2=pt2;
    screen.pt3=pt3;
    screen.pt4=pt4;
}  
//引用
struct rect juxing;
makerect(pt1,pt2,pt3,pt4,juxing);
这样定义和引用的话,结果会出错。跪求解答!!!

[ 本帖最后由 suv121 于 2014-6-20 10:55 编辑 ]
2014-06-20 10:45
suv121
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2014-4-25
收藏
得分:0 
回复 6 楼 TonyDeng
图片附件: 游客没有浏览图片的权限,请 登录注册
程序代码:
#include<math.h>
#include<stdio.h>
//定义点结构体 // 
struct point
{
    float x;
    float y;
};
/*定义矩形结构体*/
struct rect
{
    struct point pt1;
    struct point pt2;
    struct point pt3;
    struct point pt4; 
};
/*声明函数*/
float GetDis(struct point,struct point);
struct point makepoint(float x,float y);
//定义构造点函数//
struct point makepoint(float x,float y)
{
    struct point temp;
    temp.x=x;
    temp.y=y;
    return temp;
}
//定义构造矩形函数// 
struct rect makerect(struct point pt1,struct point pt2,struct point pt3,struct point pt4,struct rect screen)//struct rect *screen ?????//
{
    screen.pt1=pt1;
    screen.pt2=pt2;
    screen.pt3=pt3;
    screen.pt4=pt4;
}  
//定义判断函数//
int PtRect(struct point p,struct rect r)
{
    return p.x>=r.pt1.x && p.x<=r.pt3.x && p.y>=r.pt1.y && p.y<=r.pt3.y;

}

main()
{
    struct point pt1=makepoint(0,0);
    struct point pt2=makepoint(50,0);
    struct point pt3=makepoint(50,10);
    struct point pt4=makepoint(0,10); 
    
    struct rect juxing;
    makerect(pt1,pt2,pt3,pt4,juxing);//???????//
    struct point middle=makepoint((juxing.pt1.x+juxing.pt2.x)/2,(juxing.pt2.y+juxing.pt4.y)/2);
    printf("\n the middle point is(%f,%f)\n",middle.x,middle.y);
    printf("%f\n",pt1.x);
    printf("%f\n",pt1.y);
    printf("%f\n",pt2.x);
    printf("%f\n",pt2.y);
    printf("%f\n",pt3.x);
    printf("%f\n",pt3.y);
    printf("%f\n",pt4.x);
    printf("%f\n",pt4.y);
    
    if (PtRect(middle,juxing))
    printf("the middle point is in screen!");
    else 
    printf("the middle point is not in screen");
    
    getchar();
    getchar();
} 
2014-06-20 11:33
suv121
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2014-4-25
收藏
得分:0 
回复 6 楼 TonyDeng
这样定义的话,矩形的四个点都是0,而且你看中点输出结果大的惊人
2014-06-20 11:42
快速回复:结构体定义的问题
数据加载中...
 
   



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

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