标题:请大家帮我看看错误在哪!
只看楼主
dolphin371
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-3-14
结帖率:100%
已结贴  问题点数:20 回复次数:1 
请大家帮我看看错误在哪!
#include <iostream>
using namespace std;
class Date
{
public:  Date(int,int,int);
         void addMonth(int);
         void addYear(int);
         void addDay(int);
         void redMonth(int);
         void redYear(int);
         void redDay(int);
         void print();
private:
        int year,month,day;
};
Date::Date(int y,int m,int d)
{
    year=y;
    month=m;
    day=d;
}
void Date::redYear(int yy)
{
    year-=yy;
    if(!(year%4==0&&year%100!=0||year%400==0))
        if(month==2&&day==29)
            day=28;
}
void Date::redMonth(int mm)
{
    month-=mm;
    if(year%4==0&&year%100!=0||year%400==0)
    {
        if(month==2&&day>29)
        day=29;
    }
    else if(month==2&&day>28)
        day=28;
    if(month<1)
    {
        year=year-(-(month-13))/12;
        month=12-(-(month-1))%12;
    }
    if(month==4||month==6||month==9||month==11)
        if(day>30)
            day=30;
}
void Date::redDay(int dd)
{
    day-=dd;
    if(day<=0)
        month--;
    while(day<=0)
        {
        if(day>=0&&(++month)==13)
            month=0;
        if(year%4==0&&year%100!=0||year%400==0)
        {   
            if((++month)==2&&day>=0)
            day=29-day;
        }
        else if((++month)==2&&day>=0)
            day=28-day;
        if((++month)==1||(++month)==3||(++month)==5||(++month)==7||(++month)==8||(++month)==10||(++month)==12)
            if(day>=0)
            day=31-day;
        if((++month)==4||(++month)==6||(++month)==9||(++month)==11)
            if(day>=0)
            day=30-day;

     switch(month)
    {
case 12:
    if(day<0)
    {day+=31;month--;}
    else
        break;
case 11:
    if(day<0)
    {day+=30;month--;}
    else
        break;
case 10:
    if(day<0)
    {day+=31;month--;}
    else
        break;
case 9:
    if(day<0)
    {day+=30;month--;}
    else
        break;
case 8:  
    if(day<0)
    {day+=31;month--;}
    else
        break;
case 7:
    if(day<0)
    {day+=31;month--;}
    else
        break;
case 6:  
    if(day<0)
    {day+=30;month--;}
    else
        break;
case 5:
    if(day<0)
    {day+=31;month--;}
    else
        break;
case 4:
    if(day<0)
    {day+=30;month--;}
    else
        break;
case 3:
    if(day<0)
    {day+=31;month--;}
    else
        break;
case 2:
    if(year%4==0&&year%100!=0||year%400==0)
    {
        if(day<0)
        {day+=29;month--;}
        else
            break;
    }
    else if(day<0)
    {day+=28;month--;}
    else
        break;
default:
    if(day<0)
    {day+=31;month=12;year--;}
    }
        }
}
void Date::addDay(int dd)
{
    day+=dd;
    while(1)
        {
        if(year%4==0&&year%100!=0||year%400==0)
        {   
            if(month==2&&day<29)
                break;
        }
        else
        {
            if(month==2&&day<28)
                break;
        }
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        {
            if(day<31)
                break;
        }
        else
        {
            if(day<30)
                break;
        }
     switch(month)
    {
case 1:
    if(day>31)
    {day-=31;month++;}
    else
        break;
case 2:
    if(year%4==0&&year%100!=0||year%400==0)
    {if(day>29)
    {day-=29;month++;}
    else
        break;
    }
    else
    {if(day>28)
    {day-=28;month++;}
    else
        break;
    }
case 3:
    if(day>31)
    {day-=31;month++;}
    else
        break;
case 4:
    if(day>30)
    {day-=30;month++;}
    else
        break;
case 5:  
    if(day>31)
    {day-=31;month++;}
    else
        break;
case 6:
    if(day>30)
    {day-=30;month++;}
    else
        break;
case 7:  
    if(day>31)
    {day-=31;month++;}
    else
        break;
case 8:
    if(day>31)
    {day-=31;month++;}
    else
        break;
case 9:
    if(day>30)
    {day-=30;month++;}
    else
        break;
case 10:
    if(day>31)
    {day-=31;month++;}
    else
        break;
case 11:
    if(day>30)
    {day-=30;month++;}
    else
        break;
default:
    if(day>31)
    {day-=31;month=1;year++;}
    }
        }
}
void Date::addMonth(int mm)
{
    month+=mm;
    if(year%4==0&&year%100!=0||year%400==0)
    {
        if(month==2&&day>29)
        day=29;
    }
    else if(month==2&&day>28)
        day=28;
    if(month>12)
    {
        year=year+month/12;
        month%=12;
    }
    if(month==4||month==6||month==9||month==11)
        if(day>30)
            day=30;
}
void Date::addYear(int yy)
{
    year+=yy;
    if(!(year%4==0&&year%100!=0||year%400==0))
        if(month==2&&day==29)
            day=28;
}
void Date::print()
{
    cout<<year<<"-"<<month<<"-"<<day<<endl;
}
int main()
{
    Date date1(2012,1,10);
    date1.redDay(20);
    date1.print();
    return 0;
}最后输出不是我想要的数据。。。
搜索更多相关主题的帖子: private void include public 
2012-03-15 22:20
榴紫丫
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:33
专家分:135
注 册:2011-11-3
得分:20 
这个不是正确的吗?你单步调试一下呗,跟踪一下,就知道了!
month数据变化1 0 1 3 4 3
day 数据变化 -10 20
2012-03-15 22:55



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




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

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