在C中打印包含月份和年份的日历

时间:2014-10-07 01:17:08

标签: c calendar

我试图创建一个程序,打印出用户输入的给定年份和月份的月份。到目前为止,我最终只打印出整个日历年。

#include<stdio.h>

int determineleapyear(int year);
int days_in_month[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
  {
    " ",
    "\n\n\nJanuary",
    "\n\n\nFebruary",
    "\n\n\nMarch",
    "\n\n\nApril",
    "\n\n\nMay",
    "\n\n\nJune",
    "\n\n\nJuly",
    "\n\n\nAugust",
    "\n\n\nSeptember",
    "\n\n\nOctober",
    "\n\n\nNovember",
    "\n\n\nDecember"
  };

int determinedaycode(int year)
{
  int d1, d2, d3;
  d1 = year/4.0;
  d2 = year/100.;
  d3 = year/400.;
  return 1 + (year + d1 + d2 + d3 + 1) % 7;
}

int determineleapyear(int year) 
{
    return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
}

void calendar(int year, int month, int daycode)
{
    int day;
    printf("%s", months[month]);
    printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n");

      //Correct the position for the first date
      for (day = 1; day <= 1 + daycode * 5; day++)
    {
      printf(" ");
    }

      //Print all the dates for one month
      for (day = 1; day <= days_in_month[month]; day++)
    {
      printf("%2d", day);

      //Is day before Sat? Else start next line Sun.
      if ((day + daycode) % 7 > 0)
        printf("   ");
      else
        printf("\n ");
    }
      // Set position for next month
      daycode = (daycode + days_in_month[month]) % 7;

      if (determineleapyear(year))
        days_in_month[2] = 29;
      printf("\n     %s %d\n", months[month], year);
}

//------------------------------------------
int main() {
  int daycode, month, year;

   printf("\nEnter the month: ");
   scanf("%d", &month);
   if (month < 1 || month > 12)
   {
        printf("Error: month must be in range 1..12");
        return printf("\nEnter the month: ");
        scanf("%d", &month);
   }

   printf("\nEnter the year: ");
   scanf("%d", &year);
   if (year < 0)
   {
        printf("Error: year must be > 0");
        return scanf("%d", &year);
   }

   daycode = determinedaycode(year);
   calendar(year, month, daycode);
   printf("\n");

   return 0;
}

任何人都可以帮我把它打印一个月吗?

1 个答案:

答案 0 :(得分:1)

您的calendar函数正在运行从1到12的循环,这会导致打印所有月份。删除循环并将其替换为参数以允许用户选择月份:

void calendar(int year, int month, int daycode)
{
    int day;

    printf("%s", months[month]);
    printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n");

    //Correct the position for the first date
    for (day = 1; day <= 1 + daycode * 5; day++)
    {
        printf(" ");
    }

    //Print all the dates for one month
    for (day = 1; day <= days_in_month[month]; day++)
    {
        printf("%2d", day);

        //Is day before Sat? Else start next line Sun.
        if ((day + daycode) % 7 > 0)
            printf("   ");
        else
            printf("\n ");
    }

    // Set position for next month
    daycode = (daycode + days_in_month[month]) % 7;
}

使用从输入读取的变量

在main中调用它
calendar(year, month, daycode);

我也强烈建议验证输入。如下所示:

printf("\nEnter the month: ");
scanf("%d", &month);
if (month < 1 || month > 12)
{
    printf("Invalid month selected");
    return -1;
}