初学c++,简单的编了个查询星期的程序.望各路高手指点.
//*************************************//
// name:ÍòÄêÀú
// producer:Ë×ÀÇ
// adress:nudt
// data:2006/5/1
// 1700--2050
//*************************************//
#include<iostream>
#include<string>
using namespace std;
int Monthday(int d,int m,int y) //¼ÆËãËù²éÈÕ¾àÀëÏÂÒ»ÄêµÄÌìÊý¡£
{
int md;
switch(m)
{
case 12:md=32-d;break;
case 11:md=62-d;break;
case 10:md=93-d;break;
case 9:md=123-d;break;
case 8:md=154-d;break;
case 7:md=185-d;break;
case 6:md=215-d;break;
case 5:md=246-d;break;
case 4:md=276-d;break;
case 3:md=307-d;break;
case 2:if(y%4==0&&y%100!=0||y%400==0)md=336-d; else md=335-d;break;
case 1:if(y%4==0&&y%100!=0||y%400==0)md=367-d; else md=366-d;break;
}
return md;
}
void Xingqi(int xq) //ÅжÏÊÇÐÇÆÚ¼¸¡£
{
switch(xq)
{
case 1:cout<<"Fri";break;
case 2:cout<<"Thu";break;
case 3:cout<<"Mar";break;
case 4:cout<<"Tue";break;
case 5:cout<<"Mon";break;
case 6:cout<<"Sun";break;
case 0:cout<<"Sat";break;
}
}
void main()
{
int year,month,day,md,d,i,n,m,xq,l=1;
char yesorno;
string s;
do
{
m=0;
do
{
if(l==0)
cout<<"Sorry! but you had a mistake!\nPlease try again!\n";
cout<<"\n**Enter the year you want to inqure!(1700--2050)\n";
cin>>year>>month>>day;
l=0;
}while(year<1700||year>2050||month>12||month<1||day<1||day>31);
l=1;
if(year<2000)
{
if(year!=1999)
for(i=1999;i>year;i--)
{
if(i%4==0&&i%100!=0||i%400==0)
n=366;
else n=365;
m=m+n;
}
md=Monthday(day,month,year);
xq=(m+md)%7;
cout<<year<<"/"<<month<<"/"<<day<<endl;
cout<<"This day is : ";
Xingqi(xq);
}
else
{
for(i=2000;i<=year;i++)
{
if(i%4==0&&i%100!=0||i%400==0)
n=366;
else n=365;
m=m+n;
}
d=m%7;
md=Monthday(day,month,year);
xq=md%7;
cout<<year<<"/"<<month<<"/"<<day<<endl;
cout<<"This day is : ";
Xingqi((xq+7-d)%7);
}
cout<<endl;
cout<<"Do you want to Try again?(y/n) ";
cin>>yesorno;
}while(yesorno=='y'||yesorno=='Y');
cout<<"thank you for your using!\n"<<"Baybay!"<<endl<<endl<<"Enter ""exit"" to out!\n";
getline(cin,s);
}
还有什么更简便的算法,真心请教.不胜感激!
[此贴子已经被作者于2006-5-24 19:26:17编辑过]