diff --git a/Révisions/CM/debordement.c b/Révisions/CM/debordement.c new file mode 100644 index 0000000..2611030 --- /dev/null +++ b/Révisions/CM/debordement.c @@ -0,0 +1,20 @@ +#include +#include + +unsigned long int factorielle(unsigned long int n) { + if (n > 1) { + return n * factorielle(n-1); + } else return 1UL; +} + +int main(int argc, char *argv[]) { + unsigned long int l; + int n = 1; + + printf("%d\n", sizeof(l)); + while (factorielle(n+1) >= factorielle(n)) n++; + l = factorielle(n); + + printf("valeur de n = %d, valeur de %d! = %lu\n", n, n, l); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/Révisions/CM/parenthesage.c b/Révisions/CM/parenthesage.c new file mode 100644 index 0000000..686b8d9 --- /dev/null +++ b/Révisions/CM/parenthesage.c @@ -0,0 +1,34 @@ +#include +#include +#include + +int main(int argc, char* argv[]) { + if (argc < 2) { + puts("Veuillez préciser une expression arithmétique..."); + return EXIT_FAILURE; + } + + char* result = (char*)calloc(strlen(argv[1])+1, sizeof(char)); + int length = 0, nb_ouvrante = 0, nb_fermante = 0; + + for (int i = 0; i < strlen(argv[1]); i++) { + if (argv[1][i] == '(' || argv[1][i] == ')') { + result[length] = argv[1][i]; + length++; + + if (argv[1][i] == '(') nb_ouvrante++; + else nb_fermante++; + } + } + + int equite = 0; + for (int i = 0; i < strlen(result); i++) { + if (result[i] == '(') equite++; + else equite--; + + if (equite < 0) break; + } + + printf("%s %s %s\n", result, nb_ouvrante == nb_fermante ? "OK" : "NOK", equite == 0 ? "valide" : "non valide"); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/Révisions/CM/taille.c b/Révisions/CM/taille.c new file mode 100644 index 0000000..59619d2 --- /dev/null +++ b/Révisions/CM/taille.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + struct stat buffer; + if (argc < 2) { + puts("Veuillez préciser un fichier."); + return EXIT_FAILURE; + } + + stat(argv[1], &buffer); + printf("La taille de %s est de %d octets.\n", argv[1], buffer.st_size); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/Révisions/Fichiers/challenger.c b/Révisions/Fichiers/challenger.c new file mode 100644 index 0000000..9a475f7 --- /dev/null +++ b/Révisions/Fichiers/challenger.c @@ -0,0 +1,80 @@ +#include +#include +#include + +typedef struct joueur { + int score; + char nom[4]; +} joueur; + +joueur* readTop(char* filename) { + joueur* classement = (joueur*)calloc(10, sizeof(joueur)); + + FILE* f = fopen(filename, "r"); + + if (f) { + for (int i = 0; i < 10; i++) { + if (!feof(f)) { + int score; + char nom[4]; + fread(&score, sizeof(int), 1, f); + fread(nom, sizeof(char), 3, f); + classement[i].score = score; + strcpy(classement[i].nom, nom); + } else break; + } + fclose(f); + } + + return classement; +} + +void showTop(char* filename) { + FILE* f = fopen(filename, "r"); + + if (f) { + for (int i = 0; i < 10; i++) { + if (!feof(f)) { + int score; + char nom[4]; + fread(&score, sizeof(int), 1, f); + fread(nom, sizeof(char), 3, f); + printf("%09d %s\n", score, nom); + } else break; + } + fclose(f); + } +} + +void writeScore(char* filename, char* nom, int score) { + joueur* classement = readTop(filename); + + for (int i = 0; i < 10; i++) { + if (classement[i].score < score) { + for (int j = i; j < 9; j++) { + classement[j+1] = classement[j]; + } + + classement[i].score = score; + strcpy(classement[i].nom, nom); + break; + } + } + + FILE* f = fopen(filename, "w"); + + if (f) { + for (int i = 0; i < 10; i++) { + fwrite(&(classement[i].score), sizeof(int), 1, f); + fwrite(classement[i].nom, sizeof(char), 3, f); + } + + fclose(f); + } +} + +int main(int argc, char* argv[]) { + int score = (int)strtol(argv[1], NULL, 10); + writeScore("top10", argv[2], score); + showTop("top10"); +} \ No newline at end of file diff --git a/Révisions/Fichiers/copie.c b/Révisions/Fichiers/copie.c new file mode 100644 index 0000000..d9b1608 --- /dev/null +++ b/Révisions/Fichiers/copie.c @@ -0,0 +1,18 @@ +#include +#include + +int main(int argc, char* argv[]) { + FILE* f = fopen(argv[1], "r"); + FILE* f2 = fopen(argv[2], "w"); + + if (f && f2) { + while (!feof(f)) { + char c; + fread(&c, sizeof(char), 1, f); + fwrite(&c, sizeof(char), 1, f2); + } + + fclose(f); + fclose(f2); + } +} \ No newline at end of file diff --git a/Révisions/Fichiers/records.c b/Révisions/Fichiers/records.c new file mode 100644 index 0000000..561202f --- /dev/null +++ b/Révisions/Fichiers/records.c @@ -0,0 +1,19 @@ +#include +#include + +int main(void) { + FILE* f = fopen("top10", "r"); + + if (f) { + for (int i = 0; i < 10; i++) { + if (!feof(f)) { + int score; + char nom[4]; + fread(&score, sizeof(int), 1, f); + fread(nom, sizeof(char), 3, f); + printf("%09d %s\n", score, nom); + } else break; + } + fclose(f); + } +} \ No newline at end of file diff --git a/Révisions/Fichiers/taupe10 b/Révisions/Fichiers/taupe10 new file mode 100644 index 0000000..93fda69 Binary files /dev/null and b/Révisions/Fichiers/taupe10 differ diff --git a/Révisions/Fichiers/top10 b/Révisions/Fichiers/top10 new file mode 100644 index 0000000..153e77f Binary files /dev/null and b/Révisions/Fichiers/top10 differ diff --git a/Révisions/Structures/alterations.c b/Révisions/Structures/alterations.c new file mode 100644 index 0000000..f75eb04 --- /dev/null +++ b/Révisions/Structures/alterations.c @@ -0,0 +1,83 @@ +#include +#include +#include + +typedef struct rekt { + int x; + int y; + int w; + int h; +} rekt; + +void drawRekt(rekt rectangle) { + DessinerRectangle(rectangle.x, rectangle.y, rectangle.w, rectangle.h); +} + +rekt makeRekt() { + rekt toto; + char valid = 0; + while(valid != 2) { + if (SourisCliquee()) { + SourisPosition(); + if (valid == 0) { + toto.x = _X; + toto.y = _Y; + valid = 1; + } + else if (valid == 1) { + toto.w = _X - toto.x; + toto.h = _Y - toto.y; + valid = 2; + } + } + } + + if (toto.w < 0) { + toto.w = -toto.w; + toto.x -= toto.w; + } + + if (toto.h < 0) { + toto.h = -toto.h; + toto.y -= toto.h; + } + + return toto; +} + +void translate(rekt* rectangle, int x, int y) { + rectangle->x += x; + rectangle->y += y; +} + +void rotate(rekt* rectangle, int rotation) { + if (rotation == 1) { + int buffer = rectangle->w; + rectangle->w = rectangle->h; + rectangle->h = buffer; + rectangle->x -= rectangle->w; + } else if (rotation == -1) { + int buffer = rectangle->w; + rectangle->w = rectangle->h; + rectangle->h = buffer; + rectangle->y -= rectangle->h; + } +} + +int main(void) { + rekt rectangle_toto = {300, 300, 200, 100}; + rekt rectangle_toto2 = {300, 300, 200, 100}; + + InitialiserGraphique(); + CreerFenetre(100, 100, 1200, 700); + drawRekt(rectangle_toto); + rotate(&rectangle_toto, 1); + drawRekt(rectangle_toto); + + + rotate(&rectangle_toto2, -1); + drawRekt(rectangle_toto2); + drawRekt(makeRekt()); + Touche(); + FermerGraphique(); +} \ No newline at end of file diff --git a/Révisions/Structures/combinaisons.c b/Révisions/Structures/combinaisons.c new file mode 100644 index 0000000..7e4e69d --- /dev/null +++ b/Révisions/Structures/combinaisons.c @@ -0,0 +1,117 @@ +#include +#include +#include + +typedef struct rekt { + int x; + int y; + int w; + int h; +} rekt; + +void drawRekt(rekt rectangle) { + DessinerRectangle(rectangle.x, rectangle.y, rectangle.w, rectangle.h); +} + +int max(int x, int y) { + return x > y ? x : y; +} + +int min(int x, int y) { + return x < y ? x : y; +} + +rekt makeRekt() { + rekt toto; + char valid = 0; + while(valid != 2) { + if (SourisCliquee()) { + SourisPosition(); + if (valid == 0) { + toto.x = _X; + toto.y = _Y; + valid = 1; + } + else if (valid == 1) { + toto.w = _X - toto.x; + toto.h = _Y - toto.y; + valid = 2; + } + } + } + + if (toto.w < 0) { + toto.w = -toto.w; + toto.x -= toto.w; + } + + if (toto.h < 0) { + toto.h = -toto.h; + toto.y -= toto.h; + } + + return toto; +} + +void translate(rekt* rectangle, int x, int y) { + rectangle->x += x; + rectangle->y += y; +} + +void rotate(rekt* rectangle, int rotation) { + if (rotation == 1) { + int buffer = rectangle->w; + rectangle->w = rectangle->h; + rectangle->h = buffer; + rectangle->x -= rectangle->w; + } else if (rotation == -1) { + int buffer = rectangle->w; + rectangle->w = rectangle->h; + rectangle->h = buffer; + rectangle->y -= rectangle->h; + } +} + +rekt intersection(rekt rect1, rekt rect2) { + rekt rect_inter; + rect_inter.x = max(rect1.x, rect2.x); + rect_inter.y = max(rect1.y, rect2.y); + + rect_inter.w = min(rect1.x + rect1.w, rect2.x + rect2.w); + rect_inter.h = min(rect1.y + rect1.h, rect2.y + rect2.h); + + rect_inter.w -= rect_inter.x; + rect_inter.h -= rect_inter.y; + + return rect_inter; +} + +rekt mariage(rekt rect1, rekt rect2) { + rekt rect_inter; + rect_inter.x = min(rect1.x, rect2.x); + rect_inter.y = min(rect1.y, rect2.y); + + rect_inter.w = max(rect1.x + rect1.w, rect2.x + rect2.w); + rect_inter.h = max(rect1.y + rect1.h, rect2.y + rect2.h); + + rect_inter.w -= rect_inter.x; + rect_inter.h -= rect_inter.y; + + return rect_inter; +} + +int main(void) { + rekt rectangle_toto1 = {300, 300, 300, 200}; + rekt rectangle_toto2 = {350, 400, 200, 250}; + + InitialiserGraphique(); + CreerFenetre(100, 100, 1200, 700); + drawRekt(rectangle_toto1); + drawRekt(rectangle_toto2); + ChoisirCouleurDessin(CouleurParComposante(255, 0, 0)); + drawRekt(intersection(rectangle_toto1, rectangle_toto2)); + ChoisirCouleurDessin(CouleurParComposante(0, 0, 255)); + drawRekt(mariage(rectangle_toto1, rectangle_toto2)); + Touche(); + FermerGraphique(); +} \ No newline at end of file diff --git a/Révisions/Structures/complexes.c b/Révisions/Structures/complexes.c new file mode 100644 index 0000000..8cb4ed5 --- /dev/null +++ b/Révisions/Structures/complexes.c @@ -0,0 +1,23 @@ +#include +#include +#include + +struct complexe { + double x; + double y; +}; + +typedef struct complexe complexe; + +double module(complexe z) { + return sqrt(pow(z.x, 2) + pow(z.y, 2)); +} + +complexe conjugue(complexe z) { + z.y = -z.y; + return z; +} + +int main(void) { + +} \ No newline at end of file diff --git a/Révisions/Structures/date.c b/Révisions/Structures/date.c new file mode 100644 index 0000000..103284a --- /dev/null +++ b/Révisions/Structures/date.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int main(void) { + time_t stamp = time(NULL); + struct tm* temps = localtime(&stamp); + + printf("Nous sommes le : %4d-%02d-%02d\n", temps->tm_year + 1900, temps->tm_mon + 1, temps->tm_mday); +} \ No newline at end of file diff --git a/Révisions/Structures/rectangles.c b/Révisions/Structures/rectangles.c new file mode 100644 index 0000000..2bf8453 --- /dev/null +++ b/Révisions/Structures/rectangles.c @@ -0,0 +1,57 @@ +#include +#include +#include + +typedef struct rekt { + int x; + int y; + int w; + int h; +} rekt; + +void drawRekt(rekt rectangle) { + DessinerRectangle(rectangle.x, rectangle.y, rectangle.w, rectangle.h); +} + +rekt makeRekt() { + rekt toto; + char valid = 0; + while(valid != 2) { + if (SourisCliquee()) { + SourisPosition(); + if (valid == 0) { + toto.x = _X; + toto.y = _Y; + valid = 1; + } + else if (valid == 1) { + toto.w = _X - toto.x; + toto.h = _Y - toto.y; + valid = 2; + } + } + } + + if (toto.w < 0) { + toto.w = -toto.w; + toto.x -= toto.w; + } + + if (toto.h < 0) { + toto.h = -toto.h; + toto.y -= toto.h; + } + + return toto; +} + +int main(void) { + rekt rectangle_toto = {100, 100, 200, 50}; + + InitialiserGraphique(); + CreerFenetre(100, 100, 1200, 700); + drawRekt(rectangle_toto); + drawRekt(makeRekt()); + Touche(); + FermerGraphique(); +} \ No newline at end of file diff --git a/Révisions/Structures/tailles.c b/Révisions/Structures/tailles.c new file mode 100644 index 0000000..f5ef687 --- /dev/null +++ b/Révisions/Structures/tailles.c @@ -0,0 +1,14 @@ +#include +#include + +struct toto { + int a; + char b; + char c; +}; + +typedef struct toto toto; + +int main(void) { + printf("%d\n", sizeof(toto)); +} \ No newline at end of file