Ajout des TP
This commit is contained in:
51
BUT1/CONTROLE/CM3/exo1/exo1.c
Normal file
51
BUT1/CONTROLE/CM3/exo1/exo1.c
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void modifierImage(char *nomFichier) {
|
||||
int pixel, largeur, hauteur, intensiteMax;
|
||||
char commentaire[256],enTete[3];
|
||||
FILE *fichier = fopen(nomFichier, "r+");
|
||||
|
||||
if (fichier == NULL) {
|
||||
fprintf(stderr,"Impossible d'ouvrir le fichier !");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Lire l'en-tête */
|
||||
fscanf(fichier, "%2s", enTete);
|
||||
|
||||
if (enTete[0] != 'P' || enTete[1] != '2') {
|
||||
printf("Le fichier n'est pas au format PPM ASCII.\n");
|
||||
fclose(fichier);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Ignorer les commentaires s'il y en a*/
|
||||
while (fgetc(fichier) == '#') {
|
||||
fgets(commentaire, sizeof(commentaire), fichier);
|
||||
}
|
||||
|
||||
/* Lire les dimensions et l'intensité maximale*/
|
||||
fscanf(fichier, "%d %d %d", &largeur, &hauteur, &intensiteMax);
|
||||
|
||||
/* Modifier les pixels du message en blanc*/
|
||||
fseek(fichier, ftell(fichier), SEEK_SET); /* Revenir au début des données d'image*/
|
||||
|
||||
while ((pixel = fgetc(fichier)) != EOF) {
|
||||
/* Modifier uniquement les pixels correspondant au message (intensité 3)*/
|
||||
if (pixel == 3) {
|
||||
fseek(fichier, -1, SEEK_CUR);
|
||||
fprintf(fichier, "5 ");
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fichier);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char nomFichier[] = "image.pgm";
|
||||
modifierImage(nomFichier);
|
||||
printf("Modification terminée avec succès.\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
0
BUT1/CONTROLE/CM3/exo1/image.pgm
Normal file
0
BUT1/CONTROLE/CM3/exo1/image.pgm
Normal file
47
BUT1/CONTROLE/CM3/exo2/exo2.c
Normal file
47
BUT1/CONTROLE/CM3/exo2/exo2.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Fonction récursive pour trouver le maximum entre deux entiers*/
|
||||
int maxRecursif(int a, int b) {
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
/* Fonction récursive pour trouver le maximum dans un tableau d'entiers*/
|
||||
int maxTableauRecursif(int tableau[], int taille) {
|
||||
if (taille == 1) {
|
||||
return tableau[0];
|
||||
}
|
||||
|
||||
return maxRecursif(tableau[taille - 1], maxTableauRecursif(tableau, taille - 1));
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int i,max;
|
||||
int* entiers;
|
||||
if (argc < 2) {
|
||||
perror("Usage: ./a.out <entier1> <entier2> <entierN>\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* Allouer de la mémoire pour stocker les entiers fournis en ligne de commande*/
|
||||
entiers = (int*)malloc((argc - 1) * sizeof(int));
|
||||
|
||||
if (entiers == NULL) {
|
||||
perror("Erreur d'allocation mémoire");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* Convertir les arguments en entiers et les stocker dans le tableau*/
|
||||
for (i = 1; i < argc; i++) {
|
||||
entiers[i - 1] = atoi(argv[i]);
|
||||
}
|
||||
|
||||
/* Trouver et afficher le plus grand entier récursivement*/
|
||||
max = maxTableauRecursif(entiers, argc - 1);
|
||||
printf("%d\n", max);
|
||||
|
||||
/* Libérer la mémoire allouée pour le tableau*/
|
||||
free(entiers);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
98
BUT1/CONTROLE/CM3/exo3/exo3.c
Normal file
98
BUT1/CONTROLE/CM3/exo3/exo3.c
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Structure pour un nœud dans la liste chaînée*/
|
||||
typedef struct Noeud {
|
||||
int digit;
|
||||
struct Noeud* suivant;
|
||||
} Noeud;
|
||||
|
||||
/* Fonction pour convertir une représentation décimale en liste chaînée*/
|
||||
Noeud* convertirListeChainee(char* nombre) {
|
||||
Noeud* liste = NULL;
|
||||
int i;
|
||||
int length = strlen(nombre);
|
||||
|
||||
for (i = length - 1; i >= 0; i--) {
|
||||
Noeud* nouveauNoeud = (Noeud*)malloc(sizeof(Noeud));
|
||||
nouveauNoeud->digit = nombre[i] - '0';
|
||||
nouveauNoeud->suivant = liste;
|
||||
liste = nouveauNoeud;
|
||||
}
|
||||
|
||||
return liste;
|
||||
}
|
||||
|
||||
/* Fonction pour afficher une liste chaînée*/
|
||||
void AfficherListe(Noeud* liste) {
|
||||
while (liste != NULL) {
|
||||
printf("%d", liste->digit);
|
||||
liste = liste->suivant;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* Fonction pour additionner deux listes chaînées*/
|
||||
Noeud* ajouterAListe(Noeud* num1, Noeud* num2) {
|
||||
Noeud* resultat = NULL;
|
||||
Noeud* actuel = NULL;
|
||||
Noeud* nouveauNoeud = NULL;
|
||||
int retenue = 0;
|
||||
int somme;
|
||||
|
||||
while (num1 != NULL || num2 != NULL || retenue != 0) {
|
||||
somme = retenue;
|
||||
|
||||
if (num1 != NULL) {
|
||||
somme += num1->digit;
|
||||
num1 = num1->suivant;
|
||||
}
|
||||
|
||||
if (num2 != NULL) {
|
||||
somme += num2->digit;
|
||||
num2 = num2->suivant;
|
||||
}
|
||||
|
||||
nouveauNoeud = (Noeud*)malloc(sizeof(Noeud));
|
||||
nouveauNoeud->digit = somme % 10;
|
||||
nouveauNoeud->suivant = NULL;
|
||||
|
||||
retenue = somme / 10;
|
||||
|
||||
if (resultat == NULL) {
|
||||
resultat = nouveauNoeud;
|
||||
actuel = resultat;
|
||||
} else {
|
||||
actuel->suivant = nouveauNoeud;
|
||||
actuel = nouveauNoeud;
|
||||
}
|
||||
}
|
||||
|
||||
return resultat;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
Noeud* num1 = NULL;
|
||||
Noeud* num2 = NULL;
|
||||
Noeud* somme = NULL;
|
||||
char* nombre1;
|
||||
char* nombre2;
|
||||
if (argc != 3) {
|
||||
perror("Usage: %s <nombre1> <nombre2>\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
nombre1 = argv[1];
|
||||
nombre2 = argv[2];
|
||||
|
||||
|
||||
num1 = convertirListeChainee(nombre1);
|
||||
num2 = convertirListeChainee(nombre2);
|
||||
somme = ajouterAListe(num1, num2);
|
||||
|
||||
|
||||
AfficherListe(somme);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Reference in New Issue
Block a user