Amazon.com Widgets 13-6 Simple way
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 01, 2014, 09:29:59 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 13
| | | |-+ 13-6 Simple way
Pages: [1] Go Down
Print
Author Topic: 13-6 Simple way (Read 959 times)
tadej5553
Full Member
***
Posts: 145


Email




on: May 21, 2010, 12:00:42 PM

Date.h
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Date : NSObject
{
    int day;
    int month;
    int year;
}

@property int day, month, year;

-(Date *) dateUpdate;

@end

Date.m
Code: (Objective-C)
#import "Date.h"

@implementation Date

@synthesize day, month, year;

-(Date *) dateUpdate
{
    Date *new = [[Date alloc] init];
    new.day = day;
    new.month = month;
    new.year = year;
   
    if(day == 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
    {
        if(month == 12) {
            new.day = 1;
            new.month = 1;
            new.year++;
        }
        else {
            new.day = 1;
            new.month++;
        }
    }
    else if(day == 30 && (month == 4 || month == 6 || month == 9 || month == 11))
    {
        new.day = 1;
        new.month++;
    }
    else if((day == 28 || day == 29) && month == 2)
    {
        if (day == 29) {
            new.day = 1;
            new.month++;
        }
        else if(day == 28 && !((!(year % 4) && (year % 100)) || !(year % 400)))
        {
            new.day = 1;
            new.month++;
        }
        else
            new.day++;
    }
    else
        new.day++;
   
    return new;
}
        
@end

main.m
Code: (Objective-C)
#import <Foundation/Foundation.h>  
#import "Date.h"

int main (int argc, const char * argv[])
{  
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
    
    Date *today = [[Date alloc] init];
    Date *leap = [[Date alloc] init];
    Date *newYear = [[Date alloc] init];
    Date *d1;
    Date *d2;
    Date *d3;

    today.day = 13;
    today.month = 3;
    today.year = 2009;
    
    leap.day = 28;
    leap.month = 2;
    leap.year = 2112;

    newYear.day = 31;
    newYear.month = 12;
    newYear.year = 2012;

    d1 = [today dateUpdate];
    d2 = [leap dateUpdate];
    d3 = [newYear dateUpdate];
    
    NSLog(@"%i.%i.%i", d1.day, d1.month, d1.year);
    NSLog(@"%i.%i.%i", d2.day, d2.month, d2.year);
    NSLog(@"%i.%i.%i", d3.day, d3.month, d3.year);
    
    [d1 release];
    [d2 release];
    [d3 release];
    [today release];
    [leap release];
    [newYear release];
    
    [pool drain];  
    return 0;  
}
Last Edit: May 23, 2010, 12:22:15 PM by tadej5553 Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.