C ++日期

时间:2019-02-18 10:20:47

标签: c++ date

我正在尝试编写一个类,在该类中我想输入格式为“ 03.02.2019”的Date和一个整数(如4),我想要的代码是结果:“ 07.02.2019”。我认为这里的问题是我没有正确分配指针,因为我可以看到代码在做正确的事情。它确实会计算“ 07.02.2019”,但我无法使结果还给我;-)。请在下面找到代码:

#include <iostream>
#include "Calendar.h"
#include <ctime>
using namespace std;


int main()
{
    Calendar MyDate("03.02.2019");
    Calendar NewDate;
    NewDate = MyDate + 4;
    cout << endl << NewDate.print() << endl;
    cin.get();
    getchar();
    return 0;
}

Calender.h:

#pragma once
#include <iostream>
#include <vector>
#include <ctime>
#include <string>

using namespace std;

class Calendar
{

public:
    Calendar();
    Calendar(string thedate);
    /*
    Calendar operator-(const Calendar &TheDate);
    Calendar operator-(const int &numberOfDays);
    */
    Calendar operator+(const int &numberOfDays);
    string print(void);
    void set_Date(string dertag);
    void set_Seconds();
    ~Calendar();

private:
    vector<string>AllTheDates;
    string TheDate;
    int TheYear;
    int TheMonth;
    int TheDay;
    unsigned int YearSeconds;
    unsigned int MonthSeconds;
    unsigned int DaySeconds;
    unsigned int TotalSeconds;
    string *theNewDate2print_ptr=new string;
    //string theNewDate2print;
    bool isdate;
};

Calendar.cpp:

#include "Calendar.h"
#include <ctime>
#include <string>
#include <sstream>
using namespace std;
Calendar::Calender()
{

}
Calendar::Calendar(string thedate)
{
    this->set_Date(thedate);
}

/*
Calendar Calendar::operator-(const Calendar &TheDate)
{

}

Calendar Calendar::operator-(const int &numberOfDays)
{

}
}*/

Calendar Calendar::operator+(const int &numberOfDays)
{
    //Calendar NewDate;
    unsigned int TotalSeconds_ = TotalSeconds + (numberOfDays * 24 * 60 * 60);
    time_t timer = TotalSeconds_;
    tm *ltm = localtime(&timer);
    stringstream NewDate_;
    string TheNewDate;
    stringstream theDay;
    stringstream theMonth;
    stringstream theYear;
    string theDay_;
    string theMonth_;
    string theYear_;
    //theNewDate2print_ptr = new string;

    theDay << ltm->tm_mday;
    theMonth << 1+ltm->tm_mon;
    theYear << 1830+ltm->tm_year;

    theDay_ = theDay.str();
    theMonth_ = theMonth.str();
    theYear_ = theYear.str();

    if (theDay_.length() == 1)
        theDay_ = "0" + theDay_;

    if (theMonth_.length() == 1)
        theMonth_ = "0" + theMonth_;

    //NewDate_ << ltm->tm_mday << "." << 1+ltm->tm_mon << "." << 1830+ltm->tm_year;
    TheNewDate = theDay_ + "." + theMonth_ + "." + theYear_;
    //Calendar NewDate(TheNewDate);
    *theNewDate2print_ptr = TheNewDate;
    return TheNewDate;
}

void Calendar::set_Date(string dertag)  
{
    TheDate = dertag;
    TheDay = stoi(dertag.substr(0, 2));
    TheMonth = stoi(dertag.substr(3,2));
    TheYear = stoi(dertag.substr(6,4));
    if (TheDay > 0 && TheDay < 32 && TheMonth>0 && TheMonth < 13 && TheYear>1900 && TheYear < 3000)
        isdate = true;
    /*if(isdate)
    throw exception!*/
    set_Seconds();
}

void Calendar::set_Seconds()
{
    //Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde. 
    YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
    MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
    DaySeconds = TheDay * 24 * 60 * 60;
    TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
}

string Calendar::print()
{
    return (*theNewDate2print_ptr);
}
Calendar::~Calendar()
{

}

感谢您的帮助。非常感谢!

关于, 马可

1 个答案:

答案 0 :(得分:1)

Calendar Calendar::operator+(const int &numberOfDays)
{
    ...
    string TheNewDate;
    ...
    return TheNewDate;
}

这将从Calendar字符串构造TheNewDate对象,这将调用构造函数:

Calendar::Calendar(string thedate)
{
    this->set_Date(thedate);
}

set_Date

void Calendar::set_Date(string dertag)  
{
    TheDate = dertag;
    TheDay = stoi(dertag.substr(0, 2));
    TheMonth = stoi(dertag.substr(3,2));
    TheYear = stoi(dertag.substr(6,4));
    if (TheDay > 0 && TheDay < 32 && TheMonth>0 && TheMonth < 13 && TheYear>1900 && TheYear < 3000)
        isdate = true;
    /*if(isdate)
    throw exception!*/
    set_Seconds();
}

set_Seconds()

void Calendar::set_Seconds()
{
    //Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde. 
    YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
    MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
    DaySeconds = TheDay * 24 * 60 * 60;
    TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
}

在构造期间,*theNewDate2print_ptr没有分配任何值,该字符串为空-默认在构造函数int new string调用之前初始化它。您需要在任何地方分配它。例如在set_Seconds中:

void Calendar::set_Seconds()
{
    //Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde. 
    YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
    MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
    DaySeconds = TheDay * 24 * 60 * 60;
    TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
    *theNewDate2print_ptr = std::to_string(DaySeconds) + "-" + ....;
}

您的课程泄漏了内存。它分配新对象,并且不会在任何地方释放它。另外,还应编写复制构造函数以防止内存泄漏。您应该释放分配的内存:

Calendar::~Calendar()
{
    delete theNewDate2print_ptr;
}

成员:

    vector<string>AllTheDates;
    string TheDate;
    int TheYear;
    int TheMonth;
    int TheDay;
    unsigned int YearSeconds;
    unsigned int MonthSeconds;
    unsigned int DaySeconds;
    unsigned int TotalSeconds;
    string *theNewDate2print_ptr=new string;
    //string theNewDate2print;
    bool isdate;

看起来像许多重复的变量,它们以不同的格式保存相同的值。干净点不要重复变量。不要浪费记忆。不要混淆自己。一次存储一个变量。不需要YearSecondsTheYear。如果将日期存储为整数,则无需TheDate。编写转换函数,setter和getter函数:

    unsigned int TheYear;
    unsigned int YearSeconds() { 
            // TODO: leap years
            return TheYear * number_of_seconds_in_a_year;
    }
    void SetTheYearFromSeconds(unsigned int seconds) {
            // TODO: leap years
            TheYear = seconds / number_of_seconds_in_a_year;
    }
    // etc...
    unsigned int TotalSeconds() { 
            return YearSeconds() + .... ;
    }