Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- static unsigned months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // pastram dimensiunea fiecariei luni
- typedef struct {
- unsigned day, month, year;
- } Data; // folosim o lista sa ne organizam mai usor
- unsigned CountDays(Data d1, Data d2) { // folosim functia pentru a numara cate zile sunte intre 1900, d1 si d1/d2
- unsigned days = 0, i;
- if (d1.year == d2.year) {
- if (d1.month < d2.month) {
- days = d2.day;
- for (i = d2.month - 1; i > d1.month; i--)
- days += months[i] + (d2.month == 2 && d2.year % 4 == 0);
- days += months[i] - d1.day;
- return days;
- }
- return d2.day - d1.day;
- }
- if (d1.month == 1) { // daca e prima luna adaugam numarul de zile
- days += months[1] - d1.day + 1;
- d1.day = 1; // ziua 1
- d1.month = 2; // luna devine 2
- }
- if ((d1.month == 2 && (d1.year % 4) == 0)) days++;// daca avem februruiare an bisect
- days += months[d1.month] - d1.day ; // aduagam celelate zile din luna curenta
- for (i = d1.month + 1; i <= 12; i++) // adaugam zilele pana la sfarsitul anului
- days += months[i];
- days += d2.day - 1; // adunam zilele din luna curenta
- d2.month--;// scadem luna
- for (i = d2.month; i > 2; i--) // adaugam ziele pentru lunile anterioarene oprim la februare pentru a stabil daca anu este bisect
- days += months[i];
- if (d2.month == 2 && d2.year % 4 == 0) days++;//daca anul este bisect avem o zi in plus
- if (d2.month >= 2) days += months[2]; // daca luna este cel puntin februaie
- if (d2.month >= 1) days += months[1]; // daca luna este cel putin ianuarie
- if (d2.year > d1.year + 1)
- days += (d2.year - d1.year - 1) * 365; // acum fiecare an are 365 asa ca facemc alculul mai usor
- while (d1.year % 4 && d1.year < d2.year)d1.year++; // nu punem pe primul an bisect mai mic decat d2
- if (d1.year % 4 == 0) // daca avem un an bisect intre d1 si d2 avem o zi in plus
- days++;
- days += (d2.year - d1.year) / 4; // ca sa calculam mai usor numarul de ani bisecti avem fromula acum difernta pe 4
- return days;// returnam numarul de zile
- }
- int main() {
- Data d1, d2; // d0este anul 1 1 1900 anul de referinta
- FILE *f; // variabila de tip fiser
- f = fopen("in.txt", "r");// deschidere in mod citire
- fscanf(f, "%u%u%u", &d1.day, &d1.month, &d1.year);// citim d1 din fiser
- fscanf(f, "%u%u%u", &d2.day, &d2.month, &d2.year);// citim d2 din fiser
- fclose(f); // inchidem fiserul
- f = fopen("out.txt", "w");// deschidem in mod scriere
- d1.day += 7 - CountDays((Data) { 1, 1, 1900 }, d1) % 7; // ne mutam pe prima
- 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
- 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
- d1.month++;//incrementam luna
- }
- 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
- d1.year++;
- d1.month = 1;
- }
- if (d1.year > d2.year){ // verificam daca d1 < d2 toate cazurile
- fprintf(f, "0");
- return 0;
- }
- if (d1.month > d2.month && d1.year == d2.year) {
- fprintf(f, "0");
- return 0;
- }
- if (d1.day > d2.day && d1.month == d2.month && d1.year == d2.year) {
- fprintf(f, "0");
- return 0;
- }
- fprintf(f, "%u", CountDays(d1, d2)/7 + 1);//acum numarul de duminici este numarul de zile dintre cele doua dati pe 7
- fclose(f);// inchidem fiserul
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment