views:

101

answers:

2
#define TRUE    1
#define FALSE   0

int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
    " ",
    "\n\n\nJanuary",
    "\n\n\nFebruary",
    "\n\n\nMarch",
    "\n\n\nApril",
    "\n\n\nMay",
    "\n\n\nJune",
    "\n\n\nJuly",
    "\n\n\nAugust",
    "\n\n\nSeptember",
    "\n\n\nOctober",
    "\n\n\nNovember",
    "\n\n\nDecember"
};


int inputyear(void)
{
    int year;

    printf("Please enter a year (example: 1999) : ");
    scanf("%d", &year);
    return year;
}

int determinedaycode(int year)
{
    int daycode;
    int d1, d2, d3;

    d1 = (year - 1.)/ 4.0;
    d2 = (year - 1.)/ 100.;
    d3 = (year - 1.)/ 400.;
    daycode = (year + d1 - d2 + d3) %7;
    return daycode;
}


int determineleapyear(int year)
{
    if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
    {
        days_in_month[2] = 29;
        return TRUE;
    }
    else
    {
        days_in_month[2] = 28;
        return FALSE;
    }
}

void calendar(int year, int daycode)
{
    int month, day;
    for ( month = 1; month <= 12; month++ )
    {
        printf("%s", months[month]);
        printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );

        // Correct the position for the first date
        for ( day = 1; day <= 1 + daycode * 5; day++ )
        {
            printf(" ");
        }

        // Print all the dates for one month
        for ( day = 1; day <= days_in_month[month]; day++ )
        {
            printf("%2d", day );

            // Is day before Sat? Else start next line Sun.
            if ( ( day + daycode ) % 7 > 0 )
                printf("   " );
            else
                printf("\n " );
        }
        // Set position for next month
        daycode = ( daycode + days_in_month[month] ) % 7;
    }
}


int main(void)
{
    int year, daycode, leapyear;

    year = inputyear();
    daycode = determinedaycode(year);
    determineleapyear(year);
    calendar(year, daycode);
    printf("\n");
}

This code generates a calendar of the inputed year in the terminal. my question is how can i convert this into a Objective-C syntax instead of this C syntax. im sure this is simple process but im quite of a novice to objective - c and i need it for a cocoa project. this code outputs the calendar as a continuously series of strings until the last month hits. soo instead of creating the calendar in the terminal how can i input the calendar a series of NSMatrixes depend on the inputed year.

hope somone can help me with this thanks or every helps (you be in the credits of the finished program) :) (the calendar is just a small part of the program i making and it is one of the important parts!!)

+2  A: 

I would suggest looking at standard NSCalendar class which provides this and many more functions as well.

For example to calculate the number of days in a month (or week) for a given date you can use the following method:

- (NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date

Some classes that may also be helpful: NSDateComponents and NSDateFormatter.

Note also that c code is completely valid in objective-c so you program should be able to run ok (except you may need to change its input)

Vladimir
i have look at that class. but what im looking for is this code to be converted to objective-c/cocoa. i cant just go NSCalender *calender = [[nscalendar alloc] init];[self drawCalender:[NSCalender calenderFromClass:calender]];i have to use NSCalender only is useful for pick what type of date you are using like gregorian, julian, or what ever. than you have to use nsdate to assign what date you are useing from the calender type. than you have to calculate how many days in a week and all that stuff. and that is what this code that i have now does. how can i convert this into Objective-C
Alec Niemy
A: 

Objective-C is a strict superset of C. So you can just use your code, at least the logic part. Of course you need to convert the input (scanf) and the output (printf) for GUI calls.

That said, don't re-invent the wheel. You can use NSDatePicker, which is a ready-made UI class to show and pick the date. See the documentation.

Yuji
I know that objC is a superset of c. i cant use date picker for what i going to use the calendar for. i all ready know that i could use NSDateComponets and NSDateFormatter but i want to use this code with a matrix. soo please answer my question not give me ways of create a calender use other stuff that i have aready worked with.
Alec Niemy