myClass.h
By: jazy | Date: Sep 21 2007 01:01 | Format: C++ | Expires: never | Size: 2.83 KB | Hits: 1248
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- #include <vector>
- using namespace std;
- class dateType {
- private:
- long julianDay;
- long toJulianDay(int m, int d, int y);
- std::vector<int> monthLength;
- public:
- long getJulian() {
- return julianDay;
- }
- void incDate() {
- julianDay += 1;
- }
- int daysToDate(dateType otherDate) {
- return julianDay - otherDate.julianDay;
- }
- void setDate(int m, int d, int y) {
- cout<<"nEnter the following.... n";
- cout<<"Month: ";
- cin>>m;
- cout<<"Day: ";
- cin>>d;
- cout<<"Year: ";
- cin>>y;
- if (validDate(m,d,y)) {
- julianDay = toJulianDay(m,d,y);
- }
- else
- julianDay = 0;
- }
- void printDate() {
- cout<<"JulianDate: "<<julianDay<<endl;
- }
- void setVector(int isLeap,int y) {
- if (isLeap == 0) {
- monthLength.push_back(0);
- monthLength.push_back(31);
- monthLength.push_back(28);
- monthLength.push_back(31);
- monthLength.push_back(30);
- monthLength.push_back(31);
- monthLength.push_back(30);
- monthLength.push_back(31);
- monthLength.push_back(31);
- monthLength.push_back(30);
- monthLength.push_back(31);
- monthLength.push_back(30);
- monthLength.push_back(31);
- }
- else {
- monthLength.push_back(0);
- monthLength.push_back(31);
- monthLength.push_back(29);
- monthLength.push_back(31);
- monthLength.push_back(30);
- monthLength.push_back(31);
- monthLength.push_back(30);
- monthLength.push_back(31);
- monthLength.push_back(31);
- monthLength.push_back(30);
- monthLength.push_back(31);
- monthLength.push_back(30);
- monthLength.push_back(31);
- }
- }
- int isLeapYear(int y) {
- long tmp, tmp1;
- tmp = toJulianDay(2,28,y);
- tmp1 = toJulianDay(3,1,y);
- return ((tmp1-tmp) > 1);
- }
- bool validDate (int m,int d, int y) {
- setVector(isLeapYear(y),y);
- //have to validate the month first//
- if (m > 0 && m < 13)
- std::cout<<"month looks good.....n";
- else
- return false;
- vector<int>::iterator iter;
- iter = monthLength.begin();
- for (int i = 0 ; i < m; i++) {
- iter++;
- }
- int test = *iter;
- if (d < test && d > 0)
- cout<<"month looks goodn";
- else
- return false;
- if (y <= 9999 && y >= -4713 )
- std::cout<<"year looks good.....n";
- else
- return false;
- return true;
- }
- dateType() {
- julianDay = 0;
- }
- dateType(int m,int d,int y) {
- if (validDate(m,d,y)) {
- julianDay = toJulianDay(m,d,y);
- }
- else
- julianDay = 0;
- }
- };
- long dateType::toJulianDay(int m, int d, int y) {
- long toReturn;
- int a, b , yy, mm, dd;
- float yorr;
- yy = y;
- mm = m;
- dd = d;
- if (yy < 0)
- yy++;
- yorr = ( yy > 0 ? 0.0 : 0.75 );
- if (mm <= 2) {
- yy--;
- mm += 12;
- }
- b = 0;
- if (yy *10000.0 + mm * 100.0 + dd >= 15821015.0 ) {
- a = yy / 100;
- b = 2 - a + a /4;
- }
- toReturn = dd - 32075 + 1461 * (yy + 4800 + ( mm - 14) / 12) / 4 +
- 367L * ( mm - 2 - ( mm - 14 ) /12 * 12) / 12 -
- 3 * ( (yy+4900 +(mm -14)/12)/100)/4;
- return toReturn;
- }
Latest pastes
1 days ago
2 days ago
4 days ago
6 days ago
6 days ago