1 #include <stdio.h>
  2 //此处声明一个结构体用于保存点坐标值
  3 struct point{
  4         int x,y;
  5 }startp,endp;
  6 //结构体嵌套,用typedef声明一个这种类型的结构体
  7 typedef struct line{
  8     struct point startp,endp;
  9 }lines;

//此函数用于输入4个点,构成一条线段

 11 lines *get_line(lines *p)//指针函数,参数类型是指针
 12 {
 13     int a,b,c,d;
 14     printf("please input 4 point\n");
 15     scanf("%d %d %d %d",&a,&b,&c,&d);
 16     lines line = {{a,b},{c,d}};
 17     *p = line;
 18     printf("a:b:c:d:%d %d %d %d\n",a,b,c,d);
 19     return p;
 20 }
 21
 22 //传2条线进来,返回一条新的线段
 23 lines get_newline(lines *p1,lines *p2)
 24 {
 25     lines newl={{p1->startp.x,p1->startp.y},{p2->endp.x,p2->endp.y}};
 26     printf("p1x:%d,p1y:%d,p2x:%d,p2y:%d\n",p1->startp.x,p1->startp.y,p2->endp.x,p2->endp.    y);
 27     return newl;
 28 }
 29 //函数入口
 30 int main (void)
 31 {
 32     lines pp1,pp2;//声明结构体变量
 33     lines *p0 = &pp1;//声明一个结构体指针,用于指向结构体变量的地址
 34     lines *p1 = &pp2;
 35     lines *p2 = get_line(p0);
 36     lines *p3 = get_line(p1);
 37     lines newline = get_newline(p2,p3);
 38     return 0;
 39 }

编译之后:please input 4 point
1 2 4 5
a:b:c:d:1 2 4 5
please input 4 point
2 3 5 6
a:b:c:d:2 3 5 6
p1x:1,p1y:2,p2x:5,p2y:6

 

posted on 2013-12-27 17:11  fooke  阅读(850)  评论(0编辑  收藏  举报