Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I did this functions so far and I know it's a litlle bit long but still I'd like to try. I need to print a calender which resembles the month i of the year j.

For example:
Input:
Year: 2015
Month: 5
Output:
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16                
17 18 19 20 21 22 23
24 25 26 27 28 29 30 
31


The functions:
C#
#include <stdio.h>

int main()
{

}

int isLeapYear(int y) /* Returns 1 if the year is leaped. */
{
    int i = 0;
    if (y % 4 == 0 && y % 100 != 0) {
        i++;
        return i;
    };
    if (y % 400 == 0) {
        i++;
        return i;
    };
    if (y % 4 != 0)
        return i;
}

int yearStartDay(int y) /* Returns the day which the year starts at. 1 for Sunday to 7 for Saturday. */
{
    int i, j = 5;
    if (y == 2015)
        return j;
    if (y < 2015) {
        for (i = 2015; i > y; i--) {
            j--;
            if (isLeapYear(i - 1)  )
                j -= 1;
            if (j < 1)
                j = 7;
        }
        return j;
    }
    if (y > 2015) {
        for (i = 2015; i < y; i++) {
            j++;
            if (isLeapYear(i + 1))
                j += 1;
            if (j > 7)
                j = 1;
        }
        return j;
    }
}

int numOfDays(int m, int y) /* Returns the number of days in a month of a year. 1 for January to 12 for December. */
{
    int i;
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
        i = 31;
        return i;
    };
    if (m == 4 || m ==6 || m ==9 || m == 11) {
        i = 30;
        return i;
    };
    if (isLeapYear(y))
        i = 29;
    else
        i = 28;
    return i;
}

int readYear(int y) /* Returns year. */
{
    printf("Enter year: ");
    scanf("%d", y)
    return y;
}

int readMonth(int m) /* Returns month. */
{
    printf("Enter month: ");
    scanf("%d", m)
    return m;
}

void printCalendar(int m, int y) /* Prints the calendar of the month in the year. */
{
    int mo, ye, num, start;
    ye = readYear(y);
    mo = readMonth(m);
    num = numOfDays(mo, ye);
    start = yearStartDay(ye);
    printf("Su Mo Tu We Th Fr Sa\n");
                ...
                ...
                ...
}
Posted
Comments
Mohibur Rashid 1-May-15 4:13am    
What is the problem? Where is the error?

 
Share this answer
 
He is quering how to start print the month.
Altough the previous solution have interesting links, to be more specific he needs a function to peek the day of week for the first day of the month, this is commonly called Zeller congruence. There are many versions of this algorithm around.
The simplest version is the Michael Keith and Tom Craver algorithm:
C++
int dayofweek(int d, int m, int y)
{
     return (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7
}

This return a number in the range [0, 6], where 0=sunday and 6=friday.
in this case your calendar should print:
C++
void printmonth(int m, int y, int lastday)
{
	int day = dayofweek(1, m, y);

	printf ("SU MO TU WE TH FR SA\n");
	for (int i=0; i<day;>		printf(" - ");
	for (day = 1; day<=lastday; day++)
	{
		int dow = dayofweek(day, m, y);
		if (!dow)
			putchar('\n');
		printf ("%2i ", day);
	}
	putchar('\n');
}

It has to be invoked as:
C++
printmonth(mo, ye, numOfDays(mo, ye));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900