Code: Select all
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <signal.h>
//////////////////////////////////////////////////////
// // //
// WhoIsBest.cpp // //
// // //
// Rémi Coulom // compile as C++ //
// // //
// November, 2002 // //
// // //
//////////////////////////////////////////////////////
float ProbaBinom(int n1, int n0)
{ float *pi = new float[n1 + 2];
pi[0] = 0;
for (int i = n1 + 1; --i >= 0;)
pi[i + 1] = 1;
for (int j = n0 + 1; --j >= 0;)
for (int i = 0; i <= n1; i++)
pi[i + 1] = (pi[i + 1] + pi[i]) * 0.5;
float Result = pi[n1 + 1];
delete[] pi;
return Result*100;
}
main (argc,argv)
int argc; char *argv[];
{ int won=0; int loss=0; int draw=0; char s[10000],e1[100],e2[100]; FILE *fp; int game=0; int x,l; float f1,f2,f3,f4;
int WON,LOSS;
printf("LOS calculator\n\n");
if (argc==2) goto pgn;
loop: printf("Type number of won games "); gets(s); won=atoi(s); WON=won*2;
printf("Type number of lost games "); gets(s); loss=atoi(s); LOSS=loss*2;
printf("Type number of draw games "); gets(s); draw=atoi(s);
printf("\n\nLOS without draws = %.1f%%",ProbaBinom(won,loss));
printf("\nLOS win as double = %.1f%%\n\n",ProbaBinom(WON,LOSS));
f1=won; f2=draw; f3=f1+(f2/2); won=f3; WON=won*2;
f1=loss; f2=draw; f4=f1+(f2/2); loss=f4; LOSS=loss*2;
printf("LOS with draws = %.1f%%\n",ProbaBinom(won,loss));
printf("LOS win as double = %.1f%%\n\n",ProbaBinom(WON,LOSS));
goto loop;
pgn: fp=fopen(argv[1],"r"); if (fp==NULL) { printf("File %s not present",argv[1]); getch(); exit(1); }
// Parse PGN
// =========
next: if (feof(fp)) goto finito;
fgets(s,7777,fp); strlwr(s);
if (game==0 && strstr(s,"[white ")) { strcpy(e1,s); l=strlen(e1); memmove(e1,e1+8,l); l=strlen(e1); e1[l-3]=0; }
if (game==1 && strstr(s,"[black ")) { strcpy(e2,s); l=strlen(e2); memmove(e2,e2+8,l); l=strlen(e2); e2[l-3]=0; }
if (strstr(s,"[white ")) { x=0; game++; if ((game & 255)==0) printf("."); } // new game
if (strstr(s,e1) && strstr(s,"[white ")) x=1;
if (strstr(s,e1) && strstr(s,"[black ")) x=2;
if (strstr(s,"1-0") && strstr(s,"[result") && x==1) won++;
if (strstr(s,"0-1") && strstr(s,"[result") && x==2) won++;
if (strstr(s,"1-0") && strstr(s,"[result") && x==2) loss++;
if (strstr(s,"0-1") && strstr(s,"[result") && x==1) loss++;
if (strstr(s,"1/2-1/2") && strstr(s,"[result")) draw++;
goto next;
finito: fclose(fp);
f1=won; f2=draw; f3=f1+(f2/2);
f1=loss; f2=draw; f4=f1+(f2/2);
printf("\n\n%s-%s (G=%d) [W=%d] [L=%d] [D=%d] Score %.1f-%.1f\n\n",e1,e2,game,won,loss,draw,f3,f4);
printf("LOS without draws = %.1f%%\n",ProbaBinom(won,loss));
printf("LOS win is double = %.1f%%\n\n",ProbaBinom(won*2,loss*2));
won=f3; loss=f4;
printf("LOS with draws = %.1f%%\n",ProbaBinom(won,loss));
printf("LOS win is double = %.1f%%\n\n",ProbaBinom(won*2,loss*2));
getch();
}