求万年历的代码!这个好像是高手的活
万年历日常生活中离不开年历。编写程序要求当用户输入年份时能输出该年的日历,在日历上能够看出某天是星期几,可以显示任意一年任意一天,并能够知道是否是闰年等。(以1980年1月1日星期二为基准)
说明一下我是初学!最近有了课程设计的任务 很紧!但是我们只上到了 指针就结束了!而且关于C中一些系统命令几乎不知道!大家给的代码要用最普通的代码写出来
如果写不出 ,告诉我这个万年历的思路是怎么样的啊!感谢各位啊
#include<stdio.h> #include <stdlib.h> bool funyear(int year) { if(year%4 == 0 && year%100 != 0 || year%400 == 0) return true; else return false; } int main() { unsigned int a; unsigned int year; while(1) { scanf("%d",&year); int sum = 2; for(a = 1980;a<year;a++) { if(funyear(a)) sum+=2; else sum++; } int start = sum%7; getchar(); int i,j,k; for(j = 1;j<=12;j++) { printf("Start is %d\n",start); printf("Month:%d\n",j); if(j == 1 ||j == 3 ||j == 5 || j == 7||j == 8||j == 10||j == 12) { printf("日 1 2 3 4 5 6\n"); int k = 0; for(k = 0;k<start;k++) printf("X "); for(i = 1;i<=31;i++) { k++; printf("%-2d ",i); if(k%7 == 0) printf("\n"); } start+=31; start = start%7; } else if(j == 4 ||j == 6 ||j == 9 || j == 11) { printf("日 1 2 3 4 5 6\n"); int k = 0; for(k = 0;k<start;k++) printf("X "); for(i = 1;i<=30;i++) { k++; printf("%-2d ",i); if(k%7 == 0) printf("\n"); } start+=30; start = start%7; } else if(funyear(year)) { printf("日 1 2 3 4 5 6\n"); int k = 0; for(k = 0;k<start;k++) printf("X "); for(i = 1;i<=29;i++) { k++; printf("%-2d ",i); if(k%7 == 0) printf("\n"); } start+=29; start = start%7; } else { printf("日 1 2 3 4 5 6\n"); int k = 0; for(k = 0;k<start;k++) printf("X "); for(i = 1;i<=28;i++) { k++; printf("%-2d ",i); if(k%7 == 0) printf("\n"); } start+=28; start = start%7; } printf("\n\n"); } } return 0; }刚刚完成
#include <stdio.h> void PrintMonth(int year, int month) { int preDays, preMonth, preYears, monthDays, i; char isLeap; preYears = year - 1; preDays = preYears * 365 + preYears / 4 - preYears / 100 + preYears / 400; isLeap = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) ? 1 : 0; preMonth = month - 1; switch(preMonth) { case 1: preDays += 31; break; case 2: preDays += 59; break; case 3: preDays += 90; break; case 4: preDays += 120; break; case 5: preDays += 151; break; case 6: preDays += 181; break; case 7: preDays += 212; break; case 8: preDays += 243; break; case 9: preDays += 273; break; case 10: preDays += 304; break; case 11: preDays += 334; break; } if(preMonth >= 2 && isLeap) preDays++; switch(month) { case 1: monthDays = 31; break; case 2: monthDays = 28; break; case 3: monthDays = 31; break; case 4: monthDays = 30; break; case 5: monthDays = 31; break; case 6: monthDays = 30; break; case 7: monthDays = 31; break; case 8: monthDays = 31; break; case 9: monthDays = 30; break; case 10: monthDays = 31; break; case 11: monthDays = 30; break; case 12: monthDays = 31; break; } if(month == 2 && isLeap) monthDays++; switch(month) { case 1: printf("January"); break; case 2: printf("February"); break; case 3: printf("March"); break; case 4: printf("April"); break; case 5: printf("May"); break; case 6: printf("June"); break; case 7: printf("July"); break; case 8: printf("August"); break; case 9: printf("September"); break; case 10: printf("October"); break; case 11: printf("November"); break; case 12: printf("December"); break; } printf(" %d", year); if(isLeap) printf(" leap year"); printf("\nSun Mon Tue Wed Thu Fri Sat\n"); preDays %= 7; if(preDays < 6) for(i = 0; i <= preDays; i++) printf(" "); for(i = 1; i <= monthDays; i++) { printf("%2d ", i); if((i + preDays) % 7 == 6) printf("\n"); } printf("\n"); } int main() { int year, month; for(;;) { scanf("%d %d", &year, &month); if(year <= 0 || month <= 0) break; PrintMonth(year, month); } return 0; }