打印一个12个月的日历,只输入年份。使用C.

时间:2013-09-06 18:47:18

标签: c algorithm calendar formatting

我坚持使用用户所需年份的程序,并在同一年打印出12个月的日历。这就是我到目前为止所有这一切,我很确定我有正确的方法来确定年份是否是闰年(在代码中)以及如何找出1月1日的时间(首先是变量)然后填写)。此外,我正在尝试以正常的日历格式打印此页面,其中月份位于顶部,然后是一周中的日期,然后是日期数字。任何帮助,将不胜感激。

查找第一天:

h = (1 + [(13(m + 1))/5] + K + [K/4] + [J/4] - 2J) mod 7

h = (1 + [(13(13 + 1))/5] + (year % 100) + [(year % 100)/4] + [(year/100)/4] - 2(year/100) % 7

H是开始日,M是月,K是年%100,J是年/ 100。

/* Calendar.c */

#include <stdio.h>

int main(void){

    int year, month, date;
    int startingDay; /* initfrom user input*/

    printf("Enter the year of your desired calendar: ");
    scanf("%d\n", &year);

    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
        int months[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    else 
        int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    for (month = 0; month < 12; month++) {
        const int daysInMonth = ; /* set # of days */
        int dayOfWeek;

        printf(…); //month name
        printf(…); //days of week

        for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++)
            printf(/*blanks*/);

        for (int date = 1; date <= daysInMonth; date++) {
            printf("…", date);

            if (++dayOfWeek>6) {
                printf("\n");
                dayOfWeek = 0;
            }
        } // for date

        if (dayOfWeek !=0)
            printf("\n");

        startingDay = dayOfWeek;
    } // for month
}

输出:

        February 2009
Sun Mon Tue Wed Thu Fri Sat
  1   2   3   4   5   6   7
  8   9  10  11  12  13  14

1 个答案:

答案 0 :(得分:4)

以下编译,但仍有改进的机会:

/* calender.c */

#include <stdio.h>
#include <stdlib.h>

#define JULIAN    1
#define GREGORIAN 2

/*
 * return the day of the week for particualr date
 * flag JULIAN or GREGORIAN
 * (the divisions are integer divisions that truncate the result)
 * Sun = 0, Mon = 1, etc.
 */
int get_week_day(int day, int month, int year, int mode) {
  int a, m, y;

  a = (14 - month) / 12;
  y = year - a;
  m = month + 12*a - 2;

  if (mode == JULIAN) {
    return (5 + day + y + y/4 + (31*m)/12) % 7;
  }

  return (day + y + y/4 - y/100 + y/400 + (31*m)/12) % 7; // GREGORIAN
}


int main(void) {

    int year, month, date;
    int startingDay;     /* of the week: init from user input*/
    char *names[] = {"January", "February", "March", "April", "May", "June",
                    "July", "August", "September", "October", "November", "December"};

    printf("Enter the year of your desired calendar: ");
    scanf("%d", &year);

    // could check whether input is valid here

    startingDay = get_week_day(1, 1, year,GREGORIAN);

    int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
       months[1] = 29;

    for (month = 0; month < 12; ++month) {
        const int daysInMonth = months[month];  /* set # of days */
        int dayOfWeek, date;

        printf("\n  ------------%s-------------\n", names[month]);   // month name
        printf("  Sun  Mon  Tue  Wed  Thu  Fri  Sat\n");         // days of week

        for (dayOfWeek = 0; dayOfWeek < startingDay; ++dayOfWeek)
            printf("     ");

        for (date = 1; date <= daysInMonth; ++date) {
            printf("%5d", date);

            if (++dayOfWeek > 6) {
                printf("\n");
                dayOfWeek = 0;
            }
        } // for date

        if (dayOfWeek != 0)
            printf("\n");

        startingDay = dayOfWeek;
    } // for month

    return EXIT_SUCCESS;
}