60 lines
1.1 KiB
C
60 lines
1.1 KiB
C
|
#include<unistd.h>
|
||
|
#include<stdio.h>
|
||
|
#include<stdlib.h>
|
||
|
#include<signal.h>
|
||
|
#include<stdint.h>
|
||
|
#include<assert.h>
|
||
|
#include "helpers.h"
|
||
|
#include "helpers.c"
|
||
|
|
||
|
uint64_t shots = 0,
|
||
|
shots_in = 0;
|
||
|
|
||
|
double pi = 0, t1;
|
||
|
|
||
|
void int_handler(int){
|
||
|
alarm(0);
|
||
|
char c;
|
||
|
printf("Le programme tourne depuis %f secondes, voulez-vous vraiment l'arrêter ? (O)ui / (N)on", tstamp() - t1);
|
||
|
c = getchar();
|
||
|
if (c == 'O' || c == 'o' || c == 'Y' || c == 'y'){
|
||
|
exit(0);
|
||
|
}
|
||
|
else{
|
||
|
return;
|
||
|
alarm(5);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void alarm_handler(int){
|
||
|
printf("Valeur de pi en cours = %f\n", pi);
|
||
|
printf("Nombre de tirs effectués = %f\n", (double) shots);
|
||
|
alarm(5);
|
||
|
}
|
||
|
|
||
|
void quit_handler(int){
|
||
|
alarm(0);
|
||
|
shots = 0;
|
||
|
shots_in = 0;
|
||
|
t1 = tstamp();
|
||
|
alarm(5);
|
||
|
}
|
||
|
|
||
|
int main(int argc,char * argv[])
|
||
|
{
|
||
|
double x,y;
|
||
|
t1 = tstamp();
|
||
|
alarm(5);
|
||
|
set_signal_handler(SIGALRM, &alarm_handler);
|
||
|
set_signal_handler(SIGINT, &int_handler);
|
||
|
set_signal_handler(SIGQUIT, &quit_handler);
|
||
|
while(1){
|
||
|
x = ((double)rand())/(double)RAND_MAX;
|
||
|
y = ((double)rand())/(double)RAND_MAX;
|
||
|
shots ++;
|
||
|
if ((x*x+y*y) <= 1)
|
||
|
shots_in ++;
|
||
|
pi = shots_in * 1.0 / shots * 4;
|
||
|
}
|
||
|
}
|