diff options
| author | Anhgelus Morhtuuzh <anhgelus@anhgelus.world> | 2025-03-27 17:24:15 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <anhgelus@anhgelus.world> | 2025-03-27 17:24:15 +0100 |
| commit | c49b969659d8761442a560f8feda436bfb7b01e8 (patch) | |
| tree | 70e34b6cc1b4285e009f9acace8c87a869d6ccb2 /semestre 2/informatique/tme | |
| parent | 4a3afaf44aa29e66a6c879c60322015a2920a5ab (diff) | |
Ajout des cours du 10 au 27 mars
Diffstat (limited to 'semestre 2/informatique/tme')
| -rw-r--r-- | semestre 2/informatique/tme/semaine5/28_participation_frais.c | 5 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine5/30_compression.c | 72 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine5/31_image_mystere.c | 12 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine6/iter.c | 13 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine6/rec.c | 12 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine6/rec_2.c | 16 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine6/rec_str.c | 19 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine7/35_systeme_solaire.c | 33 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine7/hello.c | 41 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine8/build_run.sh | 4 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine8/liste_entiers.c | 102 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine8/liste_entiers.h | 24 | ||||
| -rw-r--r-- | semestre 2/informatique/tme/semaine8/test_liste.c | 17 |
13 files changed, 355 insertions, 15 deletions
diff --git a/semestre 2/informatique/tme/semaine5/28_participation_frais.c b/semestre 2/informatique/tme/semaine5/28_participation_frais.c index e5aff0d..96da9b7 100644 --- a/semestre 2/informatique/tme/semaine5/28_participation_frais.c +++ b/semestre 2/informatique/tme/semaine5/28_participation_frais.c @@ -13,9 +13,10 @@ void init_tab(float tab[NB_AMIS][NB_JOURS]){ void random_tab(float tab[NB_AMIS][NB_JOURS], int j) { int a = rand()%NB_AMIS; int v = rand()%21+30; - tab[a][j] = v; + float to_pay = (float) v/(-NB_AMIS); + tab[a][j] = v + to_pay; for (int i = 0; i < NB_AMIS; i++) { - if (i != a) tab[i][j] = (float) v/(-NB_AMIS+1); + if (i != a) tab[i][j] = to_pay; } } diff --git a/semestre 2/informatique/tme/semaine5/30_compression.c b/semestre 2/informatique/tme/semaine5/30_compression.c index 161cd00..154dbc9 100644 --- a/semestre 2/informatique/tme/semaine5/30_compression.c +++ b/semestre 2/informatique/tme/semaine5/30_compression.c @@ -14,15 +14,19 @@ int init_tab(int tab[MAX+1]) { } void compress_tab(int tab_brut[], int tab_compress[]){ - int j = 0; int last = -1; int cnt = 0; + int w = 0; for (int i = 0; tab_brut[i] != -1; i++) { if (last == -1 || tab_brut[i] != last) { if (last != -1) { - int id = j++; - tab_compress[id*2] = cnt; - tab_compress[id*2+1] = last; + if (cnt == 1) { + tab_compress[w] = last; + } else { + tab_compress[w] = cnt; + tab_compress[++w] = last; + } + w++; } last = tab_brut[i]; cnt = 0; @@ -30,11 +34,35 @@ void compress_tab(int tab_brut[], int tab_compress[]){ cnt++; } if (last != -1) { - int id = j++; - tab_compress[id*2] = cnt; - tab_compress[id*2+1] = last; + if (cnt == 1) { + tab_compress[w] = last; + } else { + tab_compress[w] = cnt; + tab_compress[++w] = last; + } + w++; + } + tab_compress[w] = -1; +} + +void decompress(int tab_brut[], int tab_compress[]) { + int i = 0; + int w = 0; + while (tab_compress[i] != -1) { + int v = tab_compress[i]; + if (v < 2) { + tab_brut[w++] = v; + } else { + int ins_v = tab_compress[++i]; + for (int j = 0; j < v; j++) { + tab_brut[w++] = ins_v; + } + } + for (int j = 0; j < w; j++) printf("%d, ", tab_brut[j]); + printf("\n"); + i++; } - tab_compress[j*2] = -1; + tab_brut[w] = -1; } int compare(int tab_brut[], int tab_compress[]){ @@ -51,7 +79,31 @@ int compare(int tab_brut[], int tab_compress[]){ int main(){ srand(time(NULL)); - int tab[MAX+1] = {}; + int tab1[]={0,0,1,1,1,1,0,1,1,1,0,0,1,0,0,0,0,-1}; + //int tab1[] = {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,-1}; + for (int i = 0; tab1[i] != -1; i++) printf("%d, ", tab1[i]); + printf("\n"); + int tab2[20]; + compress_tab(tab1,tab2); + int tab3[20]; + decompress(tab3, tab2); + int i; + for (i = 0; tab1[i] != -1 && tab3[i] != -1; i++) { + printf("%d - %d\n", tab1[i], tab3[i]); + } + printf("%d\n", i); + i = 0; + while (tab2[i] != -1) { + int v = tab2[i]; + if (v < 2) { + printf("%d ", v); + } else { + printf("%d:%d ", v, tab2[++i]); + } + i++; + } + printf("\n"); + /*int tab[MAX+1] = {}; int tab_compress[MAX+1] = {}; init_tab(tab); compress_tab(tab, tab_compress); @@ -59,6 +111,6 @@ int main(){ printf("\n"); for (int i = 0; tab_compress[i] != -1; i += 2) printf("%d:%d, ", tab_compress[i], tab_compress[i+1]); printf("\n"); - printf("%s", compare(tab, tab_compress) ? "True" : "False"); + printf("%s", compare(tab, tab_compress) ? "True" : "False");*/ return 0; } diff --git a/semestre 2/informatique/tme/semaine5/31_image_mystere.c b/semestre 2/informatique/tme/semaine5/31_image_mystere.c index 51ca321..29264e1 100644 --- a/semestre 2/informatique/tme/semaine5/31_image_mystere.c +++ b/semestre 2/informatique/tme/semaine5/31_image_mystere.c @@ -12,9 +12,15 @@ void calcule_borne_sup(int *tab, int taille){ int tire_non_equi(int *tab, int taille){ int tire = rand()%100; - int i; - for (i = taille -1; tab[i] > tire; i--); - printf("%d - %d <= %d\n", i, tab[i], tire); + int i = taille -1; + /*while (i >= 0) { + if (tire > tab[i]) break; + i--; + } + i++;*/ + for (i = taille -1; tab[i] >= tire; i--); + i++; + printf("%d >= %d\n", tab[i], tire); return i; } diff --git a/semestre 2/informatique/tme/semaine6/iter.c b/semestre 2/informatique/tme/semaine6/iter.c new file mode 100644 index 0000000..fd55e23 --- /dev/null +++ b/semestre 2/informatique/tme/semaine6/iter.c @@ -0,0 +1,13 @@ +#include <stdio.h> +int recherche_iter(int* arr, int n, int v) { + int i; + for (i = 0; arr[i] != v && i < n; i++); + if (i == n) return -1; + return i; +} + +int main() { + int arr[] = {1, 2, 3}; + printf("%d\n", recherche_iter(arr, 3, 4)); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine6/rec.c b/semestre 2/informatique/tme/semaine6/rec.c new file mode 100644 index 0000000..0a5bb34 --- /dev/null +++ b/semestre 2/informatique/tme/semaine6/rec.c @@ -0,0 +1,12 @@ +#include <stdio.h> +int recherche_rec(int* arr, int n, int v){ + if (n == 0) return -1; + if (arr[n-1] == v) return n-1; + return recherche_rec(arr, n-1, v); +} + +int main() { + int arr[] = {1, 2, 3, 5, 5}; + printf("%d\n",recherche_rec(arr, 5, 6)); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine6/rec_2.c b/semestre 2/informatique/tme/semaine6/rec_2.c new file mode 100644 index 0000000..9bfd376 --- /dev/null +++ b/semestre 2/informatique/tme/semaine6/rec_2.c @@ -0,0 +1,16 @@ +#include <stdio.h> +int recherche_rec_aux (int* tab, int n,int i, int v) { + if (i == n) return -1; + if (tab[i] == v) return i; + return recherche_rec_aux(tab, n, i+1, v); +} + +int recherche_rec (int tab[], int taille, int elem) { + return recherche_rec_aux (tab, taille, 0, elem); +} + +int main() { + int arr[] = {1, 2, 3}; + printf("%d\n", recherche_rec(arr, 3, 0)); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine6/rec_str.c b/semestre 2/informatique/tme/semaine6/rec_str.c new file mode 100644 index 0000000..a920af3 --- /dev/null +++ b/semestre 2/informatique/tme/semaine6/rec_str.c @@ -0,0 +1,19 @@ +#include <stdio.h> +int est_deb(char* s1, char* s2) { + if (*s1 == '\0' || *s2 =='\0') return 1; + if (*s1 != *s2) return 0; + return est_deb(s1+1, s2+1); +} + +int est_incluse(char *sub, char* s) { + if (*s == '\0') return 0; + return est_deb(sub, s) ? 1 : est_incluse(sub, s+1); +} + +int main() { + printf("%d\n", est_deb("alpha", "alphabet")); + printf("%d\n", est_deb("alpaga", "alphabet")); + printf("%d\n", est_incluse("alp", "alphabet")); + printf("%d\n", est_incluse("apl", "alphabet")); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine7/35_systeme_solaire.c b/semestre 2/informatique/tme/semaine7/35_systeme_solaire.c new file mode 100644 index 0000000..aa9c42b --- /dev/null +++ b/semestre 2/informatique/tme/semaine7/35_systeme_solaire.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#define NB_PLANETES 8 + +typedef struct{ + char name[10]; + float density; + float distance; + int satellites; +} planete; + +void affichePlanete(planete *p){ + printf( + "%s:\n- densité : %.02f\n- distance : %.2f\n- nombre de satellites : %d\n\n", + p->name, + p->density, + p->distance, + p->satellites + ); +} + +void afficheToutesPlanetes(planete ps[]) { + for (int i = 0; i < NB_PLANETES; i++) affichePlanete((ps+i)); +} + +void modifieDensite(planete *p, float v){ + p->density = v; +} + +int main(){ + planete systemeSolaire[NB_PLANETES] ={{"Mercure", 5.42, 58, 0},{"Venus", 5.25, 108.2, 0},{"Terre", 5.52,149.6,1},{"Mars",3.94,227.9,2},{"Jupiter",1.314,778.3,16},{"Saturne",0.69,1427,17},{"Uranus",1.19,2869,15},{"Neptune",1.6,4496,2}}; + afficheToutesPlanetes(systemeSolaire); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine7/hello.c b/semestre 2/informatique/tme/semaine7/hello.c new file mode 100644 index 0000000..4e2d9dc --- /dev/null +++ b/semestre 2/informatique/tme/semaine7/hello.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#define NB_PLANETES 8 + +/* ici la definition du type planete */ +typedef struct{ + char nom[10]; + float densite; + float distance; + int nbsat; +} planete; + +void affichePlanete(planete p){ + printf("%s : densite = %.2f, distance soleil = %.1f, nb satellites = %d", + p.nom, p.densite, p.distance, p.nbsat); +} + +void afficheToutesPlanetes(planete ps[], int n){ + for (int i = 0; i < n; i++) affichePlanete(*(ps+i)); +} + +void modifieDensite (planete *p, float v) { + p->densite = v; +} + +int main(){ + planete systemeSolaire[NB_PLANETES] ={{"Mercure", 5.42, 58, 0}, {"Venus", 5.25, 108.2, 0}, + {"Terre", 5.52, 149.6,1}, {"Mars", 3.94, 227.9, 2}, {"Jupiter", 1.314, 778.3, 16}, + {"Saturne", 0.69, 1427, 17}, {"Uranus", 1.19, 2869, 15}, {"Neptune", 1.6, 4496, 2}}; + int i; + float d; + + afficheToutesPlanetes(systemeSolaire, NB_PLANETES); + printf("\n"); + scanf("%d", &i); + scanf("%f", &d); + /* on affecte la densite d a la planete d'indice i du tableau systemeSolaire */ + modifieDensite(systemeSolaire+i, d); + affichePlanete(systemeSolaire[i]); + printf("\n"); + return 0; +} diff --git a/semestre 2/informatique/tme/semaine8/build_run.sh b/semestre 2/informatique/tme/semaine8/build_run.sh new file mode 100644 index 0000000..4a32768 --- /dev/null +++ b/semestre 2/informatique/tme/semaine8/build_run.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +gcc -Wall -o app liste_entiers.c liste_entiers.h test_liste.c +./app +rm app diff --git a/semestre 2/informatique/tme/semaine8/liste_entiers.c b/semestre 2/informatique/tme/semaine8/liste_entiers.c new file mode 100644 index 0000000..c53040c --- /dev/null +++ b/semestre 2/informatique/tme/semaine8/liste_entiers.c @@ -0,0 +1,102 @@ +#include <stdio.h> +#include <stdlib.h> +#include "liste_entiers.h" + +cellule_t * creerListe(int n) { +/* cree une liste de n entiers saisi par l'utilisateur + renvoie l'adresse du premier element de la liste */ + int i; + int val; + cellule_t *tete=NULL; + cellule_t *ptr; + + printf("Saisie des %d elements de la liste\n",n); + for (i=0; i < n; i++) { + printf("Element %d :",i+1); + scanf("%d",&val); + ptr=malloc(sizeof(cellule_t)); + ptr->donnee = val; + ptr->suivant = tete; + tete = ptr; + } + return tete; +} + +cellule_t *list(int n, cellule_t *q){ + cellule_t *h = malloc(sizeof(cellule_t)); + h->suivant = q; + h->donnee = n; + return h; +} + +void AfficherListeInt(cellule_t *liste){ + while (liste){ + printf("%d ", liste->donnee); + liste = liste->suivant; + } +} + +int nb_occurences(int val, cellule_t *liste){ + int c = 0; + while (liste){ + if (liste->donnee == val) c++; + liste = liste->suivant; + } + return c; +} + +int tous_plus_grand(int val, cellule_t *liste){ + while (liste){ + if (liste->donnee < val) return 0; + liste = liste->suivant; + } + return 1; +} + +cellule_t *Maximum(cellule_t *liste){ + cellule_t *max= NULL; + while (liste){ + if (!max || (liste->donnee > max->donnee)) max = liste; + liste = liste->suivant; + } + return max; +} + +int Renvoyer_val_element_pos(int pos, cellule_t* liste){ + int size; + cellule_t *tmp = liste; + for (size = 0; tmp; size++) tmp = tmp->suivant; + for (int i = 0; i < pos; i++) liste = liste->suivant; + // pos = 0 ~ 2 + // pos = 1 ~ 1 + // pos = 2 ~ 0 + // size-pos = 3-0 -> 3 + // size-pos = 3-1 -> 2 + // size-pos = 3-2 -> 1 + if (!liste) { + printf("error\n"); + return -1; + } + return liste->donnee; +} + +cellule_t *Concatener_it(cellule_t *liste1, cellule_t *liste2){ + if (!liste1) return liste2; + cellule_t *head = liste1; + while (liste1->suivant) liste1 = liste1->suivant; + liste1->suivant = liste2; + return head; +} + +int nb_maximum(cellule_t *liste){ + cellule_t *max= NULL; + int c = 1; + while (liste){ + if (!max || (liste->donnee > max->donnee)) { + max = liste; + c = 1; + } else if (liste->donnee == max->donnee) c++; + liste = liste->suivant; + } + return c; +} diff --git a/semestre 2/informatique/tme/semaine8/liste_entiers.h b/semestre 2/informatique/tme/semaine8/liste_entiers.h new file mode 100644 index 0000000..78ccefb --- /dev/null +++ b/semestre 2/informatique/tme/semaine8/liste_entiers.h @@ -0,0 +1,24 @@ +typedef struct _cellule_t cellule_t; + +struct _cellule_t { + int donnee; + cellule_t *suivant; +}; + +cellule_t * creerListe(int n); + +void AfficherListeInt(cellule_t *liste); + +int nb_occurences(int val, cellule_t *liste); + +int tous_plus_grand(int val, cellule_t *liste); + +cellule_t *Maximum(cellule_t *liste); + +int Renvoyer_val_element_pos(int pos, cellule_t* liste); + +cellule_t *Concatener_it(cellule_t *liste1, cellule_t *liste2); + +int nb_maximum(cellule_t *liste); + +cellule_t *list(int n, cellule_t *q); diff --git a/semestre 2/informatique/tme/semaine8/test_liste.c b/semestre 2/informatique/tme/semaine8/test_liste.c new file mode 100644 index 0000000..acd23ae --- /dev/null +++ b/semestre 2/informatique/tme/semaine8/test_liste.c @@ -0,0 +1,17 @@ +#include "liste_entiers.h" +#include <stdio.h> + +int main() { + cellule_t *ma_liste=list(1, list(3, list(0, NULL))); + int size = 0; + cellule_t *t = ma_liste; + for (size = 0; t; size++) { + printf("val %d\n", t->donnee); + t = t->suivant; + } + printf("size %d\n", size); + printf("before\n"); + for (int i = 0; i < size; i++) printf("pos %d = %d\n", i, Renvoyer_val_element_pos(i, ma_liste)); + printf("after\n"); + return 0; +} |
