当前位置:编程学习 > C/C++ >>

c或c++编写的万年历

基本要求:

所设计的系统要求界面,方便用户进行操作,完成以下功能:

(1)输入年份,能按挂历的格式输出全年所有月历(包括星期,农历)到文本文件中。

(2)输入年份和月份,能按挂历的格式输出月历(包括星期,农历)到文本文件中。

(3)输入年月日,输出相应的星期到文本文件。

(4)退出系统。(包括具体文档)

答案:#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

//日期类
class Date
{
private:
int year;
int month;
int day;
public:
void input1();//读入一个日期(年月日)
void input2();//读入一个日期(年月)
void input3();//读入一个年份
void output1();//输出万年历
void output2();//输出某月的天数
int output3();//返回某月天数的
bool panduan();//判断是否为闰年
int num();//计算某日期与公元1年1月1日的天数差
int num1();//计算某年某月1日与公元1年1月1日的天数差
void ahead();//某日期加上某天数
void back();//某日期减去某天数
};

int Date::num1()
{
int i,k,j=0,a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31},b[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
long int n1,n2=0,n;
for(i=1;i<=year;i++)
{
if((i%400==0)||((i%4==0)&&(i%100!=0)))
j++;
}
for(k=0;k<=month-1;k++)
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
n2=n2+a[k];
else
n2=n2+b[k];
}
n1=j*366+(year-1-j)*365;
n=n1+n2;
return n;
}

void Date::output1()
{

cout<<" "<<year<<"年"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<" "<<month<<"月"<<endl;
cout<<" "<<"星期日"<<" "<<"星期一"<<" "<<"星期二"<<" "<<"星期三"<<" "<<"星期四"<<" "<<"星期五"<<" "<<"星期六"<<endl;
int d=1;
for(int i=0;i<7;i++)
{
if((num1()+1)%7==i)
{
cout<<setw(10)<<d;
break;
}
else
{
cout<<setw(10)<<' ';
}
}
while(d<output3())
{
if((num1()+d)%7==6)
{
d++;
cout<<endl;
cout<<setw(10)<<d;
}
else
{
d++;
cout<<setw(10)<<d;
}
}
cout<<endl;
}

void Date::back()
{
int n,i;
cout<<"请输入需要减的天数:";
cin>>n;
for(i=1;i<=n;i++)
{
day--;
if(month==2||4||6||8||9||12||1)
{
if(day<1)
{
day=31;
month--;
}
}
else
{
if(month==3)
{
if(day<1)
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
{
day=29;
month--;
}
else
{
day=28;
month--;
}
}
}
else
{
if(day<1)
{
day=30;
month--;
}
}
}
if(month<1)
{
month=12;
year--;
}
}
cout<<"新的日期为:"<<year<<"/"<<month<<"/"<<day<<endl;
}


void Date::ahead()
{
int n,i;
cout<<"请输入需要加的天数:";
cin>>n;
for(i=1;i<=n;i++)
{
day++;
if(month==1||3||5||7||8||10||12)
{
if(day>31)
{
day=1;
month++;
}
}
else
{
if(month==2)
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
{
if(day>29)
{
day=1;
month++;
}
}
else
if(day>28)
{
day=1;
month++;
}
}
else
{
if(day>30)
{
day=1;
month++;
}
}
}
if(month>12)
{
month=1;
year++;
}
}
cout<<"新的日期为:"<<year<<"/"<<month<<"/"<<day<<endl;
}

int Date::num()
{
int i,k,j=0,a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31},b[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
long int n1,n2=0,n;
for(i=1;i<=year;i++)
{
if((i%400==0)||((i%4==0)&&(i%100!=0)))
j++;
}
for(k=0;k<=month-1;k++)
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
n2=n2+a[k];
else
n2=n2+b[k];
}
n1=j*366+(year-1-j)*365;
n=n1+n2+day-1;
return n;
}

int Date::output3()
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
{
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
return 31;
else
if(month==2)
return 29;
else
return 30;
}
else
{
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
return 31;
else
if(month==2)
return 28;
else
return 30;
}
}

void Date::output2()
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
{
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
cout<<"该月有31天."<<endl;
else
if(month==2)
cout<<"该月有29天."<<endl;
else
cout<<"该月有30天."<<endl;
}
else
{
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
cout<<"该月有31天."<<endl;
else
if(month==2)
cout<<"该月有28天."<<endl;
else
cout<<"该月有30天."<<endl;
}
}

bool Date::panduan()
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
return true;
else
return false;
}

void Date::input1()
{
cout<<"请输入年月日,以空格分开:";
cin>>year;
cin>>month;
cin>>day;
}

void Date::input2()
{
cout<<"请输入年月,以空格分开:";
cin>>year;
cin>>month;
}

void Date::input3()
{
cout<<"请输入年份:";
cin>>year;
}

//主函数
int main()
{
char x,y;
Date a,b;
cout<<"请选择:"<<endl;
cout<<"0.结束操作"<<endl;
cout<<"1.输出万年历"<<endl;
cout<<"2.判断是否为闰年"<<endl;
cout<<"3.计算某个月的天数"<<endl;
cout<<"4.天数的增减"<<endl;
cout<<"5.计算某两个日期间的天数"<<endl;
cout<<"6.计算某一天是星期几"<<endl;
cout<<endl;
cin>>x;
while(x!='0')
{
switch(x)
{
case'1':a.input2();
a.output1();
break;
case'2':a.input3();
if(a.panduan())
cout<<"该年是闰年,该年天数为366."<<endl;
else
cout<<"该年不是闰年,该年天数为365."<<endl;
cout<<endl;
break;
case'3':a.input2();
a.output2();
cout<<endl;
break;
case'4':a.input1();
cout<<"请选择:"<<endl;
cout<<"0.结束操作"<<endl;
cout<<"1.向后"<<endl;
cout<<"2.向前"<<endl;
cout<<endl;
cin>>y;
while(y!='0')
{
switch(y)
{
case'1':a.ahead();
cout<<endl;
break;
case'2':a.back();
cout<<endl;
break;
default:cout<<"输入错误!"<<endl;
cout<<endl;
break;
}
cout<<"请选择:"<<endl;
cout<<"0.结束操作"<<endl;
cout<<"1.向后"<<endl;
cout<<"2.向前"<<endl;
cout<<endl;
cin>>y;
}
break;
case'5':a.input1();
b.input1();
cout<<"所求的日期差为:"<<fabs(a.num()-b.num())<<endl;
cout<<endl;
break;

上一个:关于 C++的 若干问题
下一个:高手帮忙写个c++程序!!!

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,