finished last C file
This commit is contained in:
2
TODO.md
2
TODO.md
@@ -47,7 +47,7 @@
|
|||||||
- [x] **`wildcow`**
|
- [x] **`wildcow`**
|
||||||
- [~] Test and explain `update()` and `gotoxy()`.
|
- [~] Test and explain `update()` and `gotoxy()`.
|
||||||
- [x] Create an animated cow using `update()`, `gotoxy()`, and `sleep()`.
|
- [x] Create an animated cow using `update()`, `gotoxy()`, and `sleep()`.
|
||||||
- [ ] **`reading_cow`**
|
- [x] **`reading_cow`**
|
||||||
- [x] Cow "reads" a file character by character, displaying each in its mouth before "swallowing".
|
- [x] Cow "reads" a file character by character, displaying each in its mouth before "swallowing".
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -5,15 +5,14 @@
|
|||||||
|
|
||||||
#define MAX_TEXT 1024
|
#define MAX_TEXT 1024
|
||||||
|
|
||||||
void update() { printf("\033[H\033[J"); }
|
void update(void) { printf("\033[H\033[J"); }
|
||||||
|
|
||||||
void gotoxy(int x, int y) { printf("\033[%d;%dH", y, x); }
|
void gotoxy(int x, int y) { printf("\033[%d;%dH", y, x); }
|
||||||
|
|
||||||
void print_repeat(char c, int n) {
|
void print_repeat(char c, int n) {
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++)
|
||||||
putchar(c);
|
putchar(c);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void affiche_bulle(const char *texte) {
|
void affiche_bulle(const char *texte) {
|
||||||
int len = strlen(texte);
|
int len = strlen(texte);
|
||||||
@@ -31,15 +30,14 @@ void affiche_bulle(const char *texte) {
|
|||||||
|
|
||||||
void affiche_vache(char lettre_en_bouche) {
|
void affiche_vache(char lettre_en_bouche) {
|
||||||
printf(" \\ ^__^\n");
|
printf(" \\ ^__^\n");
|
||||||
|
printf(" \\ (oo)\\_______\n");
|
||||||
|
printf(" (__)\\ )\\/\\\\\n");
|
||||||
|
|
||||||
if (lettre_en_bouche == '\0')
|
if (lettre_en_bouche == '\0')
|
||||||
printf(" \\ (oo)\\_______\n");
|
printf(" \\_ ||----w |\n");
|
||||||
else
|
else
|
||||||
printf(" \\ (%c%c)\\_______\n", lettre_en_bouche,
|
printf(" %c ||----w |\n", lettre_en_bouche);
|
||||||
lettre_en_bouche);
|
|
||||||
|
|
||||||
printf(" (__)\\ )\\/\\\\\n");
|
|
||||||
printf(" ||----w |\n");
|
|
||||||
printf(" || ||\n");
|
printf(" || ||\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,9 +49,8 @@ void affiche_scene(const char *deja_lu, char lettre_en_bouche) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void usage(const char *prog) {
|
void usage(const char *prog) {
|
||||||
fprintf(stderr, "Usage: %s [-i intervalle] fichier\n", prog);
|
fprintf(stderr, "Usage: %s [--i intervalle] fichier\n", prog);
|
||||||
fprintf(stderr, " ou: %s [-i intervalle] -\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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
@@ -62,19 +59,19 @@ int main(int argc, char *argv[]) {
|
|||||||
FILE *f = NULL;
|
FILE *f = NULL;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
if (strcmp(argv[i], "-i") == 0) {
|
if (strcmp(argv[i], "--i") == 0) {
|
||||||
if (i + 1 >= argc) {
|
if (i + 1 >= argc) {
|
||||||
fprintf(stderr, "erreur : -i attend une valeur.\n");
|
fprintf(stderr, "erreur : --i attend une valeur.\n");
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
intervalle = atof(argv[i + 1]);
|
intervalle = atof(argv[++i]);
|
||||||
|
|
||||||
if (intervalle < 0) {
|
if (intervalle < 0) {
|
||||||
fprintf(stderr, "erreur : l'intervalle doit etre >= 0.\n");
|
fprintf(stderr, "erreur : l'intervalle doit etre >= 0.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
i++;
|
|
||||||
} else if (nom_fichier == NULL) {
|
} else if (nom_fichier == NULL) {
|
||||||
nom_fichier = argv[i];
|
nom_fichier = argv[i];
|
||||||
} else {
|
} else {
|
||||||
@@ -104,8 +101,7 @@ int main(int argc, char *argv[]) {
|
|||||||
texte_lu[0] = '\0';
|
texte_lu[0] = '\0';
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
useconds_t pause_us =
|
useconds_t pause_us = (useconds_t)(intervalle * 1000000.0);
|
||||||
(useconds_t)(intervalle * 1000000.0); // Car usleep est en ms.
|
|
||||||
|
|
||||||
while ((c = fgetc(f)) != EOF) {
|
while ((c = fgetc(f)) != EOF) {
|
||||||
if (taille >= MAX_TEXT - 1) {
|
if (taille >= MAX_TEXT - 1) {
|
||||||
@@ -116,18 +112,25 @@ int main(int argc, char *argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (c == '\n') {
|
||||||
|
texte_lu[taille++] = ' ';
|
||||||
|
texte_lu[taille] = '\0';
|
||||||
|
affiche_scene(texte_lu, '\0');
|
||||||
|
usleep(pause_us);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
affiche_scene(texte_lu, (char)c);
|
affiche_scene(texte_lu, (char)c);
|
||||||
usleep(pause_us);
|
usleep(pause_us);
|
||||||
|
|
||||||
texte_lu[taille++] = (char)c;
|
texte_lu[taille++] = (char)c;
|
||||||
texte_lu[taille] = '\0';
|
texte_lu[taille] = '\0';
|
||||||
affiche_scene(texte_lu, '\0');
|
|
||||||
usleep(pause_us);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (f != stdin) {
|
affiche_scene(texte_lu, '\0');
|
||||||
|
|
||||||
|
if (f != stdin)
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
|
||||||
|
|
||||||
gotoxy(1, 10);
|
gotoxy(1, 10);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user