主题:求星期几
程序有些长,不过很容易看懂,就是根据一些计算法则,给出一个年月日,输出是星期几,对于这个程序,在非闰年的情况下输出都是正确的,但是一到闰年,计算1,2月份的时候,就出错,应该是我标出的地方出错了,不过我找半天也没看出来。。。。。
#include <iostream>
using namespace std;
bool isleapyear(int year)
{ bool leap_year=((year%400==0)||(year%4==0&&year%100!=0));
return leap_year;
}
int getcenturyvalue(int year)
{ year=year/100;
year=year%4;
year=3-year;
year=year*2;
return year;
}
int getyearvalue(int year)
{int year1,year2;
year1=year%100;
year2=year1/4;
year=year1+year2;
return year;
}
int getmonthvalue(int month,int year)
{int m;
switch(month)
{ [color=800000]case 1: m=0;
if(isleapyear(year)==1) m=6;break;
case 2:m=3;
if(isleapyear(year)==1) m=2;break;[/color]
case 3: m=3;break;
case 4: m=6;break;
case 5: m= 1;break;
case 6: m=4;break;
case 7: m=6;break;
case 8: m=2;break;
case 9: m=5;break;
case 10: m=0;break;
case 11: m=3;break;
case 12: m=5;break;
}
return m;
}
void main()
{ int day,year,month,century,total;
cout<<"please enter the year,month,day:\n";
cin>>year>>month>>day;
century=getcenturyvalue(year);
year=getyearvalue(year);
month=getmonthvalue(month,year);
total=day+month+year+century;
total=total%7;
if(total==0) cout<<"It is Sunday!\n";
else if(total==1) cout<<"It is Monday!\n";
else if(total==2) cout<<"It is Tuesday!\n";
else if(total==3) cout<<"It is Wednesday!\n";
else if(total==4) cout<<"It is Thursday!\n";
else if(total==5) cout<<"It is Friday!\n";
else cout<<"It is Saturday!\n";
}
#include <iostream>
using namespace std;
bool isleapyear(int year)
{ bool leap_year=((year%400==0)||(year%4==0&&year%100!=0));
return leap_year;
}
int getcenturyvalue(int year)
{ year=year/100;
year=year%4;
year=3-year;
year=year*2;
return year;
}
int getyearvalue(int year)
{int year1,year2;
year1=year%100;
year2=year1/4;
year=year1+year2;
return year;
}
int getmonthvalue(int month,int year)
{int m;
switch(month)
{ [color=800000]case 1: m=0;
if(isleapyear(year)==1) m=6;break;
case 2:m=3;
if(isleapyear(year)==1) m=2;break;[/color]
case 3: m=3;break;
case 4: m=6;break;
case 5: m= 1;break;
case 6: m=4;break;
case 7: m=6;break;
case 8: m=2;break;
case 9: m=5;break;
case 10: m=0;break;
case 11: m=3;break;
case 12: m=5;break;
}
return m;
}
void main()
{ int day,year,month,century,total;
cout<<"please enter the year,month,day:\n";
cin>>year>>month>>day;
century=getcenturyvalue(year);
year=getyearvalue(year);
month=getmonthvalue(month,year);
total=day+month+year+century;
total=total%7;
if(total==0) cout<<"It is Sunday!\n";
else if(total==1) cout<<"It is Monday!\n";
else if(total==2) cout<<"It is Tuesday!\n";
else if(total==3) cout<<"It is Wednesday!\n";
else if(total==4) cout<<"It is Thursday!\n";
else if(total==5) cout<<"It is Friday!\n";
else cout<<"It is Saturday!\n";
}