Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. //stabilire daca year este an bisect
  6. #define isleapyear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))
  7.  
  8. struct date {
  9.     int year;
  10.     int month;
  11.     int day;
  12.     int wday;
  13. };
  14.  
  15. char *wdays[11]={"Luni","Marti","Miercuri","Joi","Vineri","Sambata","Duminica"};
  16.  
  17. int isdatevalid ( struct date);
  18.  
  19. void weekday( struct date &);
  20.  
  21. int main()
  22. {
  23.     int  month, day, year;
  24.     struct date d;
  25.  
  26.     cout << "Introdu data in format: zz ll aaaa : ";
  27.     cin >> d.day >> d.month >> d.year;
  28.     if (isdatevalid(d)) {
  29.         weekday(d);
  30.         cout << "Ziua spatamanii de la aceasta data este: " << wdays[d.wday] << "\n";
  31.     }
  32.     else
  33.         cout << d.day << "/" << d.month << "/" << d.year << " is not a valid date!";
  34.     getchar();
  35.     getchar();
  36.     return 0;
  37. }
  38.  
  39. //functie de verificare a validatatii datei introduse
  40. int isdatevalid(struct date d)
  41. {
  42.   if (d.day <= 0) return 0 ;
  43.   switch( d.month )
  44.     {
  45.       case 1  :
  46.       case 3  :
  47.       case 5  :
  48.       case 7  :
  49.       case 8  :
  50.       case 10 :
  51.       case 12 : if (d.day > 31) return 0 ; else return 1 ;
  52.       case 4  :
  53.       case 6  :
  54.       case 9  :
  55.       case 11 : if (d.day > 30) return 0 ; else return 1 ;
  56.       case 2  :
  57.         if ( d.day > 29 ) return 0 ;
  58.       if ( d.day < 29 ) return 1 ;
  59.       if (isleapyear(d.year)) return 1 ;   // an bisect
  60.     else return 0 ;
  61.     }
  62.   return 0 ;
  63. }
  64.  
  65. void weekday(struct date &d)
  66. {  
  67.   int ix, tx, vx;
  68.  
  69.   switch (d.month) {
  70.     case 2  :
  71.     case 6  : vx = 0; break;
  72.     case 8  : vx = 4; break;
  73.     case 10 : vx = 8; break;
  74.     case 9  :
  75.     case 12 : vx = 12; break;
  76.     case 3  :
  77.     case 11 : vx = 16; break;
  78.     case 1  :
  79.     case 5  : vx = 20; break;
  80.     case 4  :
  81.     case 7  : vx = 24; break;
  82.   }
  83.   if (d.year > 1900)  // 1900 nu a fost an bisect
  84.     d.year -= 1900;
  85.   ix = ((d.year - 21) % 28) + vx + (d.month > 2);
  86.   tx = (ix + (ix / 4)) % 7 + d.day;
  87.   d.wday =  tx % 7;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement