SHARE
TWEET

Untitled

a guest Feb 14th, 2020 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include <ctime>
  6. #pragma GCC optimize ("O2, pipe")
  7.  
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     tm first, second;
  14.    
  15.     string BEGIN;
  16.     cin >> BEGIN; cin.ignore();
  17.     string END;
  18.     cin >> END; cin.ignore();
  19.  
  20.     long int day,month,year,day1,month1,year1;
  21.    
  22.     replace( BEGIN.begin(),BEGIN.end(),'.',' ');
  23.     istringstream( BEGIN ) >> first.tm_mday >> first.tm_mon >> first.tm_year;
  24.    
  25.     replace( END.begin(),END.end(),'.',' ');
  26.     istringstream( END ) >> second.tm_mday >> second.tm_mon >> second.tm_year;
  27.    
  28.     first.tm_mon= first.tm_mon-1;
  29.     second.tm_mon= second.tm_mon-1;
  30.     first.tm_year = first.tm_year-1900;
  31.     second.tm_year = second.tm_year-1900;
  32.     first.tm_hour = NULL;
  33.     first.tm_min = NULL;
  34.     first.tm_sec = NULL;
  35.     second.tm_hour = NULL;
  36.     second.tm_min = NULL;
  37.     second.tm_sec = NULL;
  38.  
  39.     long int difference = difftime(mktime(&second), mktime(&first));//16 years, total 5844 days
  40.     long int dayt = difference / (60*60*24);
  41.     difference = second.tm_year - first.tm_year;
  42.     long int monn = second.tm_mon - first.tm_mon;
  43.  
  44.     if (second.tm_mon < first.tm_mon) {
  45.         difference=difference-1;monn=monn+12;
  46.     }
  47.    
  48.     if(second.tm_mday < first.tm_mday){
  49.         monn=monn-1;
  50.     }
  51.    
  52.     if(monn==0 && difference== 0){
  53.         cout << "total "<<dayt <<" days"<< endl;
  54.     }
  55.     else if(monn==0 && difference !=1){
  56.         cout << difference <<" years, total "<< dayt <<" days"<< endl;//16 years, total 5844 days
  57.     }
  58.     else if(monn==0 && difference ==1){
  59.         cout << difference <<" year, total "<< dayt <<" days"<< endl;//16 years, total 5844 days
  60.     }
  61.     else if (difference== 0 && monn!=1){
  62.         cout << monn<<" months, total "<< dayt <<" days"<< endl;
  63.     }
  64.     else if (difference== 0 && monn==1){
  65.         cout << monn<<" month, total "<< dayt <<" days"<< endl;
  66.     }
  67.    
  68.     if (difference != 0 && monn!=0 && dayt != 0 && monn!=1 && difference != 1)
  69.     cout << difference << " years, "<< monn<<" months, total "<< dayt <<" days"<< endl;
  70.     else if (difference != 0 && monn!=0 && dayt != 0 && monn==1 && difference == 1)
  71.     cout << difference << " year, "<< monn<<" month, total "<< dayt <<" days"<< endl;
  72.     else if (difference != 0 && monn!=0 && dayt != 0 && difference == 1)
  73.     cout << difference << " year, "<< monn<<" months, total "<< dayt <<" days"<< endl;
  74.     else if (difference != 0 && monn!=0 && dayt != 0 && monn == 1)
  75.     cout << difference << " years, "<< monn<<" month, total "<< dayt <<" days"<< endl;
  76.     //return 0;
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top