dm2
This commit is contained in:
parent
1eb1d0d95e
commit
7f93a91a7b
171
dm2/dm2.c
Normal file
171
dm2/dm2.c
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
double fraction(int a, int b) { return (double)a / (double)b; }
|
||||||
|
|
||||||
|
bool bissextile(int year) {
|
||||||
|
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
double evalue(double coef[], int size, double x) {
|
||||||
|
double res = 0;
|
||||||
|
while (size > 0) {
|
||||||
|
size -= 1;
|
||||||
|
res *= x;
|
||||||
|
res += coef[size];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ecartmin(int t[], int size) {
|
||||||
|
int min = abs(t[size - 1] - t[size - 2]);
|
||||||
|
while (size > 2) {
|
||||||
|
size -= 1;
|
||||||
|
int ecart = abs(t[size - 1] - t[size - 2]);
|
||||||
|
if (ecart < min) {
|
||||||
|
min = ecart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 0 if no integer has been detected
|
||||||
|
int extraire(char str[]) {
|
||||||
|
while (*str != '-' && (*str <= '0' || *str >= '9')) {
|
||||||
|
if (*str == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
str += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int signe = 1 - (2 * (*str == '-'));
|
||||||
|
str += *str == '-';
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
while (*str >= '0' && *str <= '9') {
|
||||||
|
res *= 10;
|
||||||
|
res += *str - '0';
|
||||||
|
str += 1;
|
||||||
|
}
|
||||||
|
return res * signe;
|
||||||
|
}
|
||||||
|
|
||||||
|
double sommemajo(double t[], int size) {
|
||||||
|
int signe = 0;
|
||||||
|
double pos = 0.;
|
||||||
|
double neg = 0.;
|
||||||
|
while (size > 0) {
|
||||||
|
size -= 1;
|
||||||
|
double x = t[size];
|
||||||
|
if (x > 0) {
|
||||||
|
signe += 1;
|
||||||
|
pos += x;
|
||||||
|
} else if (x < 0) {
|
||||||
|
signe -= 1;
|
||||||
|
neg += x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (pos * (signe > 0)) + (neg * (signe < 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only works with ASCII
|
||||||
|
int nbdiff(char str[]) {
|
||||||
|
int chars[126] = {0};
|
||||||
|
while (*str) {
|
||||||
|
chars[*str - 1] += 1;
|
||||||
|
str += 1;
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
char c = 1;
|
||||||
|
while (c < 126) {
|
||||||
|
count += chars[c] > 0;
|
||||||
|
c += 1;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool palindromeint(int a) {
|
||||||
|
int temp = a;
|
||||||
|
int rev = 0;
|
||||||
|
while (temp) {
|
||||||
|
rev *= 10;
|
||||||
|
rev += temp % 10;
|
||||||
|
temp /= 10;
|
||||||
|
}
|
||||||
|
return a == rev;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only works with alphabetical strings
|
||||||
|
bool palindrome2(char str[]) {
|
||||||
|
int size = strlen(str);
|
||||||
|
while (size > 1) {
|
||||||
|
char c1 = *str;
|
||||||
|
c1 += ('a' - 'A') * (c1 <= 'Z');
|
||||||
|
char c2 = str[size - 1];
|
||||||
|
c2 += ('a' - 'A') * (c2 <= 'Z');
|
||||||
|
if (c1 != c2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
str += 1;
|
||||||
|
size -= 2;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sommeentiers(char str[]) {
|
||||||
|
int count = 0;
|
||||||
|
while (true) {
|
||||||
|
while (*str != '-' && (*str <= '0' || *str >= '9')) {
|
||||||
|
if (*str == 0) {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
str += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int signe = 1 - (2 * (*str == '-'));
|
||||||
|
str += *str == '-';
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
while (*str >= '0' && *str <= '9') {
|
||||||
|
res *= 10;
|
||||||
|
res += *str - '0';
|
||||||
|
str += 1;
|
||||||
|
}
|
||||||
|
count += signe * res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("-2/3 = %f\n\n", fraction(-2, 3));
|
||||||
|
|
||||||
|
printf("2004 %s bissextile\n", bissextile(2004) ? "est" : "n'est pas");
|
||||||
|
printf("2100 %s bissextile\n\n", bissextile(2100) ? "est" : "n'est pas");
|
||||||
|
|
||||||
|
printf("Avec x=-3.716309026, 2.5x² - 2x + 0.04=%f\n\n",
|
||||||
|
evalue((double[3]){0.04, -2., 2.5}, 3, -3.716309026));
|
||||||
|
|
||||||
|
printf("min([2, 5, 89, 3]) = %d\n\n", ecartmin((int[4]){2, 5, 89, 3}, 4));
|
||||||
|
|
||||||
|
printf("Le premier nombre dans \"eia42snt-134eius34)\" est %d\n\n",
|
||||||
|
extraire("eia42snt-134eius34)"));
|
||||||
|
|
||||||
|
printf("sommemajo([80, -2, -1])=%f\n\n",
|
||||||
|
sommemajo((double[3]){80., -2., -1.}, 3));
|
||||||
|
|
||||||
|
printf("\"eia42snt-134eius34)\" a %d caractères différents\n\n",
|
||||||
|
nbdiff("eia42snt-134eius34)"));
|
||||||
|
|
||||||
|
printf("42 %s un palindrome\n", palindromeint(42) ? "est" : "n'est pas");
|
||||||
|
printf("123454321 %s un palindrome\n\n",
|
||||||
|
palindromeint(123454321) ? "est" : "n'est pas");
|
||||||
|
|
||||||
|
printf("\"eiasnteius\" %s un palindrome\n",
|
||||||
|
palindrome2("eiasnteius") ? "est" : "n'est pas");
|
||||||
|
printf("\"Kayak\" %s un palindrome\n\n",
|
||||||
|
palindrome2("Kayak") ? "est" : "n'est pas");
|
||||||
|
|
||||||
|
printf("La somme des entiers dans \"eia42snt-134eius34)\" est %d\n",
|
||||||
|
sommeentiers("eia42snt-134eius34)"));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user