This commit is contained in:
lefevre 2021-11-15 17:44:02 +01:00
parent 010fba24e0
commit 2b2cc8ccd6
5 changed files with 112 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,58 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*La fonction map permet de parcourir le tableau. */
void map(void *base,size_t n ,size_t size_elem,void(*f)(void *)){
int i;
char *ptr = (char*)base;
for(i=0;i<n;i++)
f((void*)(ptr+i*size_elem));
}
void print_int (void * i) {
printf("%d\n", *(int *)i ); // On force à le mettre en int
}
int cmp_int (const void* first, const void* second ) {
int firstInt = *(const int*)first;
int secondInt = *(const int*)second;
return firstInt - secondInt;
}
void print_string (void * s) {
printf ("%s", *(char* *)s ); // char* * => un pointeur sur un tableau de caractère
}
int cmp_string (const void* a, const void* b) {
const char* *ia = (const char* *)a;
const char* *ib = (const char* *)b;
return strcmp(*ia, *ib);
}
int main(){
int t1[10]={12,-7,1,-16,3,19,7,1,5,0};
char * t2[]={"chou","joujou","bijou","genou",
"caillou","hibou","pou"};
map(t1,10,sizeof(int),print_int);
printf("\n");
map(t2,7,sizeof(char*),print_string);
printf("\n");
qsort(t1,10,sizeof(int),cmp_int);
map(t1,10,sizeof(int),print_int);
printf("\n");
qsort(t2,7,sizeof(char *),cmp_string);
map(t2,7,sizeof(char*),print_string);
printf("\n");
}

View File

@ -0,0 +1,29 @@
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<stdint.h>
#include<assert.h>
#include "helpers.h"
uint64_t tirs=0,
tirs_in=0;
double pi=0,
t1;
int main(int argc,char * argv[]){
double x,
y;
t1 = tstamp();
alarm(5);
while(1){
x = ((double)rand())/(double)RAND_MAX;
y = ((double)rand())/(double)RAND_MAX;
tirs ++;
if (x*x+y*y<=1) tirs_in ++;
}
}

View File

@ -0,0 +1,19 @@
#include "helpers.h"
#include <signal.h>
#include <time.h>
int set_signal_handler(int signo, void (*handler)(int)) {
struct sigaction sa;
sa.sa_handler = handler; // call `handler` on signal
sigemptyset(&sa.sa_mask); // don't block other signals in handler
sa.sa_flags = SA_RESTART; // restart system calls
return sigaction(signo, &sa, NULL);
}
double tstamp(void) {
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
}

View File

@ -0,0 +1,6 @@
#ifndef _HELPERS_H
#define _HELPERS_H
int set_signal_handler(int signo, void (*handler)(int));
double tstamp(void);
#endif