| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 708 人关注过本帖
标题:日历制作
只看楼主 加入收藏
陌儿will
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-6-18
结帖率:0
收藏
已结贴  问题点数:10 回复次数:4 
日历制作
(1)输入年份和月份,显示年历和当月月历;
(2)输入两个日期,计算这两个日期之间的相隔天数;
代码如下:#include<iostream>
#include<string>
#include<iomanip>
using namespace std ;
class Date;

bool IsLeapYear(unsigned y);
string SwitchMonth(unsigned m);
string SwitchWeek(unsigned w);
void PrintMonth(unsigned y,unsigned m);
void PrintYear(unsigned y);
void CountDown(unsigned y1,unsigned m1,unsigned d1,Date &date3);
ostream& operator<<(ostream &out,const Date &date);

class Date
{
public:
    Date(unsigned m,unsigned d,unsigned y);
    void Before(int n);
    void After(int n);
    long Distance(Date &date);
        friend ostream& operator<<(ostream &out,const Date &date);
private:
    unsigned week,month,day,year;
};
Date::Date(unsigned m,unsigned d,unsigned y)
{
    unsigned i,total=0;
    month=m;
    day=d;
    year=y;
    for(i=1;i<year;i++)
    {
    if(IsLeapYear(i))
    total+=366;
    else
    total+=365;
    }
    for(i=1;i<month;i++)
    {
    if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
    total+=31;
    else if(i==4||i==6||i==9||i==11)
    total+=30;
    else if(i==2&&IsLeapYear(year))
    total+=29;
    else
    total+=28;
    }
    total+=day;
    week=total%7;
}
void Date::Before(int n)
{
    unsigned i,total=0;
    for(i=n;i>0;i--)
    {
        day--;
        if(day==0)
        {
            month--;
            if(month==0)
            {
            year--;
            month=12;
            }
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                day=31;
            else if(month==4||month==6||month==9||month==11)
                day=30;
             else if(month==2)
            {
                if(IsLeapYear(year))
                    day=29;
                else
                    day=28;
            }
        }
    }
        for(i=1;i<year;i++)
        {
            if(IsLeapYear(i))
                total+=366;
            else
                total+=365;
        }
        for(i=1;i<month;i++)
        {
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                total+=31;
            else if(i==4||i==6||i==9||i==11)
                total+=30;
            else if(i==2&&IsLeapYear(year))
                total+=29;
            else
                total+=28;
        }
        total+=day;
        week=total%7;
}
void Date::After(int n)
{
    unsigned i,total=0;
    for(i=n;i>0;i--)
    {
        day++;
        if(day==29&&month==2&&!IsLeapYear(year))
        {
            month++;
            day=1;
        }
        else if(day==30&&month==2)
        {
                        month++;
            day=1;
        }
        else if(day==31&&(month==4||month==6||month==9||month==11))
        {
            month++;
            day=1;
        }
        else if(day==32)
        {
            month++;
            day=1;
        }
                if(month==13)
        {
            year++;
            month=1;
        }
    }
        for(i=1;i<year;i++)
        {
            if(IsLeapYear(i))
                total+=366;
            else
                total+=365;
        }
        for(i=1;i<month;i++)
        {
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                total+=31;
            else if(i==4||i==6||i==9||i==11)
                total+=30;
            else if(i==2&&IsLeapYear(year))
                total+=29;
            else
                total+=28;
        }
        total+=day;
        week=total%7;
}
long Date::Distance(Date &date)
{
    long n=0;
    while(year<date.year)
    {
        day++;
        n++;
        if(day==29&&month==2&&!IsLeapYear(year))
        {
        month++;
        day=1;
        }
        else if(day==30&&month==2)
        {
        month++;
        day=1;
        }
        else if(day==31&&(month==4||month==6||month==9||month==11))
        {
        month++;
        day=1;
        }
        else if(day==32)
        {
        month++;
        day=1;
        }
        if(month==13)
        {
        year++;
        month=1;
        }
    }
    while(month<date.month)
    {
        day++;
        n++;
        if(day==29&&month==2&&!IsLeapYear(year))
        {
        month++;
        day=1;
        }
        else if(day==30&&month==2)
        {
        month++;
        day=1;
        }
        else if(day==31&&(month==4||month==6||month==9||month==11))
        {
        month++;
        day=1;
        }
        else if(day==32)
        {
        month++;
        day=1;
        }
    }
    while(day<date.day)
    {
        day++;
        n++;
    }
    if(year==date.year&&month==date.month&&day==date.day)
        return n;
    else return -1;
}
int main()
{
    unsigned a,n;
    unsigned y1,m1,d1,year,month;
    while(1)
    {
            cout<<"****************** 万年历 ******************\n";
        
            cout<<"\n";
            cout<<"\n";
            cout<<" (^_^) 请依次输入当前日期(按年、月、日):";
        cin>>y1>>m1>>d1;
            Date date1(m1,d1,y1);
            Date date2(m1,d1,y1);
            Date date3(m1,d1,y1);
            cout<<"请选择:\n*** 1.查询N天之前的日期;\n"
                <<"*M* 2.查询N天之后的日期;\n"
                <<"*E* 3.查询未来某一天距今天的日子;\n"
                <<"*N* 4.月历查询;\n"
                <<"*U* 5.年历查询;\n"
                <<"*** 6.退出查询.\n";
            cin>>a;
            switch(a)
            {
                case 1:
                        cout<<"请输入天数 :";
                        cin>>n;
                        date1.Before(n);
                        cout<<"日期是:";
                        cout<<date1;
                        break;
                case 2:
                        cout<<"请输入天数 :";
                        cin>>n;
                        date2.After(n);
                        cout<<"日期是:";
                        cout<<date2;
                        break;
                case 3:
                        CountDown(y1,m1,d1,date3);
                        break;
                case 4:
                        cout<<"请依次输入年份和月份:";
                        cin>>year>>month;
                        PrintMonth(year,month);
                        break;
                case 5:
                        cout<<"请输入年份:";
                        cin>>year;
                        PrintYear(year);
                        break;
                case 6:
                        cout<<"欢迎使用该系统! \n"<<endl;
                        exit(0);
            }
    }
    return 0;
}
bool IsLeapYear(unsigned y)
{
    return y%4==0&&y%100!=0||(y%400==0);
}
string SwitchMonth(unsigned m)
{
    switch(m)
    {
    case 1:
        return "一月";
        break;
    case 2:
        return "二月";
        break;
    case 3:
        return "三月";
        break;
    case 4:
        return "四月";
        break;
    case 5:
        return "五月";
        break;
    case 6:
        return "六月";
        break;
    case 7:
        return "七月";
        break;
    case 8:
        return "八月";
        break;
    case 9:
        return "九月";
        break;
    case 10:
        return "十月";
        break;
    case 11:
        return "十一月";
        break;
    case 12:
        return "十二月";
        break;
    default:
        return 0;
    }
}
string SwitchWeek(unsigned w)
{
    switch(w)
    {
    case 0:
        return "星期日";
        break;
    case 1:
        return "星期一";
        break;
    case 2:
        return "星期二";
        break;
    case 3:
        return "星期三";
        break;
    case 4:
        return "星期四";
        break;
    case 5:
        return "星期五";
        break;
    case 6:
        return "星期六";
        break;
    default:
        return 0;
    }
}
void PrintMonth(unsigned y,unsigned m)
{
    unsigned week,i,k,n,total=0;
    for(i=1;i<y;i++)
    {
    if(IsLeapYear(i))
    total+=366;
    else
    total+=365;
    }
    for(i=1;i<m;i++)
    {
    if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
    total+=31;
    else if(i==4||i==6||i==9||i==11)
    total+=30;
    else if(i==2&&IsLeapYear(y))
    total+=29;
    else
    total+=28;
    }
    total+=1;
    week=total%7;
    switch(m)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
                n=31;
                break;
        case 4:
        case 6:
        case 9:
        case 11:
                n=30;
                break;
        case 2:
                if(IsLeapYear(y))
                    n=29;
                else
                    n=28;
    }
    string s[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    cout<<"*** "<<y<<" "<<SwitchMonth(m)<<" ***"<<endl;
    for(i=0;i<7;i++)
        cout<<setw(5)<<s[i];
    cout<<'\n';
    for(i=0;i<week;i++)
        cout<<setw(5)<<' ';
    for(i=1;i<=n;i++)
    {
        cout<<setw(5)<<i;
        k=(week+i)%7;
        if(!k)
            cout<<'\n';
    }
    cout<<'\n';
}
void PrintYear(unsigned y)
{
    int i;
    for(i=1;i<=12;i++)
        PrintMonth(y,i);
}
void CountDown(unsigned y1,unsigned m1,unsigned d1,Date &date3)
{
   unsigned y2,m2,d2;
   Loop:cout<<"请输入未来某一日期(按年、月、日) :";
   cin>>y2>>m2>>d2;
   Date date4(m2,d2,y2);
   if(y2<y1||(y2==y1&&m2<m1)||(y2==y1&&m2==m1&&d2<d1))
   goto Loop;
   cout<<"在两个日期间 "<<SwitchMonth(m1)<<" "<<d1<<" "<<y1<<" and "
       <<SwitchMonth(m2)<<" "<<d2<<" "<<y2<<" 相隔天数是 :"<<date3.Distance(date4)<<endl;
}

ostream& operator<<(ostream &out,const Date &date)
{
    out<<" "<<date.year<<"年"<<" "<<SwitchMonth(date.month)<<" "<<date.day<<"日"<<" "<<SwitchWeek(date.week)<<endl;
    return out;
}








搜索更多相关主题的帖子: void 日历 include public 
2012-06-20 09:27
Alar30
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:10
帖 子:988
专家分:1627
注 册:2009-9-8
收藏
得分:3 
来学习下哈。。
2012-06-20 11:32
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:3 
c++代码,不懂,你发到c++版吧

总有那身价贱的人给作业贴回复完整的代码
2012-06-20 11:55
wwc386646959
Rank: 2
等 级:论坛游民
帖 子:29
专家分:82
注 册:2012-6-16
收藏
得分:3 
你要c语言版的吗?第一个功能我用c语言写了,第二功能随便改改就行
2012-06-20 12:26
面包店的老板
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-3-26
收藏
得分:0 
回复 3楼 embed_xuel
能劳烦解释下么初学c++。。。。
2013-03-26 08:59
快速回复:日历制作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.049348 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved