Compare commits

..

4 Commits

Author SHA1 Message Date
a2d27a2ea8 first version of the reading cow 2026-04-22 14:04:38 +02:00
6620de21ad created animated cow 2026-04-22 13:57:49 +02:00
ae219bbff4 Q2 2026-04-22 13:52:01 +02:00
528de80d51 first question of C 2026-04-22 13:49:33 +02:00
6 changed files with 259 additions and 12 deletions

View File

@@ -36,11 +36,11 @@
## 4. C Programming
- [ ] **`newcow.c`**
- [ ] Create `affiche_vache` function: displays a cow without a speech bubble.
- [ ] Compile and run.
- [x] **`newcow.c`**
- [x] Create `affiche_vache` function: displays a cow without a speech bubble.
- [x] Compile and run.
- [ ] **Add Features**
- [ ] Support `-e`/ `--eyes` option to change cow's eyes.
- [x] Support `-e`/ `--eyes` option to change cow's eyes.
- [ ] Implement other options from `cowsay` manpage.
- [ ] **New Feature**
- [ ] Implement a new feature (e.g., `--tail L` to adjust tail length, or display a herd).

1
file Normal file
View File

@@ -0,0 +1 @@
bonjour

View File

@@ -1,12 +1,23 @@
#include <stdio.h>
#include <string.h>
int main() {
const char *cow = " ^__^\n"
" (oo)\\_______\n"
" (__)\\ )\\/\\\n"
" ||----w |\n"
" || ||\n";
void affiche_vache(char yeux[3]) {
printf(" \\ ^__^\n");
printf(" \\ (%s)\\_______\n", yeux);
printf(" (__)\\ )\\/\\\n");
printf(" ||----w |\n");
printf(" || ||\n");
}
printf("%s", cow);
int main(int argc, char *argv[]) {
char yeux[3] = "oo";
if (argc == 3 &&
(strcmp(argv[1], "-e") == 0 || strcmp(argv[1], "--eyes") == 0)) {
strncpy(yeux, argv[2], 2);
yeux[2] = '\0';
}
affiche_vache(yeux);
return 0;
}

101
src/C/newcow2.c Normal file
View File

@@ -0,0 +1,101 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void update() { printf("\033[H\033[J"); }
void gotoxy(int x, int y) { printf("\033[%d;%dH", y, x); }
void affiche_vache(int x, int y, const char *yeux, const char *langue) {
gotoxy(x, y);
printf(" \\ ^__^\n");
gotoxy(x, y + 1);
printf(" \\ (%s)\\_______\n", yeux);
gotoxy(x, y + 2);
printf(" (__)\\ )\\/\\\\\n");
gotoxy(x, y + 3);
printf(" %s ||----w |\n", langue);
gotoxy(x, y + 4);
printf(" || ||\n");
fflush(stdout);
}
void animation_blink() {
for (int i = 0; i < 12; i++) {
update();
if (i % 4 == 0)
affiche_vache(5, 3, "--", " ");
else
affiche_vache(5, 3, "oo", " ");
usleep(250000);
}
}
void animation_tongue() {
for (int i = 0; i < 12; i++) {
update();
if (i % 2 == 0)
affiche_vache(5, 3, "oo", "U ");
else
affiche_vache(5, 3, "oo", " ");
usleep(250000);
}
}
void animation_walk() {
for (int x = 1; x <= 40; x += 2) {
update();
if ((x / 2) % 2 == 0)
affiche_vache(x, 3, "oo", " ");
else
affiche_vache(x, 3, "OO", " ");
usleep(120000);
}
}
void animation_bounce() {
int x = 20;
for (int i = 0; i < 18; i++) {
update();
int y;
if (i % 4 == 0 || i % 4 == 2)
y = 3;
else
y = 2;
affiche_vache(x, y, "oo", " ");
usleep(180000);
}
}
void usage(char *prog) {
printf("Usage : %s [blink|tongue|walk|bounce]\n", prog);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
usage(argv[0]);
return 1;
}
if (strcmp(argv[1], "blink") == 0) {
animation_blink();
} else if (strcmp(argv[1], "tongue") == 0) {
animation_tongue();
} else if (strcmp(argv[1], "walk") == 0) {
animation_walk();
} else if (strcmp(argv[1], "bounce") == 0) {
animation_bounce();
} else {
usage(argv[0]);
return 1;
}
gotoxy(1, 10);
return 0;
}

134
src/C/reading_cow.c Normal file
View File

@@ -0,0 +1,134 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_TEXT 1024
void update() { printf("\033[H\033[J"); }
void gotoxy(int x, int y) { printf("\033[%d;%dH", y, x); }
void print_repeat(char c, int n) {
for (int i = 0; i < n; i++) {
putchar(c);
}
}
void affiche_bulle(const char *texte) {
int len = strlen(texte);
putchar(' ');
print_repeat('_', len + 2);
putchar('\n');
printf("< %s >\n", texte);
putchar(' ');
print_repeat('-', len + 2);
putchar('\n');
}
void affiche_vache(char lettre_en_bouche) {
printf(" \\ ^__^\n");
if (lettre_en_bouche == '\0')
printf(" \\ (oo)\\_______\n");
else
printf(" \\ (%c%c)\\_______\n", lettre_en_bouche,
lettre_en_bouche);
printf(" (__)\\ )\\/\\\\\n");
printf(" ||----w |\n");
printf(" || ||\n");
}
void affiche_scene(const char *deja_lu, char lettre_en_bouche) {
update();
affiche_bulle(deja_lu);
affiche_vache(lettre_en_bouche);
fflush(stdout);
}
void usage(const char *prog) {
fprintf(stderr, "Usage: %s [-i intervalle] fichier\n", prog);
fprintf(stderr, " ou: %s [-i intervalle] -\n", prog);
fprintf(stderr, "Le fichier '-' signifie lecture depuis stdin.\n");
}
int main(int argc, char *argv[]) {
double intervalle = 1.0;
char *nom_fichier = NULL;
FILE *f = NULL;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-i") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "erreur : -i attend une valeur.\n");
usage(argv[0]);
return 1;
}
intervalle = atof(argv[i + 1]);
if (intervalle < 0) {
fprintf(stderr, "erreur : l'intervalle doit etre >= 0.\n");
return 1;
}
i++;
} else if (nom_fichier == NULL) {
nom_fichier = argv[i];
} else {
fprintf(stderr, "erreur : trop d'arguments.\n");
usage(argv[0]);
return 1;
}
}
if (nom_fichier == NULL) {
usage(argv[0]);
return 1;
}
if (strcmp(nom_fichier, "-") == 0) {
f = stdin;
} else {
f = fopen(nom_fichier, "r");
if (f == NULL) {
perror("erreur ouverture fichier");
return 1;
}
}
char texte_lu[MAX_TEXT];
int taille = 0;
texte_lu[0] = '\0';
int c;
useconds_t pause_us =
(useconds_t)(intervalle * 1000000.0); // Car usleep est en ms.
while ((c = fgetc(f)) != EOF) {
if (taille >= MAX_TEXT - 1) {
fprintf(stderr, "erreur : texte trop long (max %d caracteres).\n",
MAX_TEXT - 1);
if (f != stdin)
fclose(f);
return 1;
}
affiche_scene(texte_lu, (char)c);
usleep(pause_us);
texte_lu[taille++] = (char)c;
texte_lu[taille] = '\0';
affiche_scene(texte_lu, '\0');
usleep(pause_us);
}
if (f != stdin) {
fclose(f);
}
gotoxy(1, 10);
return 0;
}

View File

@@ -14,7 +14,7 @@ if [ $# -eq 0 ]; then
((rb < 32742)) && r2=$((rb % 51)) && break
done
result=$((r1 + r2))
message="$r1 + $r2 = $result"
message="$r1 + $r2"
else
expression="$1"
result=$(echo "scale=2; $expression" | bc -l)