dm3 jusqu'à cesar

This commit is contained in:
Arkitu 2025-10-28 23:04:48 +01:00
parent 19a80513a2
commit 2ee32997e4

View File

@ -1,6 +1,8 @@
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
int spitze(const bool t[], int size) {
@ -74,3 +76,87 @@ void derive(double t[], int size) {
}
t[size - 1] = 0;
}
double *multpol(const double t1[], const double t2[], int size1, int size2) {
if (size1 == 0 || size2 == 0) {
return NULL;
}
int size3 = size1 - 1 + size2;
double *t3 = malloc(size3 * sizeof(double));
// On s'économise l'initialisation
for (int i = 0; i < size1; i += 1) {
t3[i] = t1[i] * t2[0];
}
for (int j = 1; j < size2; j += 1) {
t3[j + size1 - 1] = t1[size1 - 1] * t2[j];
}
for (int i = 0; i < size1 - 1; i += 1) {
for (int j = 1; j < size2; j += 1) {
t3[i + j] += t1[i] * t2[j];
}
}
return t3;
}
// int main() {
// double t1[] = {2.1, 3.1, 5.1};
// double t2[] = {2, 3};
// double t3[] = {4.2, 12.5, 19.5, 15.299999999999999};
// double *res = multpol(t1, t2, 3, 2);
// for (int i = 0; i < 4; i += 1) {
// assert(t3[i] == res[i]);
// }
// free(res);
// }
char *cesar(const char str[], int a, int b) {
char map[26];
// C'est moche mais ça ne change pas la complexité. Sur de longues
// chaines ça doit même être plus efficace de calculer une map à l'avance.
for (char c = 0; c < 26; c += 1) {
int r = ((c * a) + b) % 26;
if (r < 0) {
r += 26;
}
for (char i = 0; i < c; i += 1) {
if (map[i] == r) {
assert(false && "la fonction n'est pas bijective");
}
}
map[c] = r;
}
// On peut faire plus efficace en consommant str et en stockant res dedans
char *res = malloc(strlen(str) * sizeof(char));
for (int i = 0; str[i] != 0; i += 1) {
if (str[i] >= 'a' && str[i] <= 'z') {
res[i] = 'a' + map[(str[i] - 'a')];
} else if (str[i] >= 'A' && str[i] <= 'Z') {
res[i] = 'A' + map[(str[i] - 'A')];
} else {
res[i] = str[i];
}
}
return res;
}
// int main() {
// char chaine1[] = "Hello world!";
// char *res1 = cesar(chaine1, -3, -5);
// printf("resultat 1 : %s\n", res1);
// free(res1);
// int c = 13;
// int d = 5;
// char *res2 = cesar(chaine1, c, d);
// printf("resultat 2 : %s \n", res2);
// free(res2);
// exit(0);
// }