feladat4 done, needs unit tests

This commit is contained in:
Damimani 2026-10-29 18:11:21 +01:00
parent c0886a6cc7
commit bc87732585
7 changed files with 302 additions and 138 deletions

View file

@ -1,81 +1,40 @@
//Tóth Dániel Mátyás | IFPQYY | V1
//Tóth Dániel Mátyás | IFPQYY | V1
#include <iostream>
#include "cmydateexception.h"
#include "cmydate.h"
#define TIMESTAMP_YEAR 1970
using namespace std;
static bool IsLeapYear(int year) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
{
return true;
}
return false;
}
static bool IsValidYear(int year) {
if (year < 1970)
{
throw new CMyDateException(CMyDateException::ErrMinYear);
}
return true;
}
static bool IsValidMonth(int month) {
if (month < 1)
{
throw new CMyDateException(CMyDateException::ErrMinMonth);
}
if (month > 12)
{
throw new CMyDateException(CMyDateException::ErrMaxMonth);
}
return true;
}
static bool IsValidDay(int year, int month, int day) {
if (day < 1)
{
throw new CMyDateException(CMyDateException::ErrMinDay);
}
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day > 31)
{
throw new CMyDateException(CMyDateException::ErrMaxDay);
}
break;
case 4:
case 6:
case 9:
case 11:
if (day > 30)
{
throw new CMyDateException(CMyDateException::ErrMaxDay);
}
break;
case 2:
if (day > 29 || (day > 28 && !IsLeapYear(year)))
{
throw new CMyDateException(CMyDateException::ErrMaxDay);
}
break;
default:
throw new CMyDateException(CMyDateException::ErrUnexpected);
break;
}
}
static char monthnames[12][11] = {
"Január",
"Február",
"Március",
"Április",
"Május",
"Június",
"Július",
"Augusztus",
"Szeptember",
"Október",
"November",
"December"
};
static bool IsLeapYear(int year);
static int GetMonthDayCount(int month, int year);
static bool IsValidYear(int year);
static bool IsValidMonth(int month);
static bool IsValidDay(int year, int month, int day);
static int DateToTimeStamp(int year, int month, int day);
static void TimeStampToDate(int timestamp, int* year, int* month, int* day);
static int DayOfTheYearArgs(int year, int month, int day);
CMyDate::CMyDate() {
m_iYear = 1970;
m_iMonth = 1;
m_iDay = 1;
SetYear(1970);
SetMonth(1);
SetDay(1);
}
CMyDate::CMyDate(int year, int month, int day) {
IsValid(year, month, day);
SetYear(year);
SetMonth(month);
SetDay(day);
@ -84,75 +43,53 @@ CMyDate::~CMyDate(){
//hey jimmy, get me a destructor with nothing
}
void CMyDate::Display() const {
char monthName[11] = {};
switch (m_iMonth)
{
case 1:
strncpy(monthName, "Január", 7);
break;
case 2:
strncpy(monthName, "Február", 8);
break;
case 3:
strncpy(monthName, "Március", 8);
break;
case 4:
strncpy(monthName, "Április", 9);
break;
case 5:
strncpy(monthName, "Május", 6);
break;
case 6:
strncpy(monthName, "Június", 7);
break;
case 7:
strncpy(monthName, "Július", 7);
break;
case 8:
strncpy(monthName, "Augusztus", 10);
break;
case 9:
strncpy(monthName, "Szeptember", 11);
break;
case 10:
strncpy(monthName, "Október", 8);
break;
case 11:
strncpy(monthName, "November", 9);
break;
case 12:
strncpy(monthName, "December", 9);
break;
default:
throw new CMyDateException(CMyDateException::ErrUnexpected);
break;
}
cout << m_iYear << " " << monthName << " " << m_iDay << ".";
cout << GetYear() << " " << monthnames[GetMonth() - 1] << " " << GetDay() << ".";
}
int CMyDate::DayOfTheYear() const {
int d = 30.4375 * (m_iMonth - 1); //avg day in a month
return d + m_iDay - (int)IsLeapYear(m_iYear);
return DayOfTheYearArgs(GetYear(), GetMonth(), GetDay());
}
bool CMyDate::IsValid(int year, int month, int day) const {
try
{
IsValidYear(year);
IsValidMonth(month);
IsValidDay(year, month, day);
}
catch (const CMyDateException& e)
{
}
IsValidYear(year);
IsValidMonth(month);
IsValidDay(year, month, day);
return true;
}
void CMyDate::AddDays(int days) {
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;
}
int CMyDate::SubDays(int days) {
void CMyDate::SubDays(int days) {
AddDays(days * (-1));
}
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;
}
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 CMyDate::GetDay() const {
return m_iDay;
@ -181,3 +118,117 @@ void CMyDate::SetYear(int year) {
m_iYear = year;
}
}
static bool IsLeapYear(int year) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
{
return true;
}
return false;
}
static int GetMonthDayCount(int month, int year) {
IsValidMonth(month);
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 (IsLeapYear(year))
{
return 29;
}
else
{
return 28;
}
break;
default:
throw new CMyDateException(CMyDateException::ErrUnexpected);
break;
}
}
static bool IsValidYear(int year) {
if (year < 1970)
{
throw new CMyDateException(CMyDateException::ErrMinYear);
}
return true;
}
static bool IsValidMonth(int month) {
if (month < 1)
{
throw new CMyDateException(CMyDateException::ErrMinMonth);
}
if (month > 12)
{
throw new CMyDateException(CMyDateException::ErrMaxMonth);
}
return true;
}
static bool IsValidDay(int year, int month, int day) {
int monthday = GetMonthDayCount(month, year);
if (day < 1)
{
throw new CMyDateException(CMyDateException::ErrMinDay);
}
if (day > monthday)
{
throw new CMyDateException(CMyDateException::ErrMaxDay);
}
return true;
}
static int DateToTimeStamp(int year, int month, int day) { //counting from 1970-01-01
int y = year - 1;
int e = TIMESTAMP_YEAR - 1;
int days = 365 * (year - TIMESTAMP_YEAR)
+ (y / 4 - e / 4)
- (y / 100 - e / 100)
+ (y / 400 - e / 400);
return days + DayOfTheYearArgs(year, month, day - 1);
}
static void TimeStampToDate(int timestamp, int* year, int* month, int* day) {
*year = TIMESTAMP_YEAR;
while (timestamp < 0)
{
(*year)--;
timestamp += IsLeapYear(*year) ? 366 : 365;
}
while (timestamp >= (IsLeapYear(*year) ? 366 : 365))
{
timestamp -= IsLeapYear(*year) ? 366 : 365;
(*year)++;
}
*month = 1;
int monthDay = GetMonthDayCount(*month, *year);
while (timestamp >= monthDay)
{
timestamp -= monthDay;
(*month)++;
monthDay = GetMonthDayCount(*month, *year);
}
*day = timestamp + 1;
}
static int DayOfTheYearArgs(int year, int month, int day) {
for (size_t i = 1; i < month; i++)
{
day += GetMonthDayCount(i, year);
}
return day;
}

