您的位置:首页 > 其它

来个超级大模拟-----日期计算器

2009-10-07 00:59 218 查看
以前写过的一个东东,思想就是模拟,考虑的情况比较多(闰年等等),程序功能就是给定当前时间和整数m,帮你计算m天后的具体日期,包括周几。输入格式为 2009 10 1 1 1000 ps:当前时间是2009年10月1日周1,计算1000天后的确切日期,仍按输入格式输出。转载注明哪来的哦。。。放码子:

#include<iostream>
using namespace std;
int num,tmp,y;
int month[2][12]={31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31};
class pzj//闰年2月29天
{
private:
int year1,year2;//闰年366天
public:
pzj(int a,int b){year1=a,year2=b;}
int getyear(bool);
int* getmon(bool);
~pzj(){}
};
int pzj::getyear(bool k)
{
if(!k)
return year1;
else
return year2;
}
int* pzj::getmon(bool k)
{
if(!k)
return month[0];
else
return month[1];
}
void display(int i)
{
if(i==1)
printf("按中国的纪年法--YYMMDD--输入当前日期(包括周几):/n");
if(i==2)
printf("%d天后的日期是:/n",tmp);
if(i==3)
printf("请输入您想计算多少天后的确切日期:/n");
if(i==4)
printf("继续查询请继续输入,终止程序请键入-1/n");
}
bool judge(int k)
{
if(y%400==0||(y%4==0&&y%100!=0))
return 0;
else
return 1;
}
int main()
{
int ye,mo,da,wee;
int i,j,ter;
int m,d,week;
int *getm;//无用变量
pzj pz(366,365);
printf("/t/t%c欢迎使用senjing日期查询器,bug之处敬请指正%c/n",')'-'(',-('('-')'));
display(1);
scanf("%d%d%d%d",&y,&m,&d,&week);
display(3);
wee=week;
mo=m,da=d,ye=y;
while(scanf("%d",&num),num+1)
{
y=ye,d=da,mo=m;//每次输入重新初始化为原来的值
j=0;
i=0;
tmp=num;
ter=0;
week=wee;
week+=num%7;//求周几
if(week!=7)
week%=7;
if(!judge(y))//闰年
{
getm=pz.getmon(1);//无用句
while(num>=pz.getyear(judge(y+ter)))//求年份
{
num-=pz.getyear(judge(y+ter));//随着年份的增加,要不停判断是否是闰年
ter++;
}
i=m-1;
while(num>month[judge(y+ter)][i])//月份,从初始月份开始累加
{
num-=month[judge(y+ter)][i];
i++;
}
m=i+1;
while(m>12)
{
ter++;
m%=12;
}
num+=d;
if(num>month[judge(y+ter)][m-1])
{
j++;
num-=month[judge(y+ter)][m-1];
}
m+=j;
while(m>12)
{
ter++;
m%=12;
}
}
else
{
getm=pz.getmon(judge(y+ter));
while(num>=pz.getyear(judge(y+ter)))//求年份
{
num-=pz.getyear(judge(y+ter));
ter++;
}
i=m-1;
while(num>month[judge(y+ter)][i])//月份
{
num-=month[judge(y+ter)][i];
i++;
}
m=i+1;
while(m>12)
{
ter++;
m%=12;
}
num+=d;
while(num>month[judge(y+ter)][m-1])
{
num-=month[judge(y+ter)][m-1];
j++;
}
m+=j;
while(m>12)
{
ter++;
m%=12;
}
}
display(2);
printf("%d-%d-%d,星期%d/n",ter+y,m,num,week);
display(4);
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: