Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.1 kB
2
Indexable
Never
#ifndef MYDATE_H
#define MYDATE_H

#include <string>

class MyDate {
private:
    int day;
    int month;
    int year;

    static const std::string monthNames[12];
    static const std::string dayNames[7];
    static int daysInMonth[12];

public:
    MyDate();
    MyDate(int dd, int mm, int yy);

    // Getters and setters
    int getDay() const;
    void setDay(int day);
    
    int getMonth() const;
    void setMonth(int month);
    
    int getYear() const;
    void setYear(int year);

    // Methods to display date information
    void displayDate() const;
    void displayMonthName() const;
    void displayDayOfWeek() const;

    // Static method to calculate day of the week
    static int calculateDayOfWeek(int dd, int mm, int yy);
};

#endif
//////////////////////////
#include "MyDate.h"
#include <iostream>

// Static member initialization
const std::string MyDate::monthNames[12] = {
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

const std::string MyDate::dayNames[7] = {
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};

int MyDate::daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// Default constructor
MyDate::MyDate() : day(1), month(1), year(2000) {}

// Parameterized constructor
MyDate::MyDate(int dd, int mm, int yy) : day(dd), month(mm), year(yy) {}

// Getters and setters
int MyDate::getDay() const {
    return day;
}

void MyDate::setDay(int day) {
    this->day = day;
}

int MyDate::getMonth() const {
    return month;
}

void MyDate::setMonth(int month) {
    this->month = month;
}

int MyDate::getYear() const {
    return year;
}

void MyDate::setYear(int year) {
    this->year = year;
}

// Method to display date in dd/mm/yyyy format
void MyDate::displayDate() const {
    std::cout << (day < 10 ? "0" : "") << day << "/"
              << (month < 10 ? "0" : "") << month << "/"
              << year << std::endl;
}

// Method to display month name
void MyDate::displayMonthName() const {
    if (month >= 1 && month <= 12) {
        std::cout << monthNames[month - 1] << std::endl;
    } else {
        std::cout << "Invalid month" << std::endl;
    }
}

// Method to display day of the week
void MyDate::displayDayOfWeek() const {
    int dayOfWeek = calculateDayOfWeek(day, month, year);
    std::cout << dayNames[dayOfWeek] << std::endl;
}

// Static method to calculate day of the week using Zeller's Congruence
int MyDate::calculateDayOfWeek(int dd, int mm, int yy) {
    if (mm < 3) {
        mm += 12;
        yy -= 1;
    }
    int k = yy % 100;
    int j = yy / 100;
    int h = (dd + 13*(mm + 1)/5 + k + k/4 + j/4 + 5*j) % 7;
    return (h + 5) % 7; // Adjusting for the different indexing of Zeller's Congruence
}
///////////////////////////////
#include "MyDate.h"
#include <iostream>

// Static member initialization
const std::string MyDate::monthNames[12] = {
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

const std::string MyDate::dayNames[7] = {
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};

int MyDate::daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// Default constructor
MyDate::MyDate() : day(1), month(1), year(2000) {}

// Parameterized constructor
MyDate::MyDate(int dd, int mm, int yy) : day(dd), month(mm), year(yy) {}

// Getters and setters
int MyDate::getDay() const {
    return day;
}

void MyDate::setDay(int day) {
    this->day = day;
}

int MyDate::getMonth() const {
    return month;
}

void MyDate::setMonth(int month) {
    this->month = month;
}

int MyDate::getYear() const {
    return year;
}

void MyDate::setYear(int year) {
    this->year = year;
}

// Method to display date in dd/mm/yyyy format
void MyDate::displayDate() const {
    std::cout << (day < 10 ? "0" : "") << day << "/"
              << (month < 10 ? "0" : "") << month << "/"
              << year << std::endl;
}

// Method to display month name
void MyDate::displayMonthName() const {
    if (month >= 1 && month <= 12) {
        std::cout << monthNames[month - 1] << std::endl;
    } else {
        std::cout << "Invalid month" << std::endl;
    }
}

// Method to display day of the week
void MyDate::displayDayOfWeek() const {
    int dayOfWeek = calculateDayOfWeek(day, month, year);
    std::cout << dayNames[dayOfWeek] << std::endl;
}

// Static method to calculate day of the week using Zeller's Congruence
int MyDate::calculateDayOfWeek(int dd, int mm, int yy) {
    if (mm < 3) {
        mm += 12;
        yy -= 1;
    }
    int k = yy % 100;
    int j = yy / 100;
    int h = (dd + 13*(mm + 1)/5 + k + k/4 + j/4 + 5*j) % 7;
    return (h + 5) % 7; // Adjusting for the different indexing of Zeller's Congruence
}
Leave a Comment