C ++ date Type class ..编译后一些函数运行良好,但另一个函数没有

时间:2016-03-09 10:04:12

标签: c++ visual-studio-2012

在下面是dateType的头文件。

我上课了。调试后它显示在领事窗口中: Consul Image

头文件:dateType.h

#ifndef dateType_H
#define dateType_H

class dateType
{
public:
    void setDate(int, int, int);
    void setMonth(int);
    void setDay(int);
    void setYear(int);

    void print() const;

    int numberOfDaysPassed();   

    int numberOfDaysLeft();

    void incrementDate(int nDays);

    int getMonth();
    int getDay();
    int getYear();

    int getDaysInMonth();

    bool isLeapYear();

    dateType(int = 1, int = 1, int = 1900);

private:
    int dMonth;
    int dDay;
    int dYear;
};

#endif

main()功能:

#include <iostream>
#include<time.h>
#include "dateType.h"

using namespace std;
int main ()
{
    cout <<" Program work with data type"<<endl;

    dateType myDate(1,1,1900);

    cout << "\n\n The current Date is : "<<endl;
    myDate.print();
    cout <<endl;

    cout <<"\n\n days in this year are :" <<endl;
    myDate.getDaysInMonth();
    cout <<endl;

    cout <<"\n\n Days passed in this year are :"<<endl;
    myDate.numberOfDaysPassed();
    cout <<endl;

    cout <<"\n\n Days remaining in this year are :"<<endl;
    myDate.numberOfDaysLeft();
    cout <<endl;



    myDate.incrementDate(7);
    cout <<"\n\n New date after adding days is "<<endl;
    cout <<endl;

    myDate.print();
    cout <<endl;

    system("pause");
    return 0;
}

实施文件.cpp:

#include <iostream>
#include "dateType.h"

using namespace std;

void dateType::setDate(int month,int day,int year)
{
    setYear(year);
    setMonth(month);
    setDay(day);
}

void dateType::setMonth(int month)
{
    if(month > 0 && month < 13)
        dMonth =month;
    else
        dMonth = 1;
}

void dateType::setDay(int day)
{
    int days[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
    if (isLeapYear())
        days[1]++;
    if(day >= 1 && day <=days [dMonth -1])
        dDay =day;
    else
        dDay =1;
}

void dateType::setYear(int year)
{

    if (((int)log10((double)year) + 1)!=4)
        dYear = 1990;
        else dYear = year;
}


int dateType::getDaysInMonth()
{
    int days[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
        if (isLeapYear())
        days[1]++;
        return days[ dYear -1 ];
}
int dateType::numberOfDaysPassed()
{
    int days[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
    int m, daysPassed;
        if (isLeapYear())
        days[1]++;
        m = dYear-1;
        daysPassed=0;
        while(m>0)
        {
            daysPassed+= days[m-1];
            m--;
        }
        daysPassed+= dDay;
        return daysPassed;
}
int dateType::numberOfDaysLeft()
{
    int daysRemaining=365;
    if (isLeapYear())
        daysRemaining++;
    daysRemaining-= numberOfDaysPassed();
    return daysRemaining;
}

void dateType::incrementDate(int nDays) 
{
    while ((nDays + dDay) > dateType::getDaysInMonth())
    {
        nDays -= dateType::getDaysInMonth();
        if (dMonth == 12)
        {
            dMonth = 0;
            dYear++;
        }   
        dMonth++;
    }
    dDay += nDays;
}


int dateType::getDay()
{
    return dDay;
}

int dateType::getMonth()
{
    return dMonth;
}
int dateType::getYear()
{
    return dYear;
}


void dateType::print() const
{
    cout <<"\n\t"<<dMonth<<"-"<<dDay<<"-"<<dYear;
}
dateType::dateType(int month, int day, int year)
{
    setDate(month,day,year);
}

bool dateType::isLeapYear()
{
    if (dYear % 400 == 0 || ( dYear % 100 !=0 && dYear % 4 ==0))
        return true;
    else
        return false;
}

我不确定计算我做错了这些事情: 今年的日子是: 今年过去的日子是: 今年剩余的天数是:

1 个答案:

答案 0 :(得分:0)

函数getDaysInMonthnumberOfDaysPassednumberOfDaysLeft不打印任何内容,只返回一个值。

您必须打印该值,例如std::cout << myDate.getDaysInMonth() << '\n';

<小时/> 您打印days in this year are :,但您调用的函数是getDaysInMonth。我不知道你需要哪一个,但逻辑是错误的。

  • 如果您需要一年中的日期,那么您只需要返回365366,具体取决于是否为闰年。

  • 如果你需要这个月的日子,你应该减少dMonth而不是dYear,因为你想要月份,而不是年份(因为你现在使用的是年份索引,您正在访问您不拥有的内存,这将导致访问冲突异常或某些随机值。)

函数numberOfDaysPassed计算m如下m = dYear - 1;,但同样,您应该使用dMonth。您还应该考虑使用for循环而不是while

for (int i = dMonth - 1; i > 0; --i)
    daysPassed += days[i - 1];