#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum month {
JANUARY=1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBRE, OCTOBER, NOVEMBER, DECEMBER
};
int is_leap(int year)
{
return (year%4 == 0 && year%100 != 0) || year%400 == 0;
}
int days_count(enum month m, int year)
{
switch (m) {
case JANUARY:
case MARCH:
case MAY:
case JULY:
case AUGUST:
case OCTOBER:
case DECEMBER:
return 31;
case FEBRUARY:
return is_leap(year) ? 29 : 28;
case APRIL:
case JUNE:
case SEPTEMBRE:
case NOVEMBER:
return 30;
default:
return -1;
}
}
int days_count2(enum month month, int year)
{
int day_by_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month < JANUARY || month > DECEMBER)
return -1;
days = day_by_month[month - 1];
if (days < 30)
days = is_leap(year) ? 29 : 28;
return days;
}
int exo1(int argc, char* argv[])
{
int month = (argc > 1) ? atoi(argv[1]) : 4;
int year = (argc > 2) ? atoi(argv[2]) : 2013;
printf("l'annee %d %s bissextile", year, is_leap(year) ? "est" : "n'est pas");
printf("le mois '%d' a '%d' jours\n", month, days_count(month, year));
return 0;
}
void print_ascii_table_1()
{
printf("Char.\tdec.\thex\toct\n");
for (int i = 'A'; i <= 'Z'; i = i + 1)
printf("%c\t%d\t%x\t%o\n", i, i, i, i);
}
void print_ascii_table_2(int n)
{
if (!n) n = 1;
for (int i = 0; i < n; i = i + 1)
printf("Char.\tdec.\thex\toct\t");
printf("\n");
for (int i = 0x20; i < 0x80; i = i + 1) {
printf("%c\t%d\t%x\t%o", i, i, i, i);
printf((i % n == n - 1 || i == 0x80) ? "\n" : "\t");
}
}
void print_ascii_table_3(int n, int min, int max)
{
if (n < 1) n = 1;
if (n > (max - min))
n = max - min;
for (int i = 0; i < n ; i = i + 1)
printf("Char.\tdec.\thex\toct%s",
(i % n == n - 1) ? "\n" : "\t");
for (int i = min; i <= max; i = i + 1) {
int c = (i < 0x20 || i > 0x80) ? ' ' : i;
printf("%c\t%d\t%x\t%o", c, i, i, i);
printf((i % n == n - 1 || i == max) ? "\n" : "\t");
}
}
int exo2(int argc, char* argv[])
{
int nb = 4;
int min = 32;
int max = 255;
if (argc > 1)
nb = atoi(argv[argc - 1]);
for (int i = 1; i < argc - 1; i = i + 1)
if (strcmp(argv[i], "--min") == 0) {
i = i + 1;
min = atoi(argv[i]);
} else if (strcmp(argv[i], "--max") == 0) {
i = i + 1;
max = atoi(argv[i]);
} else
nb = atoi(argv[i]);
printf("%d %d %d \n", nb, min, max);
print_ascii_table_1();
printf("\n");
print_ascii_table_2(nb);
printf("\n");
print_ascii_table_3(nb, min, max);
printf("\n");
return EXIT_SUCCESS;
}
int bit_count_1(unsigned long long nb)
{
int count = 0;
for (int j = 0; j < sizeof(nb)*8; j = j + 1) {
if (nb & (1LL << j))
count = count + 1;
}
return count;
}
int bit_count_2(unsigned long long nb)
{
int count = 0;
while (nb) {
if (nb & 1)
count = count + 1;
nb = nb >> 1;
}
return count;
}
int bit_count_2_2(long long nb)
{
int count = 0;
if (nb < 0) {
nb ^= 1 << (sizeof(nb)*8 - 1);
count = count + 1;
}
while (nb) {
if (nb & 1)
count = count + 1;
nb = nb >> 1;
}
return count;
}
int bit_count_3(long long nb)
{
int count = 0;
while (nb) {
count = count + 1;
nb = nb ^ (nb & -nb);
}
return count;
}
int bit_count_4(unsigned i)
{
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
int exo3(int argc, char *argv[])
{
for (int i = 1; i < argc; i = i + 1) {
long long nb = atoll(argv[i]);
printf("%lld (%llx) contient %d %d %d bits a 1\n",
nb, nb,
bit_count_1(nb), bit_count_2(nb), bit_count_3(nb));
}
return EXIT_SUCCESS;
}