View file

@ -1,4 +1,4 @@
//Tóth Dániel Mátyás | IFPQYY | V1
//Tóth Dániel Mátyás | IFPQYY | V1
#pragma once
class CMyDate {
public:
@ -9,7 +9,7 @@ public:
int DayOfTheYear() const;
bool IsValid(int year, int month, int day) const;
void AddDays(int days);
int SubDays(int days);
void SubDays(int days);
int DifferenceInDays(const CMyDate& other) const;
void AddMonths(int months);
int GetDay() const;

View file

@ -1,8 +1,30 @@
//Tóth Dániel Mátyás | IFPQYY | V1
//Tóth Dániel Mátyás | IFPQYY | V1
#include "cmydateexception.h"
#include <iostream>
static int CopyStr(char* dest, const char* src, const char* concat = "\0") {
if (dest == nullptr || src == nullptr) {
return -1;
}
int i = 0;
while (src[i] != '\0')
{
dest[i++] = src[i];
}
int j = 0;
while (concat[j] != '\0')
{
dest[i++] = concat[j++];
}
dest[i] = '\0';
return -1;
}
CMyDateException::CMyDateException(const char* s)
{
strncpy(msg, s, sizeof(s));
CopyStr(msg, s);
}
const char* CMyDateException::what() {
return msg;
}

View file

@ -1,14 +1,15 @@
//Tóth Dániel Mátyás | IFPQYY | V1
//Tóth Dániel Mátyás | IFPQYY | V1
#pragma once
class CMyDateException {
public:
CMyDateException(const char* s);
const char* what(void);
static constexpr const char* ErrMinDay = "Hiba: nap kisebb, mint 1!";
static constexpr const char* ErrMaxDay = "Hiba: nap nagyobb, mint az adott hónap napszáma!";
static constexpr const char* ErrMinMonth = "Hiba: hónap kisebb, mint 1!";
static constexpr const char* ErrMaxMonth = "Hiba: hónap nagyobb, mint 12!";
static constexpr const char* ErrMinYear = "Hiba: év kisebb, mint 1970!";
static constexpr const char* ErrUnexpected = "Váratlan hiba";
static constexpr const char* ErrMaxDay = "Hiba: nap nagyobb, mint az adott honap napszama!";
static constexpr const char* ErrMinMonth = "Hiba: honap kisebb, mint 1!";
static constexpr const char* ErrMaxMonth = "Hiba: honap nagyobb, mint 12!";
static constexpr const char* ErrMinYear = "Hiba: ev kisebb, mint 1970!";
static constexpr const char* ErrUnexpected = "Varatlan hiba";
private:
char* msg;
};

View file

@ -111,6 +111,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View file

@ -1,6 +1,95 @@
//Tóth Dániel Mátyás | IFPQYY | V1
#include "feladat4.h"
#include "cmydate.h"
using namespace std;
/*#define TIMESTAMP_YEAR 1970
static bool IsLeapYear(int year) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
{
return true;
}
return false;
}
static int GetMonthDayCount(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 (IsLeapYear(year))
{
return 29;
}
else
{
return 28;
}
break;
default:
throw new CMyDateException(CMyDateException::ErrUnexpected);
break;
}
}
static int DayOfTheYearArgs(int year, int month, int day) {
for (int i = 1; i < month; i++)
{
day += GetMonthDayCount(i, year);
}
return day;
}
static int DateToTimeStamp(int year, int month, int day) { //counting from 1970-01-01
int y = year - 1;
int e = TIMESTAMP_YEAR - 1;
int days = 365 * (year - TIMESTAMP_YEAR)
+ (y / 4 - e / 4)
- (y / 100 - e / 100)
+ (y / 400 - e / 400);
return days + DayOfTheYearArgs(year, month, day - 1);
}
static void TimeStampToDate(int timestamp, int* year, int* month, int* day) {
*year = TIMESTAMP_YEAR;
while (timestamp < 0)
{
(*year)--;
timestamp += IsLeapYear(*year) ? 366 : 365;
}
while (timestamp >= (IsLeapYear(*year) ? 366 : 365))
{
timestamp -= IsLeapYear(*year) ? 366 : 365;
(*year)++;
}
*month = 1;
int monthDay = GetMonthDayCount(*month, *year);
while (timestamp >= monthDay)
{
timestamp -= monthDay;
(*month)++;
monthDay = GetMonthDayCount(*month, *year);
}
*day = timestamp + 1;
}*/
int main() {
/*int t = DateToTimeStamp(1976, 10, 23);
cout << t << "\n";
int y = 0, m = 0, d = 0;
TimeStampToDate(t, &y, &m, &d);
cout << y << ". " << m << ". " << d << ".";*/
return 0;
}