Skip to content

Instantly share code, notes, and snippets.

@soltys
Created December 15, 2012 13:29
Show Gist options
  • Save soltys/4294990 to your computer and use it in GitHub Desktop.
Save soltys/4294990 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <ctype.h>
int is_leap_year(int year);
int day_of_year(int day, int month, int year);
int main() {
int day = 0, month = 0, year = 0;
printf("Podaj date (dd/mm/rrrr): ");
scanf("%d/%d/%d",&day,&month,&year);
printf("Dzień roku: %d",day_of_year(day,month,year));
return 0;
}
int is_leap_year(int year){
return (year % 4 ==0 && year %100 != 0) || year %400 ==0;
}
int get_month_days(int month,int year)
{
if(month == 2)
return 28+is_leap_year(year);
unsigned int magic_number=0xAD5;
magic_number = magic_number >> (month -1);
if(magic_number & 1)
return 31;
else
return 30;
}
int day_of_year(int day, int month, int year)
{
int number_days=0,i;
for (i=1;i<month;i++)
{
number_days+=get_month_days(i,year);
}
return number_days+day;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment