1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| /* main.c by ashgenesis
ashgenesis[at]gmail[dot]com*/
/* Pour ne pas compiler avec un compilateur C++ */
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h> /* LONG_MIN et LONG_MAX */
#include <errno.h> /* gestion d'erreur strtol */
/* macros ============================================================== */
/* prototype =========================================================== */
/* L'usage des prototypes séparés n'est pas justifié ici, car c'est
un mini projet a un seul fichier. */
static void use (char name[]);
static void isbissextil (int y);
static int showcalendar(void);
static int myfirstdays(int d, int m, int y);
static int affichage (int d, int m, int y, int fd);
static int changeparam (char *str);
/* constants =========================================================== */
/* Pour les chaines longues et irrégulières, l'usage de tableaux de
pointeurs sur chaines permet une optimisation de l'espace mémoire. */
static char const *monthnames [] = {"Janvier", "Fevrier", "Mars",
"Avril", "Mais", "Juin", "Juillet", "Aout", "Septembre", "Octobre",
"Novembre", "Decembre"};
static char const daynames[] [3]= {"Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"};
/* types =============================================================== */
/* structures ========================================================== */
/* private variables =================================================== */
/* Vu qu'il y a les 12 initialiseurs, il n'est pas indispensable de
préciser la taille du tableau.*/
static int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/* private functions =================================================== */
/* Regle d'usage du programme */
static void use (char name[])
{
printf ("Utilisation : %s\n", name);
printf ("Utilisation : %s [day] [month] [year]\n", name);
printf ("L'annee ne doit pas etre en dessous de 1970\n");
}
/* Test si l'année est bissextile */
static void isbissextil (int y)
{
if ((( y % 4 == 0) && ( y % 100 != 0)) || (y % 400 == 0))
{
days[1] = 29;
}
}
/* Affiche le calendrier du mois courant */
static int showcalendar(void){
time_t now = time (NULL);
struct tm tm_now = *localtime (&now);
char day[10];
char month[10];
char year[10];
strftime (day, sizeof day, "%d", &tm_now);
strftime (month, sizeof month, "%m", &tm_now);
strftime (year, sizeof year, "%Y", &tm_now);
int d = changeparam (day);
int m = changeparam (month);
int y = changeparam (year);
isbissextil(y);
int firstday = myfirstdays(1, m, y);
affichage (d, m, y, firstday);
return 0;
}
/* Quel jour correspond à la date */
static int myfirstdays(int d, int m, int y)
{
int jour;
jour = 367*y-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d+1721029;
jour++;
return (jour%7)+1;
}
/* affichage du calendrier en console */
static int affichage (int d, int m, int y, int fd)
{
int j;
printf (" \033[1m%s %d \n", monthnames[m-1], y);
for (j=0; j<7; j++)
{
printf ("\033[0m%s ", daynames[j] );
}
printf ("\n");
for (j=0; j<fd-1; j++)
{
printf (" ");
}
for (j=1 ; j <= days[m-1]; j++)
{
if(j == d){
printf ("\033[31m%2d ", j);
}
else
{
printf ("\033[0m%2d ", j);
}
if( fd > 6 )
{
printf("\n");
fd = 0;
}
fd++;
}
printf("\n");
return 0;
}
/* converti les parametres en int */
static int changeparam (char *str)
{
int val;
int base = 10;
char *end;
errno = 0;
val = strtol (str, &end, base);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0))
{
perror ("strtol");
exit (EXIT_FAILURE);
}
if (end == str)
{
fprintf(stderr, "Pas de chiffre trouvé\n");
exit(EXIT_FAILURE);
}
return val;
}
/* internal public functions =========================================== */
/* entry points ======================================================== */
/* public variables ==================================================== */
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int err = 0;
if (argc == 1)
{
showcalendar();
}
else if (argc == 4)
{
int year = changeparam (argv[3]);
int month = changeparam (argv[2]);
int day = changeparam (argv[1]);
/* Test si mois est bien compris entre 1 et 12
et si l'année est strictement supérieur a 1971 */
if (month > 12 || month < 1 || year < 1971 || day > 31 || day < 1)
{
err = 1;
}
else
{
isbissextil (year);
int firstday = myfirstdays(1, month, year);
affichage (day, month, year, firstday);
}
}
else
{
use (argv[0]);
}
#if defined (WIN32)
system("PAUSE");
#endif
return 0;
} |
Partager