globals h ifndef globalsh define globalsh include stdio
예제: 헤더 파일 작성 globals. h #ifndef _globals_h #define _globals_h #include <stdio. h> #include <stdlib. h> // include 파일들 #ifndef FALSE #define FALSE 0 #endif // 상수 정의 typedef struct tree. Node { struct tree. Node *left; struct tree. Node *right; int data; } Tree. Node; // 타입 정의 extern FILE* infile; extern int lineno; // 전역 변수 선언 #endif main. c #include “globals. h” int lineno = 0; //전역 변수 정의 FILE* infile; int main() { …. } 10
#ifdef
예제: #include <stdio. h> #define DEBUG #define PI 3. 1415 #define CIRCLE_AREA(r) (PI*r*r) int main(void) { double radius, area; #ifdef DEBUG printf("“디버깅 모드입니다. n"); #endif printf("원의 반지름: "); scanf("%lf", &radius); area = CIRCLE_AREA(radius); printf("원의 면적=%. 2 f", area); return 0; }
예제 #define NATION 1 #if NATION == 1 printf("안녕하세요? "); #elif NATION == 2 printf("你好�? "); #else printf("Hello World!"); #endif // else + if
예제 student. h #ifndef STUDENT_H #define STUDENT_H struct STUDENT { int number; char name[10]; }; #endif #include "student. h" // STUDENT_H 가 이미 정의되어 있으므로 무시된다 int main(void) { return 0; }
예제 power. h main. c // 다중 소스 파일 #include <stdio. h> #include "power. h" int main(void) { int x, y; printf("x의 값을 입력하시오: "); scanf("%d", &x); printf("y의 값을 입력하시오: "); scanf("%d", &y); printf("%d의 %d 제곱값은 %fn", x, y, power(x, y)); // power. c에 대한 헤더 파일 #ifndef POWER_H #define POWER_H double power(int x, int y); #endif power. c // 다중 소스 파일 #include "power. h“ double power(int x, int y) { double result = 1. 0; int i; for(i = 0; i < y; i++) result *= x; return 0; } return result; }
예제: 모듈 프로그래밍 l 4개의 모듈로 나누어 작성하라. l l main. c: main() m 1. c: housekeeping() m 2. c: main. Loop(), dept. Break() m 3. c: finish() void main. Loop(void) { if (dept != old. Dept) // 새로운 부서가 읽혀지면 dept. Break(); printf("%dtt%dn", tran. Num, amt); // print trans details dept. Total = dept. Total + amt; //update dept. Total } void dept. Break(void) { printf("The total amount of %dt: %dnn", dept. Total); // print the total amount of the current dept. Total = 0; // reinitialize dept. Total old. Dept = dept; // reset old. Dept } void finish(void) { dept. Break(); fclose(in); } #include <stdio. h> #include <stdlib. h> FILE *in; int f. Eof; int tran. Num, amt, dept, old. Dept, dept. Total; char head 1[] = "DEPARTMENT STORE SALES"; char head 2[] = "Tranactiont. Amount"; // 함수 선언 void housekeeping(void); void main. Loop(void); void finish(void); void dept. Break(void); void main(void) { housekeeping(); // 사전 작업 while (f. Eof != EOF) { // 파일에 포함된 각 레코드를 처리 main. Loop(); // 레코드 처리 f. Eof = fscanf(in, "%d %d %d", &tran. Num, &amt, &dept); // 다음번째 레코드 읽기 } finish(); // 마무리 작업 } void housekeeping(void) { in = fopen("store. dat", "r"); if (in == (FILE*) NULL) { printf("n. Failed to open the file %sn", "store. dat"); exit(1); } printf("%sn", head 1); printf("%sn", head 2); // 헤딩 출력 f. Eof = fscanf(in, "%d %d %d", &tran. Num, &amt, &dept); // 파일로부터 첫번째 레코드 읽어들이기 old. Dept = dept; // 변수 초기화 dept. Total = 0; 27 }
예제: 모듈 프로그래밍 프로그램 공통 헤더파일 pgm. h를 작성 l pgm. h #ifndef _pgm_h #define _pgm_h #include <stdio. h> #include <stdlib. h> extern FILE *in; int f. Eof; int tran. Num, amt, dept, old. Dept; int dept. Total; typedef struct{ int trans. Id; int amt; } trans. Rec; #endif #include <stdio. h> #include <stdlib. h> FILE *in; int f. Eof; int tran. Num, amt, dept, old. Dept, dept. Total; char head 1[] = "DEPARTMENT STORE SALES"; char head 2[] = "Tranactiont. Amount"; // 함수 선언 void housekeeping(void); void main. Loop(void); void finish(void); void dept. Break(void); void main(void) { … } void housekeeping(void) { … } 28
예제: 모듈 프로그래밍 l 모듈 인터페이스 작성 m 2. h m 1. h #ifndef _m 1_h #define _m 1_h #ifndef _m 2_h #define _m 2_h m 3. h #ifndef _m 3_h #define _m 3_h void housekeeping(void); void main. Loop(void); void dept. Break(void); void finish(void); #endif m 1. c void housekeeping() {…} m 2. c void main. Loop() {…} Void dept. Break() { …} m 3. c void finish() {…} 29
예제: 모듈 프로그래밍 l 모듈 작성 m 1. c main. c #include ##include "pgm. h" #include "m 1. h" “pgm. h” “m 1. h” “m 2. h” “m 3. h” FILE *in; // 전역 변수 정의 int f. Eof; int tran. Num, amt, dept, old. Dept; int dept. Total; char head 1[] = “DEPARTMENT STORE SALES”; char head 2[] = “Transaction t Amount”; trans. Rec rec; Void main(void) { …} extern char head 1[]; extern char head 2[]; extern trans. Rec rec; void housekeeping(void) {…} m 2. c #include "pgm. h" #include "m 2. h" extern trans. Rec rec; void main. Loop() {…} void dept. Break() {…} m 3. h #include "pgm. h" #include "m 2. h" #include "m 3. h" void finish() {…} 30
문제 #1: C 코드 l get. Ready() 34
문제 #1: C 코드 l process. Request() 35
- Slides: 35