From 41988f0df69adc0826ec0a91b8897be0fc881b02 Mon Sep 17 00:00:00 2001 From: Damimani Date: Sun, 1 Nov 2026 18:30:58 +0100 Subject: [PATCH] feladat4v2 --- feladat4/UnitTest1/UnitTest1.cpp | 274 ++++++++++++++++++------ feladat4/feladat4/IFPQYY_feladat4V2.zip | Bin 0 -> 4582 bytes feladat4/feladat4/UnitTest1.cpp | 244 +++++++++++++++++++++ feladat4/feladat4/cmydate.cpp | 94 ++++---- feladat4/feladat4/cmydate.h | 2 +- feladat4/feladat4/cmydateexception.cpp | 2 +- feladat4/feladat4/cmydateexception.h | 2 +- feladat4/feladat4/main.cpp | 2 +- 8 files changed, 507 insertions(+), 113 deletions(-) create mode 100644 feladat4/feladat4/IFPQYY_feladat4V2.zip create mode 100644 feladat4/feladat4/UnitTest1.cpp diff --git a/feladat4/UnitTest1/UnitTest1.cpp b/feladat4/UnitTest1/UnitTest1.cpp index 5b2a5bc..afec52c 100644 --- a/feladat4/UnitTest1/UnitTest1.cpp +++ b/feladat4/UnitTest1/UnitTest1.cpp @@ -1,9 +1,12 @@ +//Tóth Dániel Mátyás | IFPQYY | V2 #include "pch.h" #include "CppUnitTest.h" +#include #include #include using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace std; namespace UnitTest1 { @@ -11,86 +14,231 @@ namespace UnitTest1 { public: - - TEST_METHOD(ValidDate) + TEST_METHOD(ValidDates) { CMyDate date; - Assert::AreEqual(true, date.IsValid(1980, 4, 23)); - Assert::AreEqual(true, date.IsValid(1970, 1, 31)); - Assert::AreEqual(true, date.IsValid(1970, 2, 28)); - Assert::AreEqual(true, date.IsValid(1970, 9, 30)); - Assert::AreEqual(true, date.IsValid(1972, 2, 29)); + int dates[][3] = { + { 1980, 4, 23 }, + { 1970, 1, 31 }, + { 1970, 9, 30 }, + { 1970, 2, 28 }, + { 1972, 2, 29 }, + { 2000, 2, 29 }, + }; + int testcount = sizeof(dates) / 12; + for (size_t i = 0; i < testcount; i++) + { + Assert::AreEqual(true, date.IsValid(dates[i][0], dates[i][1], dates[i][2])); + } } TEST_METHOD(InvalidDates) { CMyDate date; - - try + int dates[][3] = { + { 1969, 1, 1 }, + { 1970, 0, 1 }, + { 1970, 13, 1 }, + { 1970, 1, 0 }, + { 1970, 1, 32 }, + { 1970, 9, 31 }, + { 1970, 2, 29 }, + { 2100, 2, 29 }, + }; + const char* errors[] = { + CMyDateException::ErrMinYear, + CMyDateException::ErrMinMonth, + CMyDateException::ErrMaxMonth, + CMyDateException::ErrMinDay, + CMyDateException::ErrMaxDay, + CMyDateException::ErrMaxDay, + CMyDateException::ErrMaxDay, + CMyDateException::ErrMaxDay, + }; + int testcount = sizeof(dates) / 12; + for (size_t i = 0; i < testcount; i++) { - date.IsValid(1969, 1, 1); - Assert::IsTrue(false); + Assert::IsFalse(date.IsValid(dates[i][0], dates[i][1], dates[i][2])); } - catch (const CMyDateException &e) + } + TEST_METHOD(DayOfTheYearTests) + { + CMyDate date; + int dates[][3] = { + { 1970, 1, 1 }, + { 1970, 2, 3 }, + { 1970, 3, 1 }, //non-leap year + { 1972, 3, 1 }, //leap year + }; + int counts[] = { + 1, + 34, + 60, + 61, + }; + int testcount = sizeof(dates) / 12; + for (size_t i = 0; i < testcount; i++) { - Assert::AreEqual(CMyDateException::ErrMinYear, e.what()); + date = CMyDate(dates[i][0], dates[i][1], dates[i][2]); + Assert::AreEqual(counts[i], date.DayOfTheYear()); } - try + } + + TEST_METHOD(AddDayTests) + { + CMyDate date1; + CMyDate date2; + int dates1[][3] = { + { 1970, 1, 1 }, + { 1970, 1, 1 }, + { 1970, 2, 1 }, + { 1972, 2, 1 }, + { 1970, 1, 1 }, + { 1972, 1, 1 }, + { 1970, 1, 2 }, + { 1970, 1, 1 }, + }; + int dates2[][3] = { //expected + { 1970, 1, 2 }, + { 1970, 2, 1 }, + { 1970, 3, 1 }, + { 1972, 2, 29 }, + { 1971, 1, 1 }, + { 1972, 12, 31 }, + { 1970, 1, 1 }, + { 1970, 1, 1 }, + }; + int counts[] = { + 1, + 31, + 28, + 28, + 365, + 365, + -1, //technically allowed + - 1 //since SubDays calls AddDays, this counts as its test + }; + int testcount = sizeof(dates1) / 12; + for (size_t i = 0; i < testcount; i++) { - date = CMyDate(1970, 0, 1); + try + { + CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]); + CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]); + date1.AddDays(counts[i]); + Assert::AreEqual(date2.GetYear(), date1.GetYear()); + Assert::AreEqual(date2.GetMonth(), date1.GetMonth()); + Assert::AreEqual(date2.GetDay(), date1.GetDay()); + } + catch (const CMyDateException& e) + { + Assert::AreEqual(CMyDateException::ErrMinYear, e.what()); //should only happen in the last case + } + catch (...) { + Assert::IsTrue(false); + } } - catch (CMyDateException e) + } + + TEST_METHOD(DifferenceInDaysTests) + { + CMyDate date1; + CMyDate date2; + int diff = 0; + + int dates1[][3] = { + { 1970, 1, 1 }, + { 1970, 1, 1 }, + { 1972, 1, 1 }, + { 1970, 1, 1 }, + { 1972, 1, 1 }, + { 1970, 1, 1 }, + }; + int dates2[][3] = { + { 1970, 1, 1 }, + { 1970, 3, 1 }, + { 1972, 3, 1 }, + { 1971, 1, 1 }, + { 1973, 1, 1 }, + { 1974, 1, 1 }, + }; + int counts[] = { + 0, + -59, + -60, + -365, + -366, + -1461 + }; + int testcount = sizeof(dates1)/12; + for (size_t i = 0; i < testcount; i++) { - Assert::AreEqual(CMyDateException::ErrMinMonth, e.what()); + CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]); + CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]); + diff = date1.DifferenceInDays(date2); + Assert::AreEqual(counts[i], diff); + diff = date2.DifferenceInDays(date1); //should be different in reverse ig + Assert::AreEqual(counts[i] * (-1), diff); } - try + } + + TEST_METHOD(AddMonthTests) + { + CMyDate date1; + CMyDate date2; + int dates1[][3] = { + { 1970, 1, 1 }, + { 1970, 1, 1 }, + { 1970, 1, 10 }, + { 1970, 12, 1 }, + { 1970, 1, 30 }, + { 1970, 1, 1 }, + }; + int dates2[][3] = { + { 1970, 2, 1 }, + { 1970, 3, 1 }, + { 1970, 2, 10 }, + { 1971, 1, 1 }, + { 1970, 1, 30 }, + { 1971, 1, 1 }, + }; + int counts[] = { + 1, + 2, + 1, + 1, + 1, + 12, + }; + int testcount = sizeof(dates1) / 12; + for (size_t i = 0; i < testcount; i++) { - date = CMyDate(1970, 13, 1); - } - catch (CMyDateException e) - { - Assert::AreEqual(CMyDateException::ErrMaxMonth, e.what()); - } - try - { - date = CMyDate(1970, 1, 0); - } - catch (CMyDateException e) - { - Assert::AreEqual(CMyDateException::ErrMinDay, e.what()); - } - try - { - date = CMyDate(1970, 1, 32); - } - catch (CMyDateException e) - { - Assert::AreEqual(CMyDateException::ErrMaxDay, e.what()); - } - try - { - date = CMyDate(1970, 9, 31); - } - catch (CMyDateException e) - { - Assert::AreEqual(CMyDateException::ErrMaxDay, e.what()); - } - try - { - date = CMyDate(1970, 4, 29); - } - catch (CMyDateException e) - { - Assert::AreEqual(CMyDateException::ErrMaxDay, e.what()); - } - try - { - date = CMyDate(2000, 4, 29); - } - catch (CMyDateException e) - { - Assert::AreEqual(CMyDateException::ErrMaxDay, e.what()); + CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]); + CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]); + try + { + date1.AddMonths(counts[i]); + } + catch (const CMyDateException& e) + { + Assert::AreEqual(CMyDateException::ErrMaxDay, e.what()); + } + Assert::AreEqual(date2.GetYear(), date1.GetYear()); + Assert::AreEqual(date2.GetMonth(), date1.GetMonth()); + Assert::AreEqual(date2.GetDay(), date1.GetDay()); } } + TEST_METHOD(DisplayTests) { + CMyDate date; + + stringstream buff; + streambuf* prevcoutbuff = cout.rdbuf(buff.rdbuf()); + date = CMyDate(1970, 1, 1); + date.Display(); + + string d = buff.str(); + + Assert::IsTrue(d == "1970 januar 1."); + } }; } diff --git a/feladat4/feladat4/IFPQYY_feladat4V2.zip b/feladat4/feladat4/IFPQYY_feladat4V2.zip new file mode 100644 index 0000000000000000000000000000000000000000..6596df5b04e461ad8f055e579f9c6bbc0bc66ce7 GIT binary patch literal 4582 zcmai2cQjmU-yK~D8H{c)dKn_hRii{A7@de76TSD|Ye@8Nj2a42ojyB zBN#3CDS~&Sx!ac17EnHoz45po_ZUKXwUz|tq7O5hlIhOOxwC<6X-|c>ZQ+9{g$tSmm zvz2kn-;S2&V$5YOnTYvB;U}($MRi-#Xv4j>F6P7$B>l)yirm{^AE}6E`R-k)o5OgW zy^cEzCMC41w@!z>^WO%0xB}u2D@ga6?#(4oCDwaU^aw8`yv?Q(YzbbMOxe@T)GQ{B zXno#|fO8%kY2hQuybryxM8*Nta^OwJHmf%7#1_&$6=*m+jf89>cfx=nw5>7fIO^Cy zN~d`cEJ?A4mQPS#R&+U7d}6l4K5hPuW@VKoF_+b?sGHjH#xdMd>b%=;=_!Z)Z5zti z+EMT~$>F756;{Up$Ai@89Or!X&OfOGm(C%RbMrRz2n}FxKbham3+CpajKf8t>%ETR z!Tq`YI6g(u#2m1~4<;gfJpP@_%u=me#Ut-Hl3te1Jc4_x(jT;m&@gwJ6)AqE@pwu@ zCoXS}>g(_rqg-_|SXGb!%6`jCkjwU3&(!iA9At#4Um2=`}m-<3B zHpcMOFbs#BmY!!;h7_s4u#5JML`mgRXin8)&OD>QZmeC|Xqgy~v}WPB&#{0d)-?8) zO1MSKw``}`^5Swq?-rvKps{Zmxl*dCErxZr#cJz`TIlpsrWtL7js`jJaA3{@A6m8_0m067f&q3!F4uX&TTQ)28 z47rivOj#eRkLd{~lDA{T`izv^xTh^L5no(aE=K8lO*DrrYl>LQ1?MxKWIWLJ9o16d ziLMO0jjHWz;XJzDtd-{Y+yqmPpDQG!-8$EJg2CWDc^?oeHo;cJ@5mb^$8s*yp@UT4 z1u&b7iL4*LQ<%LomDKSm?ngC4+1XMJmyT30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|zROyU6O07gH9!o%%mFXjKA<0|;?A@M5s$mLGJe|G?EuR`zhYWw^f^zX^( zDl|3GU(o-^QCF)>ZmUH9Tb18)!__K5m&W&7TKh*QYOc>B|E +#include +#include + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace std; + +namespace UnitTest1 +{ + TEST_CLASS(UnitTest1) + { + public: + + TEST_METHOD(ValidDates) + { + CMyDate date; + int dates[][3] = { + { 1980, 4, 23 }, + { 1970, 1, 31 }, + { 1970, 9, 30 }, + { 1970, 2, 28 }, + { 1972, 2, 29 }, + { 2000, 2, 29 }, + }; + int testcount = sizeof(dates) / 12; + for (size_t i = 0; i < testcount; i++) + { + Assert::AreEqual(true, date.IsValid(dates[i][0], dates[i][1], dates[i][2])); + } + } + + TEST_METHOD(InvalidDates) + { + CMyDate date; + int dates[][3] = { + { 1969, 1, 1 }, + { 1970, 0, 1 }, + { 1970, 13, 1 }, + { 1970, 1, 0 }, + { 1970, 1, 32 }, + { 1970, 9, 31 }, + { 1970, 2, 29 }, + { 2100, 2, 29 }, + }; + const char* errors[] = { + CMyDateException::ErrMinYear, + CMyDateException::ErrMinMonth, + CMyDateException::ErrMaxMonth, + CMyDateException::ErrMinDay, + CMyDateException::ErrMaxDay, + CMyDateException::ErrMaxDay, + CMyDateException::ErrMaxDay, + CMyDateException::ErrMaxDay, + }; + int testcount = sizeof(dates) / 12; + for (size_t i = 0; i < testcount; i++) + { + Assert::IsFalse(date.IsValid(dates[i][0], dates[i][1], dates[i][2])); + } + } + TEST_METHOD(DayOfTheYearTests) + { + CMyDate date; + int dates[][3] = { + { 1970, 1, 1 }, + { 1970, 2, 3 }, + { 1970, 3, 1 }, //non-leap year + { 1972, 3, 1 }, //leap year + }; + int counts[] = { + 1, + 34, + 60, + 61, + }; + int testcount = sizeof(dates) / 12; + for (size_t i = 0; i < testcount; i++) + { + date = CMyDate(dates[i][0], dates[i][1], dates[i][2]); + Assert::AreEqual(counts[i], date.DayOfTheYear()); + } + } + + TEST_METHOD(AddDayTests) + { + CMyDate date1; + CMyDate date2; + int dates1[][3] = { + { 1970, 1, 1 }, + { 1970, 1, 1 }, + { 1970, 2, 1 }, + { 1972, 2, 1 }, + { 1970, 1, 1 }, + { 1972, 1, 1 }, + { 1970, 1, 2 }, + { 1970, 1, 1 }, + }; + int dates2[][3] = { //expected + { 1970, 1, 2 }, + { 1970, 2, 1 }, + { 1970, 3, 1 }, + { 1972, 2, 29 }, + { 1971, 1, 1 }, + { 1972, 12, 31 }, + { 1970, 1, 1 }, + { 1970, 1, 1 }, + }; + int counts[] = { + 1, + 31, + 28, + 28, + 365, + 365, + -1, //technically allowed + - 1 //since SubDays calls AddDays, this counts as its test + }; + int testcount = sizeof(dates1) / 12; + for (size_t i = 0; i < testcount; i++) + { + try + { + CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]); + CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]); + date1.AddDays(counts[i]); + Assert::AreEqual(date2.GetYear(), date1.GetYear()); + Assert::AreEqual(date2.GetMonth(), date1.GetMonth()); + Assert::AreEqual(date2.GetDay(), date1.GetDay()); + } + catch (const CMyDateException& e) + { + Assert::AreEqual(CMyDateException::ErrMinYear, e.what()); //should only happen in the last case + } + catch (...) { + Assert::IsTrue(false); + } + } + } + + TEST_METHOD(DifferenceInDaysTests) + { + CMyDate date1; + CMyDate date2; + int diff = 0; + + int dates1[][3] = { + { 1970, 1, 1 }, + { 1970, 1, 1 }, + { 1972, 1, 1 }, + { 1970, 1, 1 }, + { 1972, 1, 1 }, + { 1970, 1, 1 }, + }; + int dates2[][3] = { + { 1970, 1, 1 }, + { 1970, 3, 1 }, + { 1972, 3, 1 }, + { 1971, 1, 1 }, + { 1973, 1, 1 }, + { 1974, 1, 1 }, + }; + int counts[] = { + 0, + -59, + -60, + -365, + -366, + -1461 + }; + int testcount = sizeof(dates1)/12; + for (size_t i = 0; i < testcount; i++) + { + CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]); + CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]); + diff = date1.DifferenceInDays(date2); + Assert::AreEqual(counts[i], diff); + diff = date2.DifferenceInDays(date1); //should be different in reverse ig + Assert::AreEqual(counts[i] * (-1), diff); + } + } + + TEST_METHOD(AddMonthTests) + { + CMyDate date1; + CMyDate date2; + int dates1[][3] = { + { 1970, 1, 1 }, + { 1970, 1, 1 }, + { 1970, 1, 10 }, + { 1970, 12, 1 }, + { 1970, 1, 30 }, + { 1970, 1, 1 }, + }; + int dates2[][3] = { + { 1970, 2, 1 }, + { 1970, 3, 1 }, + { 1970, 2, 10 }, + { 1971, 1, 1 }, + { 1970, 1, 30 }, + { 1971, 1, 1 }, + }; + int counts[] = { + 1, + 2, + 1, + 1, + 1, + 12, + }; + int testcount = sizeof(dates1) / 12; + for (size_t i = 0; i < testcount; i++) + { + CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]); + CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]); + try + { + date1.AddMonths(counts[i]); + } + catch (const CMyDateException& e) + { + Assert::AreEqual(CMyDateException::ErrMaxDay, e.what()); + } + Assert::AreEqual(date2.GetYear(), date1.GetYear()); + Assert::AreEqual(date2.GetMonth(), date1.GetMonth()); + Assert::AreEqual(date2.GetDay(), date1.GetDay()); + } + } + TEST_METHOD(DisplayTests) { + CMyDate date; + + stringstream buff; + streambuf* prevcoutbuff = cout.rdbuf(buff.rdbuf()); + date = CMyDate(1970, 1, 1); + date.Display(); + + string d = buff.str(); + + Assert::IsTrue(d == "1970 januar 1."); + } + }; +} diff --git a/feladat4/feladat4/cmydate.cpp b/feladat4/feladat4/cmydate.cpp index 37c8727..11af7b3 100644 --- a/feladat4/feladat4/cmydate.cpp +++ b/feladat4/feladat4/cmydate.cpp @@ -1,4 +1,4 @@ -//Tóth Dániel Mátyás | IFPQYY | V1 +//Tóth Dániel Mátyás | IFPQYY | V2 #include #include "cmydateexception.h" #include "cmydate.h" @@ -6,18 +6,18 @@ using namespace std; static char monthnames[12][11] = { - "Január", - "Február", - "Március", - "Április", - "Május", - "Június", - "Július", - "Augusztus", - "Szeptember", - "Október", - "November", - "December" + "januar", + "februar", + "március", + "aprilis", + "majus", + "junius", + "julius", + "augusztus", + "szeptember", + "oktober", + "november", + "december" }; static bool IsLeapYear(int year); @@ -30,14 +30,15 @@ static void TimeStampToDate(int timestamp, int* year, int* month, int* day); static int DayOfTheYearArgs(int year, int month, int day); CMyDate::CMyDate() { - SetYear(1970); - SetMonth(1); - SetDay(1); + m_iYear = 1970; + m_iMonth = 1; + m_iDay = 1; } CMyDate::CMyDate(int year, int month, int day) { - SetYear(year); - SetMonth(month); - SetDay(day); + IsValid(year, month, day); + m_iYear = year; + m_iMonth = month; + m_iDay = day; } CMyDate::~CMyDate(){ //hey jimmy, get me a destructor with nothing @@ -49,19 +50,32 @@ int CMyDate::DayOfTheYear() const { return DayOfTheYearArgs(GetYear(), GetMonth(), GetDay()); } bool CMyDate::IsValid(int year, int month, int day) const { - IsValidYear(year); - IsValidMonth(month); - IsValidDay(year, month, day); + try + { + IsValidYear(year); + IsValidMonth(month); + IsValidDay(year, month, day); + } + catch (const CMyDateException&) + { + return false; + } return true; } void CMyDate::AddDays(int days) { + if (days == 0) + { + return; + } int timestamp = DateToTimeStamp(GetYear(), GetMonth(), GetDay()); int y = 0, m = 0, d = 0; TimeStampToDate(timestamp + days, &y, &m, &d); - IsValid(y, m, d); - m_iYear = y; - m_iMonth = m; - m_iDay = d; + if (IsValid(y, m, d)) + { + m_iYear = y; + m_iMonth = m; + m_iDay = d; + } } void CMyDate::SubDays(int days) { AddDays(days * (-1)); @@ -70,26 +84,14 @@ int CMyDate::DifferenceInDays(const CMyDate& other) const { int thistmstmp = DateToTimeStamp(GetYear(), GetMonth(), GetDay()); int othertmstmp = DateToTimeStamp(other.GetYear(), other.GetMonth(), other.GetDay()); int result = thistmstmp - othertmstmp; - return result < 0 ? result * (-1) : result; + return result; } void CMyDate::AddMonths(int months) { - m_iYear += (m_iMonth - 1 + months) / 12; - m_iMonth += ((m_iMonth - 1 + months) % 12) + 1; - try - { - IsValidDay(m_iYear, m_iMonth, m_iDay); - } - catch (CMyDateException e) - { - if (e.what() == CMyDateException::ErrMaxDay) - { - SetDay(GetMonthDayCount(m_iMonth, m_iYear)); - } - else - { - throw e; - } - } + int y = m_iYear + (m_iMonth - 1 + months) / 12; + int m = ((m_iMonth - 1 + months) % 12) + 1; + IsValidDay(y, m, m_iDay); + m_iYear = y; + m_iMonth = m; } int CMyDate::GetDay() const { return m_iDay; @@ -107,13 +109,13 @@ void CMyDate::SetDay(int day) { } } void CMyDate::SetMonth(int month) { - if (IsValidMonth(month)) + if (IsValidMonth(month) && IsValidDay(m_iYear, month, m_iDay)) { m_iMonth = month; } } void CMyDate::SetYear(int year) { - if (IsValidYear(year)) + if (IsValidYear(year) && IsValidDay(year, m_iMonth, m_iDay)) { m_iYear = year; } diff --git a/feladat4/feladat4/cmydate.h b/feladat4/feladat4/cmydate.h index ba7e6df..5f82d40 100644 --- a/feladat4/feladat4/cmydate.h +++ b/feladat4/feladat4/cmydate.h @@ -1,4 +1,4 @@ -//Tóth Dániel Mátyás | IFPQYY | V1 +//Tóth Dániel Mátyás | IFPQYY | V2 #pragma once class CMyDate { public: diff --git a/feladat4/feladat4/cmydateexception.cpp b/feladat4/feladat4/cmydateexception.cpp index c9f8874..1c3e0cd 100644 --- a/feladat4/feladat4/cmydateexception.cpp +++ b/feladat4/feladat4/cmydateexception.cpp @@ -1,4 +1,4 @@ -//Tóth Dániel Mátyás | IFPQYY | V1 +//Tóth Dániel Mátyás | IFPQYY | V2 #include "cmydateexception.h" static int CopyStr(char* dest, const char* src, const char* concat = "\0") { diff --git a/feladat4/feladat4/cmydateexception.h b/feladat4/feladat4/cmydateexception.h index c32e3c8..728d3e4 100644 --- a/feladat4/feladat4/cmydateexception.h +++ b/feladat4/feladat4/cmydateexception.h @@ -1,4 +1,4 @@ -//Tóth Dániel Mátyás | IFPQYY | V1 +//Tóth Dániel Mátyás | IFPQYY | V2 #pragma once class CMyDateException { public: diff --git a/feladat4/feladat4/main.cpp b/feladat4/feladat4/main.cpp index aea7482..efd24c0 100644 --- a/feladat4/feladat4/main.cpp +++ b/feladat4/feladat4/main.cpp @@ -1,4 +1,4 @@ -//Tóth Dániel Mátyás | IFPQYY | V1 +//Tóth Dániel Mátyás | IFPQYY | V2 #include #include #include "cmydateexception.h"