结构体和多文件编程
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 就可以了
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 就可以了
相关文章推荐
- 在php中集成xdebug(windows平台)
- netbeans跟eclipse的细节对比
- 003_002 Python 获取上一个周几,如周五,星期五
- 嵌入式C/C++开发中的代码优化(翻译版)
- Spring多数据源的配置和使用
- OLTP与OLAP的区别
- C++中rand()函数的用法
- HashMap之Java实现
- 76-C语言中的getchar和putchar详解
- 黑马程序员-OC语言基础学习(四)
- how to use regular expressions in yii
- 003_001 Python 计算昨天和明天的日期
- cstring、string、wstring、int、char*、tchar、 int、dword等相互转换代码输出测试
- java HashMap 循环遍历map的方法(转)
- java 工厂模式的一种理解
- Python 字符串操作(截取/替换/查找/分割)
- Windows下配置PHP
- 两个奇葩的C/C++问题
- java中的异常处理(二)
- jvm垃圾回收