Ajout TPS + Entrainements
This commit is contained in:
@@ -1,4 +1,78 @@
|
||||
------- TP19 : Allocation dynamique --------
|
||||
|
||||
1.
|
||||
1. # include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
int taille_tab;
|
||||
double* tab = (double*) malloc(3*sizeof(double));
|
||||
int* tab_compteurs = (int*) malloc(sizeof(int));
|
||||
int i;
|
||||
int j;
|
||||
printf("Combien de réels souhaitez-vous entrer ? ");
|
||||
scanf("%d", &taille_tab);
|
||||
getchar();
|
||||
tab = (double*) realloc(tab, taille_tab*sizeof(double));
|
||||
tab_compteurs = (int*) realloc(tab_compteurs, taille_tab*sizeof(int));
|
||||
for (i = 0; i != taille_tab; i++) {
|
||||
printf("Entrez le %de réel : ", i + 1);
|
||||
scanf("%lf", &tab[i]);
|
||||
getchar();
|
||||
tab_compteurs[i] = 0;
|
||||
}
|
||||
|
||||
for (i = 0; i!=taille_tab; i++) {
|
||||
for (j = 0; j != taille_tab; j++) {
|
||||
if (tab[j] == tab[i]) {
|
||||
tab_compteurs[i] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i!=taille_tab; i++) {
|
||||
if (tab_compteurs[i] == 1) {
|
||||
printf("| %lf ", tab[i]);
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
2.
|
||||
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
|
||||
char* inverse(const char* s) {
|
||||
int taille_s = strlen(s);
|
||||
char* s_inverse = (char*) malloc(sizeof(char));
|
||||
int i;
|
||||
int j = 0;
|
||||
s_inverse = realloc(s_inverse, taille_s*sizeof(char));
|
||||
for (i = taille_s-1; i>0; i--) {
|
||||
s_inverse[j] = s[i];
|
||||
j++;
|
||||
}
|
||||
s_inverse[j] = s[0];
|
||||
return s_inverse;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int i;
|
||||
for (i = 1; i != argc; i++){
|
||||
if (strcmp(argv[i], inverse(argv[i])) == 0) {
|
||||
printf(argv[i]);
|
||||
printf(" est un palindrome.\n");
|
||||
}
|
||||
else {
|
||||
printf(argv[i]);
|
||||
printf(" n'est pas un palindrome.\n");
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
3.
|
||||
|
||||
|
@@ -2,31 +2,34 @@
|
||||
# include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
int taille_tab;
|
||||
int taille_tab = 0;
|
||||
double entree;
|
||||
double* tab = (double*) malloc(3*sizeof(double));
|
||||
int* tab_compteurs = (int*) malloc(sizeof(int));
|
||||
int i;
|
||||
int j;
|
||||
printf("Combien de réels souhaitez-vous entrer ? ");
|
||||
scanf("%d", &taille_tab);
|
||||
getchar();
|
||||
tab = (double*) realloc(tab, taille_tab*sizeof(double));
|
||||
for (i = 0; i != taille_tab; i++) {
|
||||
printf("Entrez le %de réel : ", i + 1);
|
||||
scanf("%lf", &tab[i]);
|
||||
while( entree != (double) 'q') {
|
||||
taille_tab++;
|
||||
tab = (double*) realloc(tab, sizeof(double));
|
||||
tab_compteurs = (int*) realloc(tab_compteurs, sizeof(int));
|
||||
printf("Entrez le %de réel : ", taille_tab);
|
||||
scanf("%lf", &entree);
|
||||
getchar();
|
||||
tab[taille_tab] = entree;
|
||||
tab_compteurs[taille_tab] = 0;
|
||||
}
|
||||
|
||||
for (i = 0; i != (taille_tab - 1); i++) {
|
||||
for (j = i; j != taille_tab; j++) {
|
||||
if (tab[i] == tab[j]) {
|
||||
tab[j] = 0.0;
|
||||
for (i = 0; i!=taille_tab; i++) {
|
||||
for (j = 0; j != taille_tab; j++) {
|
||||
if (tab[j] == tab[i]) {
|
||||
tab_compteurs[i] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i != taille_tab; i++) {
|
||||
if (tab[i] != 0.0) {
|
||||
printf("%.3f ", tab[i]);
|
||||
for (i = 0; i!=taille_tab; i++) {
|
||||
if (tab_compteurs[i] == 1) {
|
||||
printf("| %lf ", tab[i]);
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
|
Reference in New Issue
Block a user