我被一个程序困住了,它记录用户想要的年份,并打印出同一年的12个月日历。这是目前为止我所拥有的一切,我非常确定我有正确的方法来确定年份是否是闰年(在代码中),以及如何确定一月一日是什么时候(下面先是变量,然后再填写)。另外,我试图用普通的日历格式打印这个,上面是月,下面是星期几,后面是日期任何帮助都将不胜感激。
查找第一天:
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
最佳答案
以下是汇编,但仍有改进的机会:
/* 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;
}
关于c - 打印12个月的日历,仅输入年份。使用C,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18664548/