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");
}

このコードは、ターミナルで入力された年のカレンダーを生成します。私の質問は、これをこの C 構文ではなく、Objective-C 構文に変換する方法です。これは単純なプロセスだと確信していますが、私は客観的にはかなりの初心者です.cと私はココアプロジェクトに必要です. このコードは、最後の月がヒットするまで、連続した一連の文字列としてカレンダーを出力します。端末でカレンダーを作成する代わりに、入力された年に応じて一連の NSMatrix が依存するカレンダーを入力するにはどうすればよいですか。

誰かがこのおかげで私を助けてくれることを願っています.

4

2 に答える 2

2

NSCalendarこれとさらに多くの機能を提供する 標準クラスを見ることをお勧めします。

たとえば、特定の日付の 1 か月 (または 1 週間) の日数を計算するには、次のメソッドを使用できます。

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

役に立つかもしれないいくつかのクラス:NSDateComponentsおよびNSDateFormatter.

また、c コードは Objective-C で完全に有効であるため、プログラムは問題なく実行できるはずです (ただし、入力を変更する必要がある場合を除く)。

于 2010-05-20T14:49:28.827 に答える
0

Objective-CはCの厳密なスーパーセットです。したがって、少なくともロジック部分はコードを使用できます。もちろん、GUI呼び出し用に入力( scanf)と出力( )を変換する必要があります。printf

とはいえ、車輪の再発明はしないでください。NSDatePicker既製のUIクラスであるを使用して、日付を表示および選択できます。ドキュメントを参照してください。

于 2010-05-20T18:36:38.970 に答える