'전역구조체'에 해당되는 글 1건

  1. 2010.07.18 전역구조체
Objective C/C언어 기능2010. 7. 18. 17:36

#import <Foundation/Foundation.h>


//////////////////////날짜 구조체 선언


전역 구조체를 정의

 

struct date

{

int month;

int day;

int year;

};


/////////////////////내일 날짜를 구하는 함수 : 반환 값과 인수가 구조체인 struct date 함수

struct date dateUpdate(struct date today)

{


 구조체를 이용하는 함수는 컴파일러에게 

 미리 함수의 역할(인수와 반환값 ) 대한 

 프로토콜을 정의한다고 알려준다

 

int numberOfDays( struct date d );


struct date tomorrow;

if(  today.day != numberOfDays(today) ){

tomorrow.day = today.day+1;

tomorrow.month = today.month;

tomorrow.year = today.year;

//해의 마지막

}else if( today.month == 12 ){

tomorrow.day = 1;

tomorrow.month = 1;

tomorrow.year = today.year + 1;

//달의 마지막

}else{

tomorrow.day = 1;

tomorrow.month = today.month + 1;

tomorrow.year = today.year;

}

return ( tomorrow );

}



///////////////////////매달 몇일이 있는지 찾는 함수

int numberOfDays( struct date d )

{

BOOL isLeapYear( struct date d );

int answer;

int daysPerMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ,31 };

아래 YES, NO int 정의한 define이다.

 

if( isLeapYear (d) == YES && d.month == 2 ){

answer = 29;

}else{

answer = daysPerMonth[ d.month - 1 ];

}

return ( answer );

}


//////////////////////윤년인지 구하는 함수

BOOL isLeapYear( struct date d )

{

if( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 ){

return YES;

}else{

return NO;

}

}




////////프로그램 부분

int main (int argc, const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//시세템에게 구조체를 이용한 함수의 프로토콜을 알려준다.

struct date dateUpdatestruct date today);

struct date thisDay, nextDay;

NSLog(@"Enter today's date (mm dd yyyy with out zero : 06 -> 6) :");

scanf("%i%i%i", &thisDay.month, &thisDay.day, &thisDay.year);

nextDay = dateUpdate( thisDay );

NSLog(@"Tomorrow's date is %i-%i-%.2i", nextDay.month, nextDay.day, nextDay.year % 100 );

[pool drain];

    return 0;

}













Posted by 버터백통