Untitled

 avatar
unknown
plain_text
a year ago
4.1 kB
7
Indexable
[3:40 PM] Sanjeevan (Unverified)
#pragma once
 
class MyDate {
    int dd, mm, yy;
    static const char *dayNames[7];
    static const char *monthNames[12];
    static const int dayInMonth[12];
 
public:
    MyDate();
    MyDate(char *date);
    MyDate(int dd, int mm, int yy);
 
// Static Methods
    static const char *getDOW(int dd, int mm, int yy);
    static const char *getMonthName(int mm);
   
 
    int getDay(void) const;
    void setDay(int dd);
    int getMonth(void) const;
    void setMonth(int mm);
    int getYear(void) const;
    void setYear(int yy);
 
    int getDOW(void) const;
 
    void displayDate(void) const;
    void displayFullDate(void) const;
    void displayDayOfWeek(void) const;
    void displayMonthName(void) const;
};

[3:40 PM] Sanjeevan (Unverified)
#include "mydate.h"
#include <chrono>
#include <ctime>
#include <stdio.h>
#include <iostream>
 
 
using namespace std;
 
 
const char *MyDate::dayNames[7] = {"Sunday","Monday","Tuesday","Wednesday",
                                    "Thursday", "Friday","Saturday"};
 
const char *MyDate::monthNames[12] = {"January", "February", "March", "April",
                                      "May", "June", "July", "August",
                                      "September","October","November",
                                      "December"};
const int MyDate::dayInMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
 
MyDate::MyDate()
{
    // Get the current date from system and update the date
    std::time_t t = std::time(0);   // get time now
    std::tm* now = std::localtime(&t);
    yy = now->tm_year + 1900;
    mm = now->tm_mon + 1;
    dd = now->tm_mday;
}
 
 
MyDate::MyDate(char *date)
{
    // data is in the form dd/mm/yyyy
    sscanf(date, "%d/%d/%d", &this->dd, &(this->mm), &this->yy);
}
 
MyDate::MyDate(int dd, int mm, int yy)
{
    this->dd = dd;
    this->mm = mm;
    this->yy = yy;
}
 
int MyDate::getDay(void) const
{
    return this->dd;
}
 
void MyDate::setDay(int dd)
{
    if (dd < 1 || dd > dayInMonth[this->mm-1])
        return;
    this->dd = dd;
}
 
int MyDate::getMonth(void) const
{
    return this->mm;
}
 
void MyDate::setMonth(int mm)
{
    if (mm < 1 || mm > 12)
        return;
    if (this->dd > dayInMonth[mm-1])
        return;
    this->mm = mm;
}
 
int MyDate::getYear(void) const
{
    return this->yy;
}
 
void MyDate::setYear(int yy)
{
    if (yy < 1900 && yy > 2100)
        return;
    this->yy = yy;
}
 
int MyDate::getDOW(void) const
{
    int k = yy % 100;
    int j = yy / 100;
    int h = (this->dd+1 + 13 * (this->mm) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;
 
    return h;
}
 
void MyDate::displayDate(void) const
{
    cout << this->dd << "/" << this->mm << "/" << this->yy << endl;
}
 
void MyDate::displayFullDate(void) const
{
    int dow = getDOW();
    cout << this->dayNames[dow] << " " << this->dd << "-" << monthNames[this->mm - 1] << " " << this->yy << endl;
}
void MyDate::displayDayOfWeek(void) const
{
    int dow = getDOW();
    cout << this->dayNames[dow] << endl;
}
void MyDate::displayMonthName(void) const
{
    cout << this->monthNames[this->mm - 1] << endl;
}
 
 
 
const char *MyDate::getDOW(int dd, int mm, int yy)
{
    int k = yy % 100;
    int j = yy / 100;
    int h = (dd+1 + 13 * (mm) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;
 
    return dayNames[h];
   
}
 
const char *MyDate::getMonthName(int mm)
{
    return monthNames[mm - 1];
}
[3:41 PM] Sanjeevan (Unverified)
mydatemain.cpp
[3:41 PM] Sanjeevan (Unverified)
#include "mydate.h"
#include <iostream>
using namespace std;
 
 
int main()
{
    MyDate today;
 
    today.displayDate();
    today.displayDayOfWeek();
    today.displayFullDate();
    today.displayMonthName();
 
    MyDate tomorrow((char *)"21/5/2024");
    tomorrow.displayDate();
    tomorrow.displayDayOfWeek();
    tomorrow.displayFullDate();
    tomorrow.displayMonthName();
 
    MyDate yesterday(19, 5, 2024);
    yesterday.displayDate();
    yesterday.displayDayOfWeek();
    yesterday.displayFullDate();
    yesterday.displayMonthName();
 
    cout << MyDate::getDOW(20, 5, 2024) << endl;
    cout << MyDate::getMonthName(5) << endl;
}
Editor is loading...
Leave a Comment