diff --git a/exemples/Makefile b/exemples/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..2c0d498f45a0dfa21e02a9168b8d9506d8611b37 --- /dev/null +++ b/exemples/Makefile @@ -0,0 +1,12 @@ +CC=gcc +OPTIONS=-Wall -Wextra -pedantic -fsanitize=address +LIBS=-fsanitize=address -lm + +alot_of_scanfs: alot_of_scanfs.o + $(CC) -o $@ $< $(LIBS) + +alot_of_scanfs.o: alot_of_scanfs.c + $(CC) -c $^ $(OPTIONS) + +clean: + rm -f *.o alot_of_scanfs diff --git a/exemples/alot_of_scanfs.c b/exemples/alot_of_scanfs.c new file mode 100644 index 0000000000000000000000000000000000000000..4ca876d6b80dc0043fef4c3d48c0ad47764f1b7e --- /dev/null +++ b/exemples/alot_of_scanfs.c @@ -0,0 +1,96 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// Ce programme prend en argument deux entiers se trouvant chacun +// sur une nouvelle ligne et affiche +// la somme des deux entiers en argument sur une nouvelle ligne. + +// Ex: +// 12 +// 19 +// +// 31 + +void sum_two() { + int a, b; + scanf("%d %d", &a, &b); + + printf("\n%d\n", a + b); +} + +// Ce programme prend en argument 12 nombres à virgule flottante se trouvant chacun +// sur une nouvelle ligne. Multiplie chaque nombre par deux et affiche leur somme +// sur une nouvelle ligne suivi de CHF. + +// Ex: +// 12.2 +// 45.5 +// 1.5 +// 65.1 +// 89.4 +// 567.6 +// 112.8 +// 67.0 +// 35.1 +// 112.2 +// 3.3 +// 9.8 +// +// 2243.000000 CHF + +void sum_array() { + float sum = 0.0; + for (int i = 0; i < 12; ++i) { + float a = 0.0; + scanf("%f", &a); + a *= 2.0; + sum += a; + } + + printf("\n%f CHF\n", sum); +} + +// Ce programme prend en argument 2 chaînes de caractères sur des lignes séparées (longueur max de 80), +// les sépare au milieu et retourne les 4 chaînes chacune sur une nouvelle ligne +// (si la longueur N est paire on sépare en 2 chaînes de longueur N/2, sinon la première +// aura une longueur de N/2 et la seconde N/2+1). + +// Ex: +// abcdefgh +// asdfghjkl +// +// abcd +// efgh +// asdf +// ghjkl + +void split_mid() { + char str_one[2][41], str_two[2][41]; + for (int j = 0; j < 2; ++j) { + char str[81]; + scanf("%s", str); + int n = strlen(str); + int n1 = n / 2; + int n2 = n - n1; + for (int i = 0; i < n1; ++i) { + str_one[j][i] = str[i]; + } + str_one[j][n1] = '\0'; + for (int i = 0; i < n2; ++i) { + str_two[j][i] = str[n1 + i]; + } + str_two[j][n2] = '\0'; + } + printf("\n"); + for (int j = 0; j < 2; ++j) { + printf("%s\n", str_one[j]); + printf("%s\n", str_two[j]); + } +} + +int main() { + sum_two(); + sum_array(); + split_mid(); +} \ No newline at end of file