这周老师给布置一这么一题:
编一个年历的程序,当给出年的时候,输出整年的日历,要求标明星期一至日;当给出某年某月时,输出某年某月的日历,也要求标明星期一至日。
请问我该如何着手?我们这周学的是if...else...if,while,do...while,for,switch等语法。还没学函数。
#include<iostream> #include<cstdlib> using namespace std;
int main() { int year = 0; bool isLeapYear = false; int daySum = 0; int days = 0;
cout<<"Enter a year\n"; cin>>year; int temp = 0; int weekday = 0; int month = 1; if(year<=2004) { temp = year; while(temp!=2004) { isLeapYear = (temp % 4 == 0) && ((temp % 100 != 0) || (temp % 400) == 0); temp++; if(isLeapYear) daySum += 366; else daySum += 365; } daySum += 186; weekday = (1 - (daySum%7) + 7)%7; } else { temp = 2004; daySum += 180; temp++; while(temp<year) { isLeapYear = (temp % 4 == 0) && ((temp % 100 != 0) || (temp % 400) == 0); temp++; if(isLeapYear) daySum += 366; else daySum += 365; } weekday = 1 + (daySum%7); }
char theweekday[10]; switch(weekday) { case 1: strcpy(theweekday, "Monday"); break; case 2: strcpy(theweekday, "Tuesday"); break; case 3: strcpy(theweekday, "Wednesday"); break; case 4: strcpy(theweekday, "Thursday"); break; case 5: strcpy(theweekday, "Friday"); break; case 6: strcpy(theweekday, "Saturday"); break; case 7: strcpy(theweekday, "Sunday"); break; } for(month = 1; month<=12; month++) { switch(month) { case 1: cout<<"January\n"; break; case 2: cout<<"February\n"; break; case 3: cout<<"March\n"; break; case 4: cout<<"April\n"; break; case 5: cout<<"May\n"; break; case 6: cout<<"June\n"; break; case 7: cout<<"July\n"; break; case 8: cout<<"August\n"; break; case 9: cout<<"September\n"; break; case 10: cout<<"October\n"; break; case 11: cout<<"November\n"; break; case 12: cout<<"December\n"; break; } isLeapYear = (year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0); if(month == 2 && isLeapYear) days = 29; else if(month == 2) days = 28; else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) days = 31; else days = 30; cout<<"Mon Tue Wed Thu Fri Sat Sun\n"; if(weekday == 0) weekday = 7; for(int j =0; j<4*(weekday)-2; j++) cout<<' '; weekday = weekday % 7; for(int i = 1; i<=days; i++) { if(weekday == 0) { cout<<i<<endl; if(i<9) cout<<" "; else cout<<" "; } else if(i<9) cout<<i<<" "; else cout<<i<<" "; weekday++; weekday = weekday%7; } cout<<endl; } system("pause"); return 0; }
我来说说我的算法,算法是最主要的,有了一个好的算法,接下来的就是把算法落实为代码了。
我写程序的那天是7月5号。星期一。 以这一天为参照日,就可以知道任何一年的第一天为星期几。
我们首先求得那天和7月5号之间的差距,也就是他们之间相差几天,然后%7, 如果正好为0, 那么那一天也是星期一,确定是星期几,用这样一个算式对于年份小于等于2004 来说: (1 - daydistance%7+7)%7
同样的对于年份大于2004 的来说,我们也是求出 那一年的第一天与2004年7月5号之间的差值(daydistance), 将他%7 不过确定星期几的算式是另外一个 :1+daydistance%7
现在你知道了,那一年的第一天是星期几,接下来的依次推了。
关于那个头文件,他只是C++中用于输出输入的一个头文件, 如果我将cin 改为 scanf, 将cout 改为 printf, 那么就可将头文件iostream 改为 stdio.
我这里贴出的那个程序还不是OOP 程序,如果要写OOP程序,就要写一个 Kalender 的class 将相关的计算和打印工作都作为方法写入Kalender这个类中,在主程序中,也就是main 中 建立 类的对象, 以对象调用类中的方法即可,考虑到你的程序为c程序,所以就以过程化来写了。
// 刚才有一处还是忘了改了,现在完全改过了。
#include<stdlib.h> #include<stdio.h>
int main() { int year = 0; bool isLeapYear = false; int daySum = 0; int days = 0;
printf("Enter a year\n"); scanf("%d", &year); int temp = 0; int weekday = 0; int month = 1; if(year<=2004) { temp = year; while(temp!=2004) { isLeapYear = (temp % 4 == 0) && ((temp % 100 != 0) || (temp % 400) == 0); temp++; if(isLeapYear) daySum += 366; else daySum += 365; } daySum += 186; weekday = (1 - (daySum%7) + 7)%7; } else { temp = 2004; daySum += 180; temp++; while(temp<year) { isLeapYear = (temp % 4 == 0) && ((temp % 100 != 0) || (temp % 400) == 0); temp++; if(isLeapYear) daySum += 366; else daySum += 365; } weekday = 1 + (daySum%7); }
for(month = 1; month<=12; month++) { switch(month) { case 1: printf("January\n"); break; case 2: printf("February\n"); break; case 3: printf("March\n"); break; case 4: printf("April\n"); break; case 5: printf("May\n"); break; case 6: printf("June\n"); break; case 7: printf("July\n"); break; case 8: printf("August\n"); break; case 9: printf("September\n"); break; case 10: printf("October\n"); break; case 11: printf("November\n"); break; case 12: printf("December\n"); break; } isLeapYear = (year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0); if(month == 2 && isLeapYear) days = 29; else if(month == 2) days = 28; else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) days = 31; else days = 30; printf("Mon Tue Wed Thu Fri Sat Sun\n"); if(weekday == 0) weekday = 7; for(int j =0; j<4*(weekday)-2; j++) printf(" "); weekday = weekday % 7; for(int i = 1; i<=days; i++) { if(weekday == 0) { printf("%d\n",i); if(i<9) printf(" "); else printf(" "); } else if(i<9) { printf("%d",i); printf(" "); } else { printf("%d",i); printf(" "); } weekday++; weekday = weekday%7; } printf("\n"); } system("pause"); return 0; }