再给你一个办法:把一年的日历都填在一个以星期为单元的数组中,再把数据抽出来输出。
挑一个感觉容易点的办法自己做吧,做得出这个,比你做五十道数学题有益得多。
挑一个感觉容易点的办法自己做吧,做得出这个,比你做五十道数学题有益得多。
授人以渔,不授人以鱼。
using namespace System; void PrintWeek(DateTime firstDayOfMonth, int row, int col); void main(void) { Console::Title = "年历测试程序"; Console::SetWindowSize(120, 40); Console::Clear(); int year = 2008; Console::SetCursorPosition(48, 0); Console::Write("{0:D}年年历", year); int row = 2; int col = 1; for (int month = 1; month <= 12; ++month) { PrintWeek(DateTime(year, month, 1), row, col); if (month % 3 == 0) { row += 7; col = 1; } else { col += 35; } } Console::SetCursorPosition(0, ++row); Console::Write("按任意键结束程序..."); Console::ReadLine(); } void PrintWeek(DateTime firstDayOfMonth, int row, int col) { Console::SetCursorPosition(col, row); Console::Write("{0,2:D} SUN MON TUE WED THU FRI SAT", firstDayOfMonth.Month); Console::SetCursorPosition(col + 3, ++row); for (DayOfWeek index = DayOfWeek::Sunday; index < firstDayOfMonth.DayOfWeek; ++index) { Console::Write(" "); } DayOfWeek counter = firstDayOfMonth.DayOfWeek; for (int day = firstDayOfMonth.Day; day <= DateTime::DaysInMonth(firstDayOfMonth.Year, firstDayOfMonth.Month); ++day) { Console::Write("{0,3:D} ", day); if (counter < DayOfWeek::Saturday) { ++counter; } else { Console::SetCursorPosition(col + 3, ++row); counter = DayOfWeek::Sunday; } } }
#include<stdio.h> int getweek(int year, int month, int week, char * str) { const int m[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int d[] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int leap, from, last, i; leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; year--; from = week * 7 - (year * 365 + year / 4 - year / 100 + year / 400 + d[month] + (month > 2 && leap) + 1) % 7 - 6; last = m[month] + (month == 2 && leap); for(i = 0; i < 7; i++, from++) sprintf(str + i * 4, (from > 0 && from <= last ? "%4d" : " "), from); return from <= last + 7; } void print_calendar(int year) { const char *week_title = " SUN MON TUE WED THU FRI SAT"; char s1[32], s2[32]; int i, j; printf("|------------------- The Calendar of Year %04d -------------------|\n", year); for(i = 1; i <= 6; i++) { printf("| %d %s %2d %s |\n", i, week_title, i + 6, week_title); for(j = 1; getweek(year, i, j, s1) | getweek(year, i + 6, j, s2); j++) printf("| %s %s |\n", s1, s2); } printf("|-----------------------------------------------------------------|\n"); } int main() { int year; printf("Please input the year whose calendar you want to know: "); scanf("%d", &year); print_calendar(year); return 0; }