hunterwolf 发布留言 2008-6-25 11:51 请各位帮忙解释一下这段源代码 希望能详细的解释 谢谢!!请各位帮忙解释一下这段源代码 希望能详细的解释 #include #include
char* month_str[]={"January","February","March","April","May","June","July","August","September","October","November","December"}; char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int IsLeapYear(int year) /*find out the year is leap year or not*/ { if((year%4==0&&year%100!=0)||(year%400==0)) return 1; else return 0;
} int month_day(int year,int month) { int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31}; if(IsLeapYear(year)&&month==2) return 29; else return(mon_day[month-1]);
} int DaySearch(int year,int month,int day) /*search what day this day is*/ { int c=0; float s; int m; for(m=1;mc=c+month_day(year,m); c=c+day; s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c; return ((int)s%7); }
int PrintAllYear(int year)/*print the all year*/ { int temp; int i,j; printf("\n\n%d Calander\n",year); for(i=1;i<=12;i++) { printf("\n\n%s(%d)\n",month_str,i); printf("0 1 2 3 4 5 6 \n"); printf("S M T W T F S \n\n"); temp=DaySearch(year,i,1); for(j=1;j<=month_day(year,i)+temp;j++) { if(j-temp<=0) printf(" "); else if(j-temp<10) printf("%d ",j-temp); else printf("%d ",j-temp);
if(j%7==0) printf("\n"); } } return 0; }
int main() { int option,da; char ch; int year,month,day; printf("\nWelcome to use the WanNianLi system\n");
while(1) { printf("\nPlease select the service you need:\n"); printf("\n1 Search what day the day is"); printf("\n2 Search whether the year is leap year or not"); printf("\n3 Print the calander of the whole year"); printf("\n4 Exit\n"); scanf("%d",&option);
switch(option) { case 1: while(1) { printf("\nPlease input the year,month and day(XXXX,XX,XX):"); scanf("%d,%d,%d,%c",&year,&month,&day); da=DaySearch(year,month,day); printf("\n%d-%d-%d is %s,do you want to continue?(Y/N)",year,month,day,week[da]); fflush(stdin); scanf("%c",&ch); if(ch=='N'||ch=='n') break; } break; case 2: while(1) { printf("\nPlease input the year which needs searched?(XXXX)"); scanf("%d",&year); if(IsLeapYear(year)) printf("\n%d is Leap year,do you want to continue?(Y/N)",year); else printf("\n%d is not Leap year,do you want to continue(Y/N)?",year); fflush(stdin); scanf("%c",&ch); if(ch=='N'||ch=='n') break; } break; case 3: while(1) { printf("\nPlease input the year which needs printed(XXXX)"); scanf("%d",&year); PrintAllYear(year); printf("\nDo you want to continue to print(Y/N)?"); fflush(stdin); scanf("%c",&ch); if(ch=='N'||ch=='n') break; } break; case 4: fflush(stdin); printf("Are you sure?(Y/N)"); scanf("%c",&ch); if(ch=='Y'||ch=='y') exit(0); break; default: printf("\nError:Sorry,there is no this service now!\n"); break; }
}
return 0; }liyanhong 发布留言 2008-6-25 12:00 自己看不懂 直接PASSorzorz 发布留言 2008-6-25 14:32 想要注释,是吧? 找我QQ: 724393224hunterwolf 发布留言 2008-6-25 22:45 注释关键的几句也可以...谢谢各位了!StarWing83 发布留言 2008-6-25 22:55 万年历啊…… 这东西,最省力的方法是打表…… 当然LZ这种也不错……neverTheSame 发布留言 2008-6-25 23:58 这个程序大概是打印全年的日历,查看今天日期,查看今年是不是闰年。hunterwolf 发布留言 2008-6-26 08:56 楼上说的非常正确 因为这是我以为朋友做的,所以有几处我不太明白。 比如:int month_day(int year,int month) { int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31}; if(IsLeapYear(year)&&month==2) return 29; else return(mon_day[month-1]); }
int DaySearch(int year,int month,int day) /*search what day this day is*/ { int c=0; float s; int m; for(m=1;mc=c+month_day(year,m); c=c+day; s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c; return ((int)s%7); } 不太明白...希望各位高手能讲解一下lai832 发布留言 2008-6-26 09:49 程序有BUG大家试运行,打印2099年的日历 问题应该在: s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c; (还在改运算中:)hunterwolf 发布留言 2008-6-26 13:12 我刚发现...那请问楼上如何改正呢?谢谢!hunterwolf 发布留言 2008-6-26 13:13 不知道从多少年之后就开始不对了...大家能否帮忙改正呢?lai832 发布留言 2008-6-26 13:47 还你(有部分地方已改)#include #include
char* month_str[]={"January","February","March","April","May","June","July","August","September","October","November","December"}; //定义月的名字 char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; //字一周七天的名字 int IsLeapYear(int year) /*find out the year is leap year or not*/ { if((year%4==0&&year%100!=0)||(year%400==0))//润年返回1 return 1; else//否则返回0 return 0; }
int month_day(int year,int month)//找定义各个月的最大天数 { int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31}; if(IsLeapYear(year)&&month==2)//如是润年的2月返回29 return 29; else//否则返回当月的最大天数 return(mon_day[month-1]); }
int DaySearch(int year,int month,int day) /*search what day this day is*/ { int c=0; float s; int m; for(m=1;m c=c+month_day(year,m); c=c+day;//再加上mm月的天数 (输入的day) s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c; //以2040年为参考;year-1:减去本年;+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400:补上润年; -40:修正 if(year>2039)//参考年及后面的年份-1天;修正 s=s-1; return ((int)s%7);//计算当天是星期几 }
int PrintAllYear(int year)/*print the all year*/ { int temp; int i,j; printf("\n%d Calander\n",year); for(i=1;i<=12;i++)//输出一年的日历 { printf("\n%s\n",month_str); // printf("0 1 2 3 4 5 6 \n"); printf("Sun Mon Tue Wed Thu Fri Sat \n"); temp=DaySearch(year,i,1); for(j=1;j<=month_day(year,i)+temp;j++)//输出一个月的日历 { if(j-temp<=0) printf(" "); else printf("%3d ",j-temp); if(j%7==0)//输完一周(周六)后回车换行输出下一周 printf("\n"); } } return 0; }
int main() { int option,da; char ch; int year,month,day; while(1) { printf("\nPlease select the service you need:\n");//输出菜单 printf("\n1 Search what day the day is"); printf("\n2 Search whether the year is leap year or not"); printf("\n3 Print the calander of the whole year"); printf("\n4 Exit\n"); scanf("%d",&option);//选择 switch(option)//事件选择 { case 1://查询某天是周几 while(1) { printf("\nPlease input the year,month and day(XXXX,XX,XX):"); scanf("%d,%d,%d,%c",&year,&month,&day); da=DaySearch(year,month,day); printf("\n%d-%d-%d is %s,do you want to continue?(Y/N)",year,month,day,week[da]); fflush(stdin); scanf("%c",&ch); if(ch=='N'||ch=='n') break; } break; case 2://查询某年是否润年 while(1) { printf("\nPlease input the year which needs searched?(XXXX)"); scanf("%d",&year); if(IsLeapYear(year)) printf("\n%d is Leap year,do you want to continue?(Y/N)",year); else printf("\n%d is not Leap year,do you want to continue(Y/N)?",year); fflush(stdin); scanf("%c",&ch); if(ch=='N'||ch=='n') break; } break; case 3://输出某年的日历 while(1) { printf("\nPlease input the year which needs printed(XXXX)"); scanf("%d",&year); PrintAllYear(year); printf("\nDo you want to continue to print(Y/N)?"); fflush(stdin); scanf("%c",&ch); if(ch=='N'||ch=='n') break; } break; case 4://退出 fflush(stdin); printf("Are you sure?(Y/N)"); scanf("%c",&ch); if(ch=='Y'||ch=='y') exit(0); break; default: printf("\nError:Sorry,there is no this service now!\n"); break; } }return 0; }hunterwolf 发布留言 2008-6-26 13:59 非常感谢!!hunterwolf 发布留言 2008-6-28 22:34 请问各位,如何让这段程序显示的当前页与系统的日期为准,显示每一天(显示出日及对应的星期)?系统变到下一月,其自动翻到下一月。coming 发布留言 2008-6-29 00:40 什么闰年多一天少一天现在我都没怎么弄明白...真是愚钝...
页: [1] 特别说明:如网页特效代码中有引用图片文件等,请自己下载到本地调试! |