1

我被困在一个程序上,该程序需要用户期望的年份并打印出同年的 12 个月日历。这就是我到目前为止所拥有的,我很确定我有正确的方法来确定这一年是否是闰年(在代码中)以及如何找出一月 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 是 yr % 100,J 是 yr / 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
4

1 回答 1

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;
}
于 2013-09-06T20:16:31.883 回答