Alx09

Ex 20 Sebi

May 9th, 2020
1,602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. static unsigned months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // pastram dimensiunea fiecariei luni
  5.  
  6. typedef struct  {
  7.     unsigned day, month, year;
  8. } Data; // folosim o lista sa ne organizam mai usor
  9. unsigned CountDays(Data  d1, Data d2) {  // folosim functia pentru a numara cate zile sunte intre 1900, d1  si d1/d2
  10.     unsigned days = 0, i;
  11.     if (d1.year == d2.year) {
  12.         if (d1.month < d2.month) {
  13.             days = d2.day;
  14.             for (i = d2.month - 1; i > d1.month; i--)
  15.                 days += months[i] + (d2.month == 2 && d2.year % 4 == 0);
  16.             days += months[i] - d1.day;
  17.             return days;
  18.         }
  19.         return d2.day - d1.day;
  20.     }
  21.     if (d1.month == 1) { // daca e prima luna adaugam numarul de zile
  22.         days += months[1] - d1.day + 1;
  23.         d1.day = 1; // ziua 1
  24.         d1.month = 2; // luna devine 2
  25.     }
  26.     if ((d1.month == 2 && (d1.year % 4) == 0)) days++;// daca avem februruiare an bisect
  27.     days += months[d1.month] - d1.day ; // aduagam celelate zile din luna curenta
  28.     for (i = d1.month + 1; i <= 12; i++) // adaugam zilele pana la sfarsitul anului
  29.         days += months[i];
  30.        
  31.        days += d2.day - 1; //  adunam zilele din luna curenta
  32.        d2.month--;// scadem luna
  33.     for (i = d2.month; i > 2; i--) // adaugam ziele pentru lunile anterioarene oprim la februare pentru a stabil daca anu este bisect  
  34.         days += months[i];
  35.  
  36.     if (d2.month == 2 && d2.year % 4 == 0) days++;//daca anul este bisect avem o zi in plus  
  37.  
  38.     if (d2.month >= 2) days += months[2]; // daca luna este cel puntin februaie
  39.  
  40.     if (d2.month >= 1) days += months[1]; // daca luna este cel putin ianuarie
  41.  
  42.     if (d2.year > d1.year + 1)
  43.         days += (d2.year - d1.year - 1) * 365; // acum fiecare an are 365 asa ca facemc alculul mai usor
  44.     while (d1.year % 4 && d1.year < d2.year)d1.year++; // nu punem pe primul an bisect mai mic decat d2
  45.        
  46.         if (d1.year % 4 == 0) // daca avem un an bisect intre d1 si d2 avem o zi in plus
  47.             days++;
  48.         days += (d2.year - d1.year) / 4; // ca sa calculam mai usor numarul de ani bisecti avem fromula acum difernta pe 4
  49.     return days;// returnam numarul de zile
  50. }
  51.  
  52.  
  53. int main() {
  54.     Data d1, d2; // d0este anul 1 1 1900 anul de referinta
  55.     FILE *f; // variabila de tip fiser
  56.     f = fopen("in.txt", "r");// deschidere in mod citire
  57.     fscanf(f, "%u%u%u", &d1.day, &d1.month, &d1.year);// citim d1 din fiser
  58.     fscanf(f, "%u%u%u", &d2.day, &d2.month, &d2.year);// citim d2 din fiser
  59.     fclose(f); // inchidem fiserul
  60.     f = fopen("out.txt", "w");// deschidem in mod scriere
  61.          d1.day += 7 - CountDays((Data) { 1, 1, 1900 }, d1) % 7; // ne mutam pe prima
  62.          if (d1.day > months[d1.month] + (d1.month == 2 && d1.year % 4 == 0)) { // verificam daca am depasit numarul de zile din luna si daca anul este bisect luam in calcul si februarie
  63.              d1.day = d1.day - months[d1.month] - (d1.month == 2 && d1.year % 4 == 0); // daca am depasit scadem numarul de zile din d1 corespunzator luni curente si am luat incalcul si cazul februarie an bisect
  64.              d1.month++;//incrementam luna
  65.          }
  66.          if (d1.month > 12){ //vedem daca acum luna este mai mare decat 12 in caz ca suntem pe 29.12.2008 daca mai adaugam un 6 depasim anul
  67.              d1.year++;
  68.              d1.month = 1;
  69.     }
  70.      if (d1.year > d2.year){ // verificam daca d1 < d2 toate cazurile
  71.          fprintf(f, "0");
  72.          return 0;
  73.      }
  74.      if (d1.month > d2.month && d1.year == d2.year) {
  75.          fprintf(f, "0");
  76.          return 0;
  77.      }
  78.      if (d1.day > d2.day && d1.month == d2.month && d1.year == d2.year) {
  79.          fprintf(f, "0");
  80.          return 0;
  81.      }
  82.     fprintf(f, "%u", CountDays(d1, d2)/7 + 1);//acum numarul de duminici este numarul de zile dintre cele doua dati pe 7
  83.     fclose(f);// inchidem fiserul
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment