标题:大家帮忙啊!!改改这个程序 刚学 不太清楚
只看楼主
yang133054
Rank: 1
等 级:新手上路
威 望:1
帖 子:57
专家分:0
注 册:2008-11-4
 问题点数:0 回复次数:4 
大家帮忙啊!!改改这个程序 刚学 不太清楚
要求:
1)    该系统主要处理时间的相关信息。
2)    完成以下的操作:日期的设置、显示、实现在某一个日期上(月、日、年)加减一个天数的功能、计算两个日期间隔天数
提示:
1)    建立日期类
2)    日期类的初始化
3)    按某个固定格式(如:yyyy-MM-dd)一次性的设置某个日期对象
4)    以固定格式显示某个日期
5)    在日期上加一天
6)    在日期上减一天
7)    输入两个日期,然后输出日期的间隔天数
#include<iostream.h>
#include<math.h>
class Date{
public:
    Date(int year,int month,int day)//建立日期类
    {
        year=0000;
        month=00;
        day=00;
    }
    int date_set(int year,int month,int day)
    {}


    int date_output()
    {
        cout<<year<<"--"<<month<<"--"<<day<<endl;
    }
    int date_plus(int day)
    {
        day++;//加上一天
    }
    int date_down(int day)
    {
        day--;//减去一天
    }
    int days_calculate()//计算两个日期间的天数
    {
    }
private:
    int year,month,day;
}
day_calculate(int year1,int year2,int month1,int month2,int day1,int day2)
{
    cout<<"Please input two dates";
    cin>>year1>>month1>>day1;
    cin>>year2>>month2>>day2;
    int days,month,year;
    switch(month){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:days=31;
        break;
    case 4:
    case 6:
    case 9:
    case 11:days=30;
        break;
    case 2:if(year%4==0&&year%100!=0)
               days=29;
        else days=28;

    }



    days=365*(year1-year2)+30*(month1-month2)+day1-day2;
    days=fabs(days);
    cout<<"the days are:"<<days<<endl;
}
main()
{
    Date date;
    date.showdate();
}













main()
{Date date;



}
搜索更多相关主题的帖子: 改改 
2008-11-24 22:37
hsxyhq
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-11-3
得分:0 
稍微改了改!。。。。你试试!
#include<iostream.h>
#include<math.h>
class Date
{
    public:
        Date()//
        {
            year=0000;
            month=00;
            day=00;
        }
        Date(int year_input,int month_input,int day_input)//
        {
            year=year_input;
            month=month_input;
            day=day_input;
        }
        void date_set(int year_input,int month_input,int day_input)//
        {
            year=year_input;
            month=month_input;
            day=day_input;
        }
        void date_output()
        {
            cout<<year<<"--"<<month<<"--"<<day<<endl;
        }
        void date_plus()
        {
            if(day+1<=get_days(month,year))
                day++;
            else
            {
                month_plus();
                day=1;
            }
        }
        void date_down()
        {
            if(day-1>=1)
                day--;
            else
            {
                month_down();
                day=get_days(month,year);
            }
        }
        void month_plus()
        {
            if(month+1<=12)
                month++;
            else
            {
                year++;
                month=1;
            }

        }
        void month_down()
        {
            if(month-1>=1)
                month--;
            else
            {
                year--;
                month=12;
            }
        }
        int days_calculate(int year_input,int month_input,int day_input)//计算两个日期间的天数
        {
            int total_days=0;
            int leapyears;
            int i;
            if(year_input==year)
            {
                for(i=1;i<month;i++)
                    total_days+=get_days(i,year);
                total_days+=day;
                for(i=1;i<month_input;i++)
                    total_days-=get_days(i,year_input);
                total_days-=day_input;
                return abs(total_days);
            }
            else if(year_input<year)
            {
                leapyears=0;
                for(i=year_input;i<year;i++)
                    if(is_leap(i))
                        leapyears++;
                total_days=(year-year_input)*365+leapyears;

                for(i=1;i<month;i++)
                    total_days+=get_days(i,year);
                total_days+=day;
                for(i=1;i<month_input;i++)
                    total_days-=get_days(i,year_input);
                total_days-=day_input;
                return abs(total_days);
            }
            else
            {
                leapyears=0;
                for(i=year;i<year_input;i++)
                    if(is_leap(i))
                        leapyears++;
                total_days=(year_input-year)*365+leapyears;

                for(i=1;i<month_input;i++)
                    total_days+=get_days(i,year_input);
                total_days+=day_input;
                for(i=1;i<month;i++)
                    total_days-=get_days(i,year);
                total_days-=day;
                return abs(total_days);
            }
        }
    private:
        int get_days(int month_,int year_)
        {
            switch(month_)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                    break;
                case 2:
                    if(is_leap(year_))
                        return 29;
                    else
                        return 28;
                    break;
                default:
                    return 0;
            }
        }
        bool is_leap(int year_)
        {
            if(year_%4)
                return false;
            else if(year_%100)
                return true;
            else if(year_%400)
                return false;
            return true;
        }
    private:
        int year,month,day;
};
void main()
{
    Date date(2008,11,26);
    date.date_output();
    date.date_plus();
    date.date_output();
    date.date_down();
    date.date_output();
    cout<<date.days_calculate(2000,10,30)<<endl;
}
收到的鲜花
  • yang1330542008-12-16 12:26 送鲜花  1朵   附言:很好
2008-11-26 23:56
zhangyafei4353
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-11-28
得分:0 
兄弟不才,改了一下上楼main()代码:
void main()
{
    int m_year,m_month,m_Day,result;
    char m_choice;
    Date date(2008,11,26);
    cout<<"    1.日期的设置'S';2.日期的显示'X';3.日期加一天'A';\n    4.日期减一天'J';5.日期算间隔'G';6.退出'Q';\n\n\n"<<endl;
    while(m_choice!='Q')
    {
        cout<<"请选择:"; cin>>m_choice;
        switch(m_choice)
        {
            case 'S':    cout<<"Y:"; cin>>m_year;
                        cout<<"M:"; cin>>m_month;
                        cout<<"D:"; cin>>m_Day;
                        date.date_set(m_year,m_month,m_Day);
                        break;
            case 'X':    break;
            case 'A':    date.date_plus();                
                        break;    
            case 'J':    date.date_down();                
                        break;
            case 'G':    cout<<"    目前时间:\n\n"<<endl;                
                        date.date_output();
                        cout<<"Y:"; cin>>m_year;
                        cout<<"M:"; cin>>m_month;
                        cout<<"D:"; cin>>m_Day;
                        result=date.days_calculate(m_year,m_month,m_Day);
                        cout<<"    间隔时间:"<<result<<endl;
                        break;                        
        }
        if(m_choice!='G')  date.date_output();
    }
}
2008-11-29 18:57
yang133054
Rank: 1
等 级:新手上路
威 望:1
帖 子:57
专家分:0
注 册:2008-11-4
得分:0 
谢谢了
2008-11-29 20:31
s1037460907
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-4-25
得分:0 
回复 2楼 hsxyhq
每一行的意思能解释一下吗  我们下周要答辩
2019-04-25 19:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-246069-1-1.html




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

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