如何打印日历

问题描述:

到目前为止,我已经完成了这项功能,我知道它有点长,但我还是想尝试一下。我需要打印一个类似于第j年的月份的日历。



例如:

输入:

年份:2015

月:5

输出:

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:

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

一些有用的链接。





http://www.codingunit.com/how-to-make-a-calendar-in-c [ ^ ]



使用C编程语言的日历 [ ^ ]
Hi, some helpful links .


http://www.codingunit.com/how-to-make-a-calendar-in-c[^]

Calendar using C Programming language[^]


他正在询问如何开始打印月份。

尽管以前的解决方案有一些有趣的链接,但更具体的是他需要一个函数来查看本月第一天的星期几,这通常是叫泽勒一致。这个算法有很多版本。

最简单的版本是Michael Keith和Tom Craver算法:

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:
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
}



返回[0,6]范围内的数字,其中0 =星期日,6 =星期五。

在这种情况下,您的日历应打印:


This return a number in the range [0, 6], where 0=sunday and 6=friday.
in this case your calendar should print:

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:

printmonth(mo, ye, numOfDays(mo, ye));