Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class date
- {
- int day;
- int month;
- int year;
- public:
- date()
- {
- cout << "enter the day" << endl;
- cin >> day;
- cout << "enter the month" << endl;
- cin >> month;
- cout << "enter the year" << endl;
- cin >> year;
- }
- date(int a, int b, int c) : day(a), month(b), year(c)
- {
- }
- date(const date &prod)
- {
- this->day = prod.day, this->month = prod.month, this->year = prod.year;
- }
- date & operator+=( int d);
- date & operator-=( int d);
- date & operator =(const date&);
- int countdays();
- date & operator+=( date&);
- date & operator -=(date&);
- friend ostream& operator<<(ostream& os, date&);
- friend istream& operator >> (istream& is, date& prod);
- ~date() { }
- };
- date & date :: operator = (const date &prod1)
- {
- this->day = prod1.day, this->month =prod1.month, this->year = prod1.year;
- return *this;
- }
- int main()
- {
- date prod, prod1;
- prod = prod1;
- cout <<"prod = "<< prod;
- cout << endl;
- cout << "prod1 = "<<prod1;
- cout << "enter the amount of days you want to add" << endl;
- int amount; cin >> amount;
- prod += amount;
- cout <<"prod +"<<amount<<" days =" << prod;
- prod -= amount;
- cout << "prod -" << amount << " days =" << prod;
- prod += prod1;
- cout << "prod += prod1=" <<prod;
- prod -= prod1;
- cout <<"prod -= prod1 = "<< prod;
- system("pause");
- return 0;
- }
- date & date :: operator+=( date& prod1)
- {
- int k = prod1.countdays();
- *this += k;
- return *this;
- }
- ostream& operator<<(ostream& os, date& prod)
- {
- os << prod.day << "." << prod.month << "." << prod.year << endl;
- return os;
- }
- istream & operator >> (istream & is, date&prod)
- {
- cout << "Ente day,month,year " << endl;
- is >> prod.day;
- is >> prod.month;
- is >> prod.year;
- return is ;
- }
- date & date :: operator-=(int d)
- {
- int c = day, count = 0;
- day -= d;
- int i = 0;
- if (year % 4 == 0 && (((year % 100 != 0 || year % 400 == 0)))&& month>2)i++;
- while (d > 0)
- {
- if (count != 0)
- {
- c = day; day -= d;
- }
- count++;
- switch (month-1)
- {
- case 0:
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- {
- if (day < 1)
- {
- d -= c;
- day = 31;
- month -= 1;
- if (month < 1)
- {
- month = 12;
- year -= 1;
- if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))i++;
- if (year == 0) { cout << "Even Jesus Christ was born later..." << endl; d = 0; break; }
- }
- }
- else d = 0;
- break;
- }
- case 4:
- case 6:
- case 9:
- case 11:
- {
- if (day < 1)
- {
- d -= c;
- day = 30;
- month -= 1;
- }
- else d = 0;
- break;
- }
- case 2:
- {
- if (day < 1)
- {
- d -= c;
- day = 28+i;
- month -= 1;
- if(i==1) i = 0;
- }
- else d = 0;
- break;
- }
- }
- }
- return *this;
- }
- date& date :: operator+=(int d)
- {
- int c = day, count = 0;
- day += d;
- int i = 0;
- if (((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) && month <= 2)i++;
- while (d > 0)
- {
- if (count != 0) {
- c = day; day += d;
- }
- count++;
- switch (month)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- {
- if (day > 31)
- {
- d -= 32 - c;
- day = 1;
- month += 1;
- if (month > 12)
- {
- month = 1;
- year += 1;
- if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))i++;
- }
- }
- else d = 0;
- break;
- }
- case 4:
- case 6:
- case 9:
- case 11:
- {
- if (day > 30)
- {
- d -= 31 - c;
- day = 1;
- month += 1;
- }
- else d = 0;
- break;
- }
- case 2:
- {
- if (day > 28 + i)
- {
- d -= 29 + i - c;
- day = 1;
- month += 1;
- if(i==1) i = 0;
- }
- else d = 0;
- break;
- }
- }
- }
- return *this;
- }
- int date :: countdays()
- {
- int s = 0; int i = 0; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)i++;
- switch (month)
- {
- case 1: s = 0; break;
- case 2: s = 31; break;
- case 3: s = 59 + i; break;
- case 4: s = 90; break;
- case 5: s = 120; break;
- case 6: s = 151; break;
- case 7: s = 181; break;
- case 8: s = 212; break;
- case 9: s = 243; break;
- case 10: s = 273; break;
- case 11: s = 304; break;
- case 12: s = 334; break;
- default: return 1;
- }
- s = s + day + (365.25 *year) - i;
- return s;
- }
- date & date::operator -=(date&prod)
- {
- int p = prod.countdays();
- int t = this->countdays();
- if (t < p)
- cout << "Even Jesus Christ was born later..." << endl;
- else *this -= p;
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement