Révisions

This commit is contained in:
HORVILLE 2022-01-25 12:00:33 +01:00
parent c30ba3ccdf
commit 64944874bc
14 changed files with 492 additions and 0 deletions

View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

17
Révisions/CM/taille.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
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;
}

View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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");
}

View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
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);
}
}

View File

@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
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);
}
}

BIN
Révisions/Fichiers/taupe10 Normal file

Binary file not shown.

BIN
Révisions/Fichiers/top10 Normal file

Binary file not shown.

View File

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
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();
}

View File

@ -0,0 +1,117 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
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();
}

View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
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) {
}

View File

@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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);
}

View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
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();
}

View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
struct toto {
int a;
char b;
char c;
};
typedef struct toto toto;
int main(void) {
printf("%d\n", sizeof(toto));
}