|
From: Carlos R. <cr...@us...> - 2001-11-12 23:10:14
|
Update of /cvsroot/aedii2001/aedii2001/src
In directory usw-pr-cvs1:/tmp/cvs-serv8612/src
Added Files:
dates.c dates.h
Log Message:
modulo de tratamento de datas (agora sim)
--- NEW FILE: dates.c ---
/*
* Faculdade de Ciências e Tecnologia
* Universidade Nova de Lisboa
*
* Algoritmos e Estruturas de Dados II
* 2001 / 2002
*
* Carlos Rodrigues 9566
* Helder Correia 9937
*/
/* Tratamento de datas */
#include <stdio.h>
#include "dates.h"
static int is_leap_year(int year);
static int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int accum_days[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
static int accum_days_leap[] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
static int is_leap_year(int year)
{
return (year % 4) == 0;
}
int is_valid_date(int day, int month, int year)
{
return year >= MIN_YEAR &&
month >= 1 &&
month <= MAX_MONTH &&
day >= 1 &&
day <= ((is_leap_year(year) && month == 2) ? 29 : days_in_month[month - 1]);
}
int date_to_yearday(int day, int month, int year)
{
int *accum_days_p;
if (!is_valid_date(day, month, year)) {
return -1;
}
accum_days_p = is_leap_year(year) ? accum_days_leap : accum_days;
return accum_days_p[month - 1] + day;
}
int yearday_to_date(int yearday, int year, int *day, int *month)
{
int i;
int *accum_days_p;
if (yearday < 0 || yearday > is_leap_year(year) ? MAX_DAYS_YEAR_LEAP : MAX_DAYS_YEAR ) {
return -1;
}
accum_days_p = is_leap_year(year) ? accum_days_leap : accum_days;
for (i = 0; i < MAX_MONTH; i++) {
if (yearday <= accum_days_p[i]) {
break;
}
}
*month = i;
*day = yearday - accum_days_p[i - 1];
return 0;
}
/*
int main(int argc, char *argv[])
{
int d, m, y, i;
scanf("%d %d %d", &d, &m, &y);
i = date_to_yearday(d, m, y);
printf("Day %d\n", i);
yearday_to_date(i, y, &d, &m);
printf("Is date: %d %d %d\n", d, m, y);
return 0;
}
*/
/* EOF - dates.c */
--- NEW FILE: dates.h ---
/*
* Faculdade de Ciências e Tecnologia
* Universidade Nova de Lisboa
*
* Algoritmos e Estruturas de Dados II
* 2001 / 2002
*
* Carlos Rodrigues 9566
* Helder Correia 9937
*/
/* Tratamento de datas */
#ifndef _DATES_H
#define _DATES_H
#define MAX_DAY 31
#define MAX_MONTH 12
#define MIN_YEAR 1901
#define MAX_DAYS_YEAR 365
#define MAX_DAYS_YEAR_LEAP 366
int is_valid_date(int day, int month, int year);
int date_to_yearday(int day, int month, int year);
int yearday_to_date(int yearday, int year, int *day, int *month);
#endif /* _DATES_H */
|