반응형
헤더 파일 및 C 파일 만들기
1. main.c
#include<stdio.h>
#include"point.h"
#include"line.h"
#include"rect.h"
double distance(Line *pLine);
double area(Rect *pRect);
int main(){
Line l1 = {{10, 40}, {30, 20}};
Rect r1 = {{10, 40}, {30, 20}};
printf("point1 (%d, %d), point2 (%d, %d)\n", l1.start.x, l1.start.y, l1.end.x, l1.end.y);
printf("직선의 길이 : %.2lf\n\n", distance(&l1));
printf("point1 (%d, %d), point2 (%d, %d)\n", r1.lt.x, r1.lt.y, r1.rb.x, r1.rb.y);
printf("사각형의 면적: %.2lf\n", area(&r1));
return 0;
}
2. point.h
typedef struct point{
int x, y;
}Point;
3. rect.h
#include"point.h" // point.h의 구조체 변수 Point를 사용하기 때문에 'point.h' 라이브러리 파일을 포함시켜준다.
typedef struct rect{
Point lt, rb; // 사각형을 만들기 위해 lt(왼쪽 상단점), rb(오른쪽 하단점) 변수를 생성한다.
} Rect;
4. line.h
#include"point.h"
typedef struct line{
Point start, end; // 직선을 만들기 위해 두 개의 점을 타나내는 start와 end 변수를 생성한다.
}Line;
5. distance.c
#include<math.h> // sqrt()
#include"line.h"
double distance(Line *pLine){
int dx = pLine->end.x - pLine->start.x;
int dy = pLine->end.y - pLine->start.y;
return sqrt((dx * dx) + (dy * dy));
}
6. area.c
#include"rect.h"
double area(Rect *pRect){
int dx = pRect->rb.x - pRect->lt.x;
int dy = pRect->lt.y - pRect->rb.y;
return dx * dy;
}
사용자가 만든 헤더 파일 및 C 파일을 사용하기 위해서는 "main.c"와 같은 경로에 위치하여야 하고, 사용자 정의 헤더 파일을 포함할 때는 #include"사용자정의헤더파일명"으로 입력해 준다.
반응형
'C' 카테고리의 다른 글
[C언어] 열거형 (Enumeration) (0) | 2021.06.11 |
---|---|
[C언어] 공용체 (Union) (0) | 2021.06.11 |
[C언어] 실습: 배열에 저장된 정수의 합 출력하기 (0) | 2021.06.08 |
[C언어] time(NULL) (0) | 2021.06.07 |
[C언어] 난수 생성 함수 (0) | 2021.06.04 |