Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <sstream>
  5. //#include <ctime>
  6. #pragma GCC optimize ("O2")
  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--;
  29.     second.tm_mon--;
  30.     first.tm_year -= 1900;
  31.     second.tm_year -= 1900;
  32.     first.tm_hour = first.tm_min = first.tm_sec = 0;
  33.     second.tm_hour = second.tm_min = second.tm_sec = 0;
  34.  
  35.     long int difference = difftime(mktime(&second), mktime(&first));//16 years, total 5844 days
  36.     long int dayt = difference / (60*60*24);
  37.     difference = second.tm_year - first.tm_year;
  38.     long int monn = second.tm_mon - first.tm_mon;
  39.  
  40.     if (second.tm_mon < first.tm_mon) {
  41.         difference=difference-1;monn=monn+12;
  42.     }
  43.    
  44.     if(second.tm_mday < first.tm_mday){
  45.         monn=monn-1;
  46.     }
  47.    
  48.     if(monn==0 && difference== 0){
  49.         cout << "total "<<dayt <<" days"<< endl;
  50.     }
  51.     else if(monn==0 && difference !=1){
  52.         cout << difference <<" years, total "<< dayt <<" days"<< endl;//16 years, total 5844 days
  53.     }
  54.     else if(monn==0 && difference ==1){
  55.         cout << difference <<" year, total "<< dayt <<" days"<< endl;//16 years, total 5844 days
  56.     }
  57.     else if (difference== 0 && monn!=1){
  58.         cout << monn<<" months, total "<< dayt <<" days"<< endl;
  59.     }
  60.     else if (difference== 0 && monn==1){
  61.         cout << monn<<" month, total "<< dayt <<" days"<< endl;
  62.     }
  63.     if (difference != 0 && monn!=0 && dayt != 0 && monn!=1 && difference != 1)
  64.     cout << difference << " years, "<< monn<<" months, total "<< dayt <<" days"<< endl;
  65.     else if (difference != 0 && monn!=0 && dayt != 0 && monn==1 && difference == 1)
  66.     cout << difference << " year, "<< monn<<" month, total "<< dayt <<" days"<< endl;
  67.     else if (difference != 0 && monn!=0 && dayt != 0 && difference == 1)
  68.     cout << difference << " year, "<< monn<<" months, total "<< dayt <<" days"<< endl;
  69.     else if (difference != 0 && monn!=0 && dayt != 0 && monn == 1)
  70.     cout << difference << " years, "<< monn<<" month, total "<< dayt <<" days"<< endl;
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement