【发布时间】:2016-03-15 17:59:29
【问题描述】:
我正在处理一个大约 4 天前到期的日历项目,但我一直在处理它,即使分数会大幅降低,因为我想尝试理解它。到目前为止,我已经达到了在调试中打印出日历以及日历中正确天数的程度。我唯一要解决的问题是,我想让每个月的第一天从正确的位置开始,所以可能是某种循环+“”,这样它就可以将 4 个空格循环到该月的第一天或其他东西像那样。我也想要这样当我输入月份、年份和 num 月份时,月份和年份将显示一个月的日历,而 nummonths 将显示接下来的月份。这对我来说是一个很难理解的概念。帮助很大!赞赏!我一直在研究这个 WAYYY 很长时间了。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int getdaycode(int month, int year);
void printheader(int month, int year);
int getndim(int month, int year);
int main(void) {
int day, month, year, nummonths;
printf("Enter Month, Year, and number of Months: ");
if (scanf("%d %d %d", &month, &year, &nummonths) != 3
|| year < 0 || month < 1 || month > 12) {
printf("invalid input\n");
return 1;
}
for (int i = 0; i < nummonths; i++) {
printheader(month, year);
int numdays = getndim(month, year);
int daycode = getdaycode(month, year);
printf("%*s", daycode * 4, ""); /* print 4 spaces for each skipped day */
for (day = 1; day <= numdays; day++) {
printf("%3d", day);
daycode = (daycode + 1) % 7;
if (daycode != 0)
printf(" ");
else
printf("\n");
}
if (daycode != 0)
printf("\n");
printf("\n");
month = month + 1;
if (month > 12) {
month -= 12;
year += 1;
}
}
return 0;
}
int getdaycode(int month, int year)
{
int numdays;
{
numdays = ((year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400)); // how many days including exceptions
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //check if leapyear
{
if (month == 1) // January
numdays = numdays;
if (month == 2) // February
numdays = numdays + 31;
if (month == 3) // March
numdays = numdays + 28 + 31 + 1;
if (month == 4) // April
numdays = numdays + 31 + 28 + 31 + 1;
if (month == 5) // May
numdays = numdays + 30 + 31 + 28 + 31 + 1;
if (month == 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
}
else
{
if (month == 1) // January
numdays = numdays;
if (month == 2) // February
numdays = numdays + 31;
if (month == 3) // March
numdays = numdays + 28 + 31;
if (month == 4) // April
numdays = numdays + 31 + 28 + 31;
if (month == 5) // May
numdays = numdays + 30 + 31 + 28 + 31;
if (month == 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31;
if (month == 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
}
}
int daycode = (numdays + 1) % 7;
return daycode;
}
void printheader(int month, int year)
{
printf("%14d %1d\n", month, year);
printf("Sun ");
printf("Mon ");
printf("Tue ");
printf("Wed ");
printf("Thu ");
printf("Fri ");
printf("Sat\n");
}
int getndim(int month, int year)
{
int numdays;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //check if leapyear
{
if (month == 1) // January
numdays = 31;
if (month == 2) // February
numdays = 29;
if (month == 3) // March
numdays = 31;
if (month == 4) // April
numdays = 30;
if (month == 5) // May
numdays = 31;
if (month == 6) // June
numdays = 30;
if (month == 7) // July
numdays = 31;
if (month == 8) // August
numdays = 31;
if (month == 9) // September
numdays = 30;
if (month == 10) // October
numdays = 31;
if (month == 11) // November
numdays = 30;
if (month == 12) // December
numdays = 31;
}
else
{
if (month == 1) // January
numdays = 31;
if (month == 2) // February
numdays = 28;
if (month == 3) // March
numdays = 31;
if (month == 4) // April
numdays = 30;
if (month == 5) // May
numdays = 31;
if (month == 6) // June
numdays = 30;
if (month == 7) // July
numdays = 31;
if (month == 8) // August
numdays = 31;
if (month == 9) // September
numdays = 30;
if (month == 10) // October
numdays = 31;
if (month == 11) // November
numdays = 30;
if (month == 12) // December
numdays = 31;
}
return numdays;
}
【问题讨论】:
-
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
-
表示如果是闰年 2 月是 29 天,如果不是则 28 天
-
我只需要一种方法将我的日期代码乘以“”四个空格
-
而且我从未听说过这些命令,所以我假设如果我在代码中不使用任何命令会很奇怪