1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
#include<iostream>
using namespace std;
/*-- Function prototypes--*/
int POS_INT_TRANS(char* system_number);
bool Switch_ERROR_message_ALERT(int d,int m,int y);
unsigned long int GeT_The_NumbER_of_DAYS();//up to 4.294.967.296 days , approximately 11.767.033 years (365 days per year).
/*--global variables--*/
char date_buf[13];
char temp_day[3];
char temp_month[3];
char temp_year[6];
int day1,day2;
int month1,month2;
int year1,year2;
main( int argc , char* argv[] )/*--this program does not use the main arguments.*/
{
bool Sc=0;
char ch1,ch2;
do
{
cout<<"Enter the first date in the following format DD-MM-YYYY(for Europe)--:";
cin.get(date_buf[0]);//--in case the user press 'enter' without giving any other data
if( date_buf[0]==10 )
{
cout<<"--ERROR--Wrong format of the given data."<<endl;
continue;
}
cin.get( date_buf+1,11 );
for(int i=0; i<2; i++)
{
temp_day[i]=date_buf[i];
temp_day[i+1]='\0';
}
for(int c=0,i=3; i<5; c++,i++)
{
temp_month[c]=date_buf[i];
temp_month[c+1]='\0';
}
for(int c=0,i=6; i<10; c++,i++)
{
temp_year[c]=date_buf[i];
temp_year[c+1]='\0';
}
day1 =POS_INT_TRANS( temp_day );
month1=POS_INT_TRANS( temp_month );
year1 =POS_INT_TRANS( temp_year );
Sc=Switch_ERROR_message_ALERT(day1,month1,year1);
cin.ignore(4096,'\n');
}
while(!Sc);
/*------end of first input block-------------------------------------------------------------------*/
/***************************************************************************************************/
/*--------second input block-----------------------------------------------------------------------*/
/***************************************************************************************************/
Sc=0;
do
{
cout<<"Enter the second date (the way you did previously)---------------------:";
cin.get(date_buf[0]);//--in case the user press 'enter' without giving any other data
if( date_buf[0]==10 )
{
cout<<"--ERROR--Wrong format of the given data."<<endl;
continue;
}
cin.get( date_buf+1,11 );
for(int i=0; i<2; i++)
{
temp_day[i]=date_buf[i];
temp_day[i+1]='\0';
}
for(int c=0,i=3; i<5; c++,i++)
{
temp_month[c]=date_buf[i];
temp_month[c+1]='\0';
}
for(int c=0,i=6; i<10; c++,i++)
{
temp_year[c]=date_buf[i];
temp_year[c+1]='\0';
}
day2 =POS_INT_TRANS( temp_day );
month2=POS_INT_TRANS( temp_month );
year2 =POS_INT_TRANS( temp_year );
Sc=Switch_ERROR_message_ALERT(day2,month2,year2);
cin.ignore(4096,'\n');
}
while(!Sc);
/*------end of second input block-------------------------------------------------------------------*/
/*********************************************************************************************/
system("clear");
cout<<"---***************************---"<<endl;
cout<<"---******----RESULTS----******---"<<endl;
cout<<"---***************************---"<<endl;
cout<<"---------------------------------"<<endl;
cout<<"---OldER DaTE--:"<<day1<<"-"<<month1<<"-"<<year1<<endl;
cout<<"---NewER DaTE--:"<<day2<<"-"<<month2<<"-"<<year2<<endl;
unsigned long int TOTAL_DAYS=GeT_The_NumbER_of_DAYS();//--call of GeT_The_NumbER_of_DAYS() routine
cout<<"----****--"<<TOTAL_DAYS<<" days between these two dates!!---"<<endl;
cout<<"\n-------EnD OF ThE PRogrAM!!!---ByE----"<<endl;
}
/*------END OF MAIN FUNCTION BLOCK------------------------------------------------------------------*/
/****************************************************************************************************/
/*-----------------FUNCTIONS FIELD------------------------------------------------------------------*/
/****************************************************************************************************/
/*-----Function GeT_The_NumbER_of_DAYS------*/
unsigned long int GeT_The_NumbER_of_DAYS()
{
int m_day1,m_month1,m_year1,m_day2,m_month2,m_year2;
int C,month_CounteR;
int TotaL_DAYS=0;
if( (year1<year2) || ((year1==year2) && (month1<month2)) || ((year1==year2) && (month1==month2) && (day1<=day2)) )
{
m_day1=day1;
m_day2=day2;
m_month1=month1;
m_month2=month2;
m_year1=year1;
m_year2=year2;
}
if( (year1>year2) || ((year1==year2) && (month1>month2)) || ((year1==year2) && (month1==month2) && (day1>day2)) )
{
m_day1=day2;
m_day2=day1;
m_month1=month2;
m_month2=month1;
m_year1=year2;
m_year2=year1;
}
for(int y=m_year1; y<=m_year2; y++)
{
if( y==m_year1 )
{
C=m_month1;
month_CounteR=12;
}
if( y==m_year2 )
{
month_CounteR=m_month2-1;
}
for(C; C<=month_CounteR; C++)
{
if( ((C%2==1)&&(C!=9)&&(C!=11)) || C==8 || C==10 || C==12 )//-January-March-May-July-August-October-December-
{
TotaL_DAYS=TotaL_DAYS+31;
continue;
}
if( (C==2)&&(y%4==0) )//-February and leap year-
{
TotaL_DAYS=TotaL_DAYS+29;
continue ;
}
if( (C==2)&&(y%4!=0) )//-February and no leap year-
{
TotaL_DAYS=TotaL_DAYS+28;
continue;
}
TotaL_DAYS=TotaL_DAYS+30;//-Normal-*-April-June-September-November-
}//--end of internal loop
//cout<<TotaL_DAYS<<endl;
C=1;
}//--end of external loop
TotaL_DAYS=TotaL_DAYS-m_day1+m_day2;
return TotaL_DAYS;
}
/*################################################################*/
/*-----Function POS_INT_TRANS------*/
int POS_INT_TRANS(char* system_number)/*--this function transforms a string ONLY into a POSITIVE integer.-only 0-9 characters accepted*/
{
int sum=0;
if(system_number==0)
{
return -1;
}
for(int i=0; system_number[i]!='\0'; i++)
{
if( system_number[i]<48 || system_number[i]>57 )
{
return -1;//no arithmetic characters
}
}
sum=atoi(system_number);
return sum;
}
/*################################################################*/
/*-----Function switch_error_message_alert------*/
bool Switch_ERROR_message_ALERT(int d,int m,int y)
{
int control=-1;
if(y<0)
control=1;
if(m>12)
control=2;
if(m<1)
control=3;
if(d>31)
control=4;
if(d<1)
control=5;
if(m==2/*February*/ && d>29)
control=6;
if(m==2 && (y%4)!=0/*no leap year*/ && d==29)
control=7;
if(m==4/*April*/ && d==31)
control=8;
if(m==6/*June*/ &&d==31)
control=9;
if(m==9/*September*/ && d==31)
control=10;
if(m==11/*November*/ && d==31)
control=11;
if(date_buf[0]==45||date_buf[1]==45||date_buf[2]!=45||date_buf[3]==45||date_buf[4]==45||date_buf[5]!=45||date_buf[6]==45||date_buf[6]==0||date_buf[7]==45||date_buf[7]==0||date_buf[8]==45||date_buf[8]==0||date_buf[9]==45||date_buf[9]==0||date_buf[10]!=0)
{
control=0;
}
switch( control )
{
case 0: cout<<"--ERROR--Wrong format of the given data."<<endl; break;
case 1: cout<<"--ERROR--The year you gave is invalid.Year should be >0 and <=9999."<<endl; return 0; break;
case 2: cout<<"--ERROR--The month you gave is invalid.Month should be >0 and <13."<<endl; return 0; break;
case 3: cout<<"--ERROR--The month you gave is invalid.Month should be >0 and <13."<<endl; return 0; break;
case 4: cout<<"--ERROR--The day you gave is too large.Day should be >0 and <32."<<endl; return 0; break;
case 5: cout<<"--ERROR--The day you gave is invalid.Day should be >0 and <32."<<endl; return 0; break;
case 6: cout<<"--ERROR--There is no such day["<<d<<"] on February."<<endl; return 0; break;
case 7: cout<<"--ERROR--The year["<<y<<"] is not a leap one.Thus,29th February "<<y<<" is not a valid date."<<endl; return 0; break;
case 8: cout<<"--ERROR--April has just 30 days."<<endl; return 0; break;
case 9: cout<<"--ERROR--June has just 30 days."<<endl; return 0; break;
case 10: cout<<"--ERROR--September has just 30 days."<<endl; return 0; break;
case 11: cout<<"--ERROR--November has just 30 days."<<endl; return 0; break;
default : return 1; break;
}
}
|