Enter the year, then print the calendar

I am fixated on the following problem from my C programming class:

Write a program in which the user enters a year and then displays a calendar (for the whole year).

I have no idea how to approach this problem. Usually I can start homework (this is an optional problem), but I'm really lost. We worked in chapters 1-10 of Deitel and Deitel (loops, arrays, pointers, I / O, etc.), but I don’t know how to approach this at all. Any hints or suggestions would be appreciated.

+5
source share
10 answers

This can help you understand the math of the calendar. If the fabulous book Calendar Calculations is not included in your university library, they can get you a reprint of an article by the same authors in Software and Practice. And ask your professor to request a book for the library.

+4
source

In general, when you have a big problem like this, you want to break it down into small problems that are easier to solve.

One small problem may arise here: if you know how many days a month, and on what weekday the first month comes, can you display a calendar for this month?

+3
source

?: -)

char command[]="cal 2010";
sprintf(command,"cal %d",argv[1]);
system(command);

Unix cal .

+1

localtime (3) mktime (3). , . ( , 1 , taht , ), , , .

, , , .

0

, - , , 1 .

( , num_days[]), .

, . , for(i=0;i<NumMonths;++i). , , , . month[i], .

Sun Mon Tue... .

1 ( FirstDay), . , max_month[i], 31 ( ). FirstDay.

0

, . -, , 1 . , , . Google. , , - , 12 .

, . 29 . , , , . .

0

doomsday. " ", 31 , , 2008 , . .

0

, :

. mktime() : , , . "2010-01-60" 29 2010 . 1970 . ( 100%, " t, unix 1 1970 , , , mktime() unix).

Pseudo-Code, (YYYY-MM-DD):

void print_cal( int year ) {
    static char weekdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    struct tm tm;
    for( int day=0; day<365; ++day ) {
        memset( &tm, 0, sizeof(tm) );
        tm.tm_year = year - 1900;
        tm.tm_mday = day;
        mktime( &tm ); // modifies tm

        printf( "%04d-%02d-%02d, %s\n", tm.tm_year, tm.tm_mon+1, tm.tm_mday, weekdays[tm.tm_wday] );
    }
}

. , ! , .

EDIT: .

0
source

All Articles