您的位置:首页 > 编程语言

结构体和多文件编程

2014-03-20 23:07 274 查看
用结构体表示线段和点,然后调用函数来判断就是那条线段长度最长。来返回值。

main.c

#include <stdio.h>
#include "cal.h"
int main()
{
line one,two;
int num = 0;
printf("Please input the positon of two points to represent first line.\n");
scanf("%d %d %d %d",&one.p1.x,&one.p1.y,&one.p2.x,&one.p2.y);
printf("Please input the positon of two points to represent first line.\n");
scanf("%d %d %d %d",&two.p1.x,&two.p1.y,&two.p2.x,&two.p2.y);
num = cal(&one,&two);
if(num == 1){
printf("The first line is longer.\n");
}else if(num == 2){
printf("The second line ls longer.\n");
}else {
printf("The two lines have equal length.\n");
}
return 0;
}
cal.h

#ifndef __CAL_H__
#define __CAL_H__
typedef struct dot{ //structure should be defined in head,then the structure in cal.c can be used in cal.c
int x,y;
}dot; // create the dot structure
typedef struct line{
dot p1,p2;
}line;
int cal(line *,line *);
#endif


cal.c

#include"cal.h"

int cal(line *t1,line *t2){
int tmp1 = (t1->p1.x - t1->p2.x) * (t1->p1.x - t1->p2.x) + (t1->p1.y - t1->p2.y) * (t1->p1.y - t1->p2.y);
int tmp2 = (t2->p1.x - t2->p2.x) * (t2->p1.x - t2->p2.x) + (t2->p1.y - t2->p2.y) * (t2->p1.y - t2->p2.y);
if(tmp1 > tmp2){
return 1;
}else if(tmp1 == tmp2){
return 0;
}else {
return 2;
}
}

gcc cal.c main.c 就可以了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: