Progression TP01

This commit is contained in:
HORVILLE 2021-11-09 15:50:11 +01:00
parent 5ec8f48e12
commit de651f92e5
4 changed files with 93 additions and 0 deletions

21
APL1.2/TP01/palindromes.c Normal file
View File

@ -0,0 +1,21 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* inverse(const char* s) {
char* inv_str = (char*) calloc(strlen(s), sizeof(char));
for (int i = strlen(s)-1; i >= 0; i--) {
inv_str[i] = s[strlen(s)-i-1];
}
return inv_str;
}
int main(int argc, char * argv[]) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], inverse(argv[i])) == 0) printf("%s est palindrome.\n", argv[i]);
else printf("%s n'est pas palindrome.\n", argv[i]);
}
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,40 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char * argv[]) {
printf("Veuillez donner vos valeurs : \n");
double* list = (double*) calloc(1, sizeof(double));
int size = 1;
do {
char reel[50];
scanf("%s", &reel);
if (reel[0] == 'q') break;
int found = 0;
double var = strtod(reel, NULL);
for (int i = 0; i < size; i++) {
if (list[i] == var) found = 1;
}
if (!found) {
list[size-1] = var;
size++;
double* new_list = (double*) realloc(list, size * sizeof(double));
list = new_list;
}
} while(1);
for (int i = 0; i < size-1; i++) {
printf("%f ", list[i]);
}
printf("\n");
free(list);
return EXIT_SUCCESS;
}

32
APL1.2/TP01/singletons.c Normal file
View File

@ -0,0 +1,32 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char * argv[]) {
if (argc < 3) {
puts("Veuillez renseigner deux valeurs ou plus.");
return EXIT_FAILURE;
}
double* list = (double*) calloc(argc-1, sizeof(double));
for (int i = 0; i < argc-1; i++) {
double val = strtod(argv[i+1], NULL);
int found = 0;
for (int j = 0; j < i; j++) {
if (list[j] == val) found = 1;
}
if (!found) {
list[i] = val;
printf("%.2f ", val);
}
}
free(list);
printf("\n");
return EXIT_SUCCESS;
}