save
This commit is contained in:
parent
010fba24e0
commit
2b2cc8ccd6
BIN
ASR3.1/TP05 Pointeurs Signaux/a.out
Executable file
BIN
ASR3.1/TP05 Pointeurs Signaux/a.out
Executable file
Binary file not shown.
58
ASR3.1/TP05 Pointeurs Signaux/exo0.c
Normal file
58
ASR3.1/TP05 Pointeurs Signaux/exo0.c
Normal 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");
|
||||
|
||||
}
|
29
ASR3.1/TP05 Pointeurs Signaux/exo1/exo1.c
Normal file
29
ASR3.1/TP05 Pointeurs Signaux/exo1/exo1.c
Normal 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 ++;
|
||||
}
|
||||
}
|
19
ASR3.1/TP05 Pointeurs Signaux/exo1/helpers.c
Normal file
19
ASR3.1/TP05 Pointeurs Signaux/exo1/helpers.c
Normal 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;
|
||||
}
|
||||
|
6
ASR3.1/TP05 Pointeurs Signaux/exo1/helpers.h
Normal file
6
ASR3.1/TP05 Pointeurs Signaux/exo1/helpers.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef _HELPERS_H
|
||||
#define _HELPERS_H
|
||||
int set_signal_handler(int signo, void (*handler)(int));
|
||||
double tstamp(void);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user