当前位置: 代码迷 >> C语言 >> [求助]计算任何一天是星期几?
  详细解决方案

[求助]计算任何一天是星期几?

热度:231   发布时间:2007-03-29 22:49:47.0
[求助]计算任何一天是星期几?
     RT谁帮忙说一下,多谢啦!!
----------------解决方案--------------------------------------------------------
首先要给出某一天的星期,然后再以此作为开始来计算天数.
----------------解决方案--------------------------------------------------------
以下是引用nuciewth在2007-3-29 23:02:52的发言:
首先要给出某一天的星期,然后再以此作为开始来计算天数.

我记得有一个数学公式

我去查查


----------------解决方案--------------------------------------------------------

在VC下运行

#include <iostream.h>
void main()
{int days=0,year,month,day,n,week,i;
int mont[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"Input a date:"<<endl;
cout<<"year:";
cin>>year;
cout<<"month:";
cin>>month;
cout<<"day:";
cin>>day;
if(year%400==0||(year%4==0&&year%100!=0))
mont[2]=29;
for(i=1;i<month;i++)
days+=mont[i];
days+=day;
n=year-1+(year-1)/4-(year-1)/100+(year-1)/400+days;
week=n%7;
cout<<"This date is:";
switch (week)
{case 0: cout<<"Sunday.";break;
case 1: cout<<"Monday.";break;
case 2: cout<<"Tuesday.";break;
case 3: cout<<"Wendesday.";break;
case 4: cout<<"Thursday.";break;
case 5: cout<<"Friday.";break;
case 6: cout<<"Saturday.";break;
}
cout<<endl;
}


----------------解决方案--------------------------------------------------------

俺自己写的万年历 那出来给你看上一眼
#include"stdio.h"
void weekday();
int leap(int);
int _2000yera(int x)
{
int sum=0;
int i;
if(x>=2000){
for(i=2000;i<x;i++)
{
sum+=(i%4==0&&i%100==0||i%400==0)?366:365;
}
return (sum+6)%7;
}
else{
for(i=x;i<2000;i++)
{
sum+=(i%4==0&&i%100==0||i%400==0)?366:365;
}
return (-sum%7+6)%7;
}

}
void weekday()
{
int i;
for(i=1;i<=7;i++)
printf("星期%d\t",i);
printf("\n");
}
int sp(int x)
{
int i;
for(i=1;i<=_2000yera(x);i++)
printf("\t");
return i;
}
int month(int ch,int x)
{
switch(ch)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2:return leap(x);
}
}
int leap(int x)
{
int b;
b=(x%4==0&&x%100!=0)||x%400==0?29:28;
return b;
}
int printyear(int x)
{
int mon,i=1,j,day;
printf("*********1月*********");
printf("\n");
weekday();
printf("\n");
i=sp(x);
for(day=1,mon=1;mon<=12;mon++)
{
for(j=1;j<=month(mon,x);j++,i++,day++)
{
printf("%d\t",j);
if(i%7==0){printf("\n");day=1;}
}
if(mon<12){
printf("\n");
printf("*********%d月*********\n",mon+1);
weekday();
printf("\n");
i=1;
i+=day-2;
for(;day-2>0;day--)
printf(" \t");}
}
}
main()
{
int x,exit;
printf("year:");
scanf("%d",&x);
printyear(x);
printf("\n输入任意数字结束:");
scanf("%d",&exit);

}


----------------解决方案--------------------------------------------------------
顶楼上的,为什么没解释程序
----------------解决方案--------------------------------------------------------
程序要自己慢慢看得
----------------解决方案--------------------------------------------------------
  相关解决方案