View Single Post
Old 02-22-2005, 12:35 AM   #3 (permalink)
i like c++
Registered User
 
Join Date: Feb 2005
Posts: 5
i like c++ is on a distinguished road
Here is my code.

Here is my code, but its very long and i'm not sure how to shorten it.
Code:
//Set current variable.
void Date::Date(int DD, int MM, int YYYY){
  date=DD;
  month=MM;
  year=YYYY;
}

//Next date for the current variable.
void Date::next(){
  if(date<28)
  date+=1;
  else{
    switch(month){
      //Compare the month.
      case '1':if(date==31){
                 date=1;
                 month=2;               
               }               
               else
                 date+=1;
               break;  
      case '2':if(year<=1752){       //For years following Julian calendar.
                 if(year%4==0){     //Leap year.
                   if(date==29){
                     date=1;
                     month=3;
                   }
                   else
                   date+=1;
                 }
                 else{
                   date=1;
                   month=3;
                 }      
               }                                
               if(year>1752 && year<=9999){     //For year following the Gregorian calendar.
                 if((year%4==0 && year%1000!=0) || year%400==0){   //Leap year.
                   if(date==29){
                     date=1;
                     month=3;
                   }
                   else                   
                   date+=1;
                 }
                 else{
                   date=1;
                   month=3;
                 }        
               }
               break;
      case '3':if(date==31){
                 date=1;
                 month=4;               
               }
               else
                 date+=1;
               break; 
      case '4':if(date==30){
                 date=1;
                 month=5;               
               }
               else
                 date+=1;
               break;  
      case '5':if(date==31){
                 date=1;
                 month=6;               
               }
               else
                 date+=1;
               break;  
      case '6':if(date==30){
                 date=1;
                 month=7;              
               }
               else
                 date+=1;
               break;  
      case '7':if(date==31){          
                 date=1;
                 month=8;                     
               }              
               else
                 date+=1;
               break;  
      case '8':if(date==31){
                 date=1;
                 month=9;               
               }
               else
                 date+=1;
               break;  
      case '9':if(year==1752 && date==2){    
          //Special case in september 1972.
                   date=14;                 
               }
               else{
                 if(date==30){
                   date=1;
                   month=10;               
                 }
                 else
                   date+=1;  
               }
               break;  
      case '10':if(date==31){
                 date=1;
                 month=11;               
               }
               else
                 date+=1;
               break;  
      case '11':if(date==30){
                 date=1;
                 month=2;               
               }
               else
                 date+=1;
               break;  
      case '12':if(date==31){
                 date=1;
                 month=1;
                 if(year<9999)
                 year+=1;
               }
               else
                 date+=1;
               break;  
    }//End of switch loop.      
  }//End of else.
}

//Print out variable day of week.
void printD(){
  int tempDate, tempMonth, tempYear, k, numOfDays=0;//Local declarations.
  tempDate=date;
  tempMonth=month;
  tempYear=year;
  date=1;
  month=1;
  year=1;
  
  while(tempDate!=date && tempMonth!=month && tempYear!=year){
     next();
     numOfDays++;
  }//End of while loop.

  k=numOfDays%7;  
  switch(k){
    case '0':cout << "Saturday";
    case '1':cout << "Sunday";
    case '2':cout << "Monday";
    case '3':cout << "Tuesday";
    case '4':cout << "Wednesday";
    case '5':cout << "Thursday";
    case '6':cout << "Friday";  
  }//End of switch.
  
  date=tempDate;
  month=tempMonth;
  year=tempYear;
  if(date==1 && month==1)
    cout << " (New Year's Day)";//If that day is a new year.
  if(date=14 && month==2)
    cout << " (Valentine's Day)";//If that day is a valentine's day.
  cout << endl;
}//End of printD function.

//Print out variable as DD/MM/YYYY.
void printT(){
  cout << date << "/" << month << "/" << year << endl;
}//End of printT function.

//Print out variable in the form of 28 March 2004;
void printW(){
  cout << date;
  //Compare month.
  switch(month){
    case '1':cout << " January ";
    case '2':cout << " FebRuary ";
    case '3':cout << " March ";
    case '4':cout << " April ";
    case '5':cout << " May ";
    case '6':cout << " June ";
    case '7':cout << " July ";
    case '8':cout << " August ";
    case '9':cout << " September ";
    case '10':cout << " October ";
    case '11':cout << " November ";
    case '12':cout << " December ";
  }//End of switch.
  cout << year << endl;
}//End of printW function.

//Print out the absolute number of days between current date and a particular date, D.
void diff(int theDate, int theMonth, int theYear){
int tempDate, tempMonth, tempYear, k,difference, numOfDays=0;//Local declarations.
  tempDate=date;
  tempMonth=month;
  tempYear=year;
  if(theDate>=date && theMonth>=month && theYear>=year){
     // If current date is earlier than or same as D.
    while(date!=theDate && month!=theMonth && year!=theYear){
      next();
      numOfDays++;  
    }//End of while loop.
    if(numOfDays>=1)
      difference=numOfDays-1;
    else
      difference=numOfDays;
    cout << difference << endl;
    date=tempDate;
    month=tempMonth;
    year=tempYear;
  }
  else{       //If d is earlier than current date.
    date=theDate;
    month=theMonth;
    year=theYear;
    while(date!=tempDate && month!=tempMonth && year!=tempYear){
      next();
      numOfDays++;  
    }
    if(numOfDays>=1)
      difference=numOfDays-1;
    else
      difference=numOfDays;
    cout << difference << endl;
    date=tempDate;
    month=tempMonth;
    year=tempYear;  
  }
}//End of diff function.

Last edited by redhead; 02-22-2005 at 01:52 AM.
i like c++ is offline   Reply With Quote