diff options
| author | Anhgelus Morhtuuzh <anhgelus@anhgelus.world> | 2025-01-31 15:39:30 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <anhgelus@anhgelus.world> | 2025-01-31 15:39:30 +0100 |
| commit | d7c69934248f3fab4988c0d37c11feba25d653b8 (patch) | |
| tree | 6c396257cb21374e13a26b8f78c05353806220bc /semestre 2/informatique/tme | |
| parent | ed631f52ee7af474625e37c6855c7c55903317fc (diff) | |
Ajout du début du deuxième semestre
Diffstat (limited to 'semestre 2/informatique/tme')
9 files changed, 272 insertions, 0 deletions
diff --git a/semestre 2/informatique/tme/semaine1/03_jeuTest.c b/semestre 2/informatique/tme/semaine1/03_jeuTest.c new file mode 100644 index 0000000..1099c11 --- /dev/null +++ b/semestre 2/informatique/tme/semaine1/03_jeuTest.c @@ -0,0 +1,18 @@ +#include <assert.h> + +int alternative(int n1, int n2, int n3){ + int res; + if (n1 > 8) res = 3; + else if (n3 == 20) res = 2; + else if (n2 >= 10 && n3 >= 10) res = 1; + else res = 0; + return res; +} + +int main(){ + assert(alternative(-4, 3, 20) == 2); // teste de la branche n3 == 20 + assert(alternative(9,-2,20) == 3); // teste de la branche n1 > 8 + assert(alternative(-5, 15, 1098765) == 1); // teste de la branche n2 >= 10 && n3 >= 10 + assert(alternative(4, 8, 23) == 0); // teste des autres cas + return 0; +} diff --git a/semestre 2/informatique/tme/semaine1/05_discriminant.c b/semestre 2/informatique/tme/semaine1/05_discriminant.c new file mode 100644 index 0000000..575f776 --- /dev/null +++ b/semestre 2/informatique/tme/semaine1/05_discriminant.c @@ -0,0 +1,28 @@ +#include <math.h> +#include <stdio.h> + +int discriminant(int a, int b, int c){ + return b*b-4*a*c; +} + +void afficheRacines(int a, int b, int c){ + int disc = discriminant(a,b,c); + if (disc > 0) { + float sq = sqrt(disc); + printf("Racine 1 : %f\nRacine 2 : %f\n", (-b-sq)/(2*a), (-b+sq)/(2*a)); + } else if (disc == 0) { + printf("Racine double : %f\n", (-b)/(float) (2*a)); + } else { + printf("Pas de racine réelle\n"); + } +} + +int main(){ + afficheRacines(1, 1, 1); + afficheRacines(1, 1, -1); + afficheRacines(-1, 1, -1); + afficheRacines(-1, 1, 1); + afficheRacines(1, 2, 1); + afficheRacines(2, 4, 2); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine1/06_signe_produit.c b/semestre 2/informatique/tme/semaine1/06_signe_produit.c new file mode 100644 index 0000000..764f184 --- /dev/null +++ b/semestre 2/informatique/tme/semaine1/06_signe_produit.c @@ -0,0 +1,20 @@ +#include <assert.h> + +int signeProduit(int a, int b){ + if (a == 0 || b == 0) return 0; + else if ( + (a < 0 && b > 0) || (a > 0 && b < 0) + ) return -1; + return 1; +} + +int main(){ + assert(signeProduit(0, 0) == 0); + assert(signeProduit(1, 0) == 0); + assert(signeProduit(0, -1) == 0); + assert(signeProduit(1, 1) == 1); + assert(signeProduit(-1, -1) == 1); + assert(signeProduit(1, -1) == -1); + assert(signeProduit(-1, 1) == -1); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine1/07_tour_londres.c b/semestre 2/informatique/tme/semaine1/07_tour_londres.c new file mode 100644 index 0000000..92f0cff --- /dev/null +++ b/semestre 2/informatique/tme/semaine1/07_tour_londres.c @@ -0,0 +1,37 @@ +#include <assert.h> +#define ADU 22.7 +#define KID 10.75 +#define FAMILLY 57.8 +#define COMPARE_FLOAT_PRECISION 0.00001 + +float prixEntree(int adus, int kids){ + float no_familly = adus * ADU + kids * KID; + float with_familly = FAMILLY; + int r_adus = adus - 2; + int r_kids = kids - 3; + if (r_adus > 0){ + with_familly += r_adus * ADU; + } + if (r_kids > 0){ + with_familly += r_kids * KID; + } + if (no_familly < with_familly) return no_familly; + return with_familly; +} + +int compareFloat(float f1, float f2, float eps){ + /* Prend trois floats (f1, f2 et eps) et renvoie |f1-f2|<eps ; est une comparaison de deux + * floats à epsilon près */ + float diff = f1 - f2; + if (diff < 0) diff = - diff; + return diff < eps; +} + +int main(){ + assert(compareFloat(prixEntree(2, 3), FAMILLY, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(2, 2), FAMILLY, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(2, 1), 2*ADU + KID, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(1, 3), ADU + 3*KID, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(0, 1), KID, COMPARE_FLOAT_PRECISION)); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine2/09_lib_graphique.c b/semestre 2/informatique/tme/semaine2/09_lib_graphique.c new file mode 100644 index 0000000..d90845e --- /dev/null +++ b/semestre 2/informatique/tme/semaine2/09_lib_graphique.c @@ -0,0 +1,15 @@ +#include <cini.h> + +void diagonale(int x){ + for (int i = 0; i <= x; i++) { + CINI_draw_pixel(i, i, "orange"); + } +} + +int main(){ + CINI_open_window(400, 300, "hello world"); + diagonale(200); + CINI_loop(); + + return 0; +} diff --git a/semestre 2/informatique/tme/semaine2/10_carre.c b/semestre 2/informatique/tme/semaine2/10_carre.c new file mode 100644 index 0000000..45029bb --- /dev/null +++ b/semestre 2/informatique/tme/semaine2/10_carre.c @@ -0,0 +1,28 @@ +#include <cini.h> + +void carre(int c, int x, int y){ + for (int i = 0; i<c;i++){ + CINI_draw_pixel(x+i, y, "blue"); + CINI_draw_pixel(x+i, y+c, "green"); + CINI_draw_pixel(x, y+i, "red"); + CINI_draw_pixel(x+c, y+i, "black"); + } +} + +void carres_remontant(int c, int x, int y){ + int nx = x, ny = y; + while (nx >= 0 && ny >= 0){ + carre(c, nx, ny); + nx -= 20; + ny -= 20; + } +} + +int main() { + CINI_open_window(400, 400, "Carre"); + CINI_fill_window("white"); + carres_remontant(100, 50, 100); + CINI_loop(); + + return 0; +} diff --git a/semestre 2/informatique/tme/semaine2/11_propagation_epidemie.c b/semestre 2/informatique/tme/semaine2/11_propagation_epidemie.c new file mode 100644 index 0000000..26b763e --- /dev/null +++ b/semestre 2/informatique/tme/semaine2/11_propagation_epidemie.c @@ -0,0 +1,41 @@ +#include <assert.h> +#include <stdio.h> + +int jours(int infected, int pop, float percentage){ + int n = 1, c = 0; + while (n / (float) pop < percentage/100){ + n += infected * n; + c++; + } + return c; +} + +float pourcentage(int infected, int pop, int day){ + int n = 1, c = 0; + while (c < day && n <= pop){ + n += infected*n; + c++; + } + if (n >= pop) return 100; + return 100*n/ (float) pop; +} + +int eps_pres(float a, float b, float eps){ + float v = a - b; + if (v < 0) v = -v; + return v < eps; +} + +int main(){ + assert(jours(5, 10000, 100) == 6); + assert(jours(5, 10000, 50) == 5); + assert(jours(5, 10000, 25) == 5); + assert(jours(5, 10000, 10) == 4); + + assert(eps_pres(pourcentage(5, 10000, 2), 0.36, 0.001)); + assert(eps_pres(pourcentage(5, 10000, 3), 2.16, 0.001)); + assert(eps_pres(pourcentage(5, 10000, 4), 12.96, 0.001)); + assert(eps_pres(pourcentage(5, 10000, 5), 77.76, 0.001)); + assert(eps_pres(pourcentage(5, 10000, 6), 100.00, 0.001)); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine2/12_droite_points.c b/semestre 2/informatique/tme/semaine2/12_droite_points.c new file mode 100644 index 0000000..a202e24 --- /dev/null +++ b/semestre 2/informatique/tme/semaine2/12_droite_points.c @@ -0,0 +1,34 @@ +#include <cini.h> +#define WIDTH 400 +#define HEIGHT 400 + +int position(int a, int b, int x, int y){ + // t(z) = az+b + // y(x) = alpha x + beta + int straight_line_y = a*x+b; + if (straight_line_y > y) return 1; + if (straight_line_y < y) return -1; + return 0; +} + +void affiche(int a, int b, int width, int height){ + for (int x = 0; x < width; x++){ + for (int y = 0; y < height; y++){ + int pos = position(a, b, x, y); + char* color; + if (pos == 1) color = "red"; + else if (pos == -1) color = "blue"; + else color = "black"; + CINI_draw_pixel(x, y, color); + } + } +} + +int main(){ + CINI_open_window(WIDTH, HEIGHT, "Hello"); + CINI_fill_window("white"); + affiche(1, 0, WIDTH, HEIGHT); + CINI_loop(); + return 0; +} + diff --git a/semestre 2/informatique/tme/semaine2/14_tour_londres.c b/semestre 2/informatique/tme/semaine2/14_tour_londres.c new file mode 100644 index 0000000..1c86e65 --- /dev/null +++ b/semestre 2/informatique/tme/semaine2/14_tour_londres.c @@ -0,0 +1,51 @@ +#include <assert.h> +#define ADU 22.7 +#define KID 10.75 +#define FAMILLY 57.8 +#define MAX_ADU_FAMILLY 2 +#define MAX_KID_FAMILLY 3 +#define COMPARE_FLOAT_PRECISION 0.00001 + +float prixEntree(int adus, int kids){ + int max_familly = 0; + int r_adus = adus; + int r_kids = kids; + do { + r_adus -= MAX_ADU_FAMILLY; + r_kids -= MAX_KID_FAMILLY; + max_familly++; + } while (r_adus > 0 || r_kids > 0); + float min = -1; + for (int f = 0; f <= max_familly; f++){ + int r_adus = adus - MAX_ADU_FAMILLY*f; + int r_kids = kids - MAX_KID_FAMILLY*f; + float t = f*FAMILLY; + if (r_adus > 0) t += r_adus * ADU; + if (r_kids > 0) t += r_kids * KID; + if (min == -1 || t < min) { + min = t; + } + } + return min; +} + +int compareFloat(float f1, float f2, float eps){ + /* Prend trois floats (f1, f2 et eps) et renvoie |f1-f2|<eps ; est une comparaison de deux + * floats à epsilon près */ + float diff = f1 - f2; + if (diff < 0) diff = - diff; + return diff < eps; +} + +int main(){ + assert(compareFloat(prixEntree(2, 3), FAMILLY, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(2, 2), FAMILLY, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(2, 1), 2*ADU + KID, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(6, 3), 4*ADU + FAMILLY, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(1, 3), 1*ADU + 3*KID, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(5, 7), 2*FAMILLY + ADU + KID, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(6, 8), 3*FAMILLY, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(10, 0), 227, COMPARE_FLOAT_PRECISION)); + assert(compareFloat(prixEntree(0, 4), 43, COMPARE_FLOAT_PRECISION)); + return 0; +} |
