mariem sebai TD04 TP08
compte rendu TP3
exercice 1:
int compter(int n) {
if (n == 0) return 0;
return 1 + compter(n / 10);
}
int somme(int n) {
if (n == 0) return 0;
return (n % 10) + somme(n / 10);
}
int estPremier(int n, int div) {
if (n < 2) return 0;
if (div == 1) return 1;
if (n % div == 0) return 0;
return estPremier(n, div - 1);
}
int premier(int n) {
return estPremier(n, n / 2);
}
int sommeDiviseurs(int n, int div) {
if (div == 0) return 0;
return (n % div == 0 ? div : 0) + sommeDiviseurs(n, div - 1);
}
int parfait(int n) {
return (sommeDiviseurs(n, n / 2) == n);
}
int Pair(int n) {
if (n == 0) return 1;
if (n == 1) return 0;
return Pair(n - 2);
}
int impair(int n);
int pair(int n) {
if (n == 0) return 1;
return impair(n - 1);
}
int impair(int n) {
if (n == 0) return 0;
return pair(n - 1);
}
void permutationGauche(int *a, int *b, int *c, int g) {
for (int i = 0; i < g; i++) {
int temp = *a;
*a = *b;
*b = *c;
*c = temp;
}
}
void permutationDroite(int *a, int *b, int *c, int d) {
for (int i = 0; i < d; i++) {
int temp = *c;
*c = *b;
*b = *a;
*a = temp;
}
}
void AfficheInverse(int n) {
if (n == 0) return;
printf("%d", n % 10);
AfficheInverse(n / 10);
}
int main() {
int n, a, b, c, g, d;
printf("Entrez un entier positif : ");
scanf("%d", &n);
printf("Nombre de chiffres : %d\n", compter(n));
printf("Somme des chiffres : %d\n", somme(n));
printf("Nombre premier : %s\n", premier(n) ? "Oui" : "Non");
printf("Nombre parfait : %s\n", parfait(n) ? "Oui" : "Non");
printf("Nombre pair (sans modulo) : %s\n", Pair(n) ? "Oui" :
"Non");
printf("Nombre pair (récursivité croisée) : %s\n", pair(n) ?
"Oui" : "Non");
printf("Affichage inverse : ");
AfficheInverse(n);
printf("\n");
printf("\nEntrez trois entiers (a, b, c) : ");
scanf("%d %d %d", &a, &b, &c);
printf("Entrez le nombre de permutations à gauche : ");
scanf("%d", &g);
permutationGauche(&a, &b, &c, g);
printf("Après permutation gauche : a=%d, b=%d, c=%d\n", a,
b, c);
printf("Entrez le nombre de permutations à droite : ");
scanf("%d", &d);
permutationDroite(&a, &b, &c, d);
printf("Après permutation droite : a=%d, b=%d, c=%d\n", a,
b, c);
return 0;
}
exercice 2:
#include <stdio.h>
#include <string.h>
int Longueur(const char *str) {
return (*str == '\0') ? 0 : 1 + Longueur(str + 1);
}
void inverser(char *str, int left, int right) {
if (left >= right) return;
char temp = str[left];
str[left] = str[right];
str[right] = temp;
inverser(str, left + 1, right - 1);
}
int comparer(const char *str1, const char *str2) {
if (*str1 == '\0' && *str2 == '\0') return 0;
if (*str1 != *str2) return (*str1 - *str2);
return comparer(str1 + 1, str2 + 1);
}
int palindrome(const char *str, int left, int right) {
if (left >= right) return 1;
if (str[left] != str[right]) return 0;
return palindrome(str, left + 1, right - 1);
}
int Rechercher(const char *str, char c, int index) {
if (*str == '\0') return -1;
if (*str == c) return index;
return Rechercher(str + 1, c, index + 1);
}
int Compter(const char *str, char c) {
if (*str == '\0') return 0;
return ((*str == c) ? 1 : 0) + Compter(str + 1, c);
}
void Supprimer(char *str, int index, int shift) {
if (str[index] == '\0') {
str[index - shift] = '\0';
return;
}
if (str[index] == ' ') shift++;
str[index - shift] = str[index];
Supprimer(str, index + 1, shift);
}
void Tri_Selection(char *str, int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (str[j] < str[minIdx]) minIdx = j;
}
char temp = str[i];
str[i] = str[minIdx];
str[minIdx] = temp;
}
}
void Tri_Insertion(char *str, int n) {
for (int i = 1; i < n; i++) {
char key = str[i];
int j = i - 1;
while (j >= 0 && str[j] > key) {
str[j + 1] = str[j];
j--;
}
str[j + 1] = key;
}
}
void Tri_Rapide(char *str, int low, int high) {
if (low < high) {
char pivot = str[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (str[j] < pivot) {
i++;
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
char temp = str[i + 1];
str[i + 1] = str[high];
str[high] = temp;
int pi = i + 1;
Tri_Rapide(str, low, pi - 1);
Tri_Rapide(str, pi + 1, high);
}
}
int main() {
char str[100], c;
printf("Entrez une chaîne de caractères : ");
scanf("%s", str);
printf("Longueur : %d\n", Longueur(str));
char tempStr[100];
strcpy(tempStr, str);
inverser(tempStr, 0, strlen(tempStr) - 1);
printf("Chaîne inversée : %s\n", tempStr);
printf("Entrez un caractère à rechercher : ");
scanf(" %c", &c);
printf("Position : %d\n", Rechercher(str, c, 0));
printf("Occurrences : %d\n", Compter(str, c));
if (palindrome(str, 0, strlen(str) - 1))
printf("La chaîne est un palindrome\n");
else
printf("La chaîne n'est pas un palindrome\n");
strcpy(tempStr, str);
Supprimer(tempStr, 0, 0);
printf("Sans espaces : %s\n", tempStr);
strcpy(tempStr, str);
Tri_Selection(tempStr, strlen(tempStr));
printf("Tri sélection : %s\n", tempStr);
strcpy(tempStr, str);
Tri_Insertion(tempStr, strlen(tempStr));
printf("Tri insertion : %s\n", tempStr);
strcpy(tempStr, str);
Tri_Rapide(tempStr, 0, strlen(tempStr) - 1);
printf("Tri rapide : %s\n", tempStr);
return 0;
}