elo

cmdline fide-elo calculator in C
Log | Files | Refs

elo.c (1151B)


      1 #include <stdio.h>
      2 #include <unistd.h>
      3 #include <stdlib.h>
      4 int roof(int *x){
      5 if (*x<-400) return -400;
      6 else if (*x>400) return 400;
      7 return *x;
      8 }
      9 int main(int argc, char **argv)
     10 
     11 {
     12 	int c;
     13 	int myelo,opprating;
     14 	int kfactor = 20;
     15 	int diff;
     16 	float res;
     17 	char *mopt=0, *eopt=0,*ropt=0, *kopt=0;
     18 	while ((c= getopt(argc,argv,"m:e:r:k:")) != -1) {
     19 		
     20 		switch (c) {
     21 			case 'm':
     22 			mopt=optarg;
     23 			break;
     24 			case 'r':
     25 			ropt=optarg;
     26 			break;
     27 			case 'e':
     28 			eopt=optarg;
     29 			break;
     30 			case 'k':
     31 			kopt=optarg;
     32 			kfactor= strtol(kopt,NULL,10);
     33 			break;
     34 			default:
     35 			printf("%s\n", "Ajabaja" );
     36 		}
     37 	}
     38 	myelo= strtol(eopt,NULL,10);
     39 	opprating= strtol(mopt,NULL,10);
     40 	res = strtof(ropt,NULL);
     41 	diff = myelo - opprating;
     42 	diff = roof(&diff);
     43 	const int caps[42] = {7,14,21,29,36,43,50,57,65,72,80,87,95,102,110,117,125,133,141,149,158,166,175,184,193,202,211,220,230,240,251,262,273,284,296,309,322,336,351,366,383,400};
     44 	int offset=0;
     45 	int p=50;
     46 	if (diff < 0) {
     47 	offset =100;
     48 		diff = -diff;
     49 	}
     50 	for (int i=0;i<42;++i) {
     51 		if (diff >= caps[i])
     52 		{p++;}
     53 
     54 	}
     55 	if (offset){ p = offset -p;}
     56 	printf("%3.2f\n", (float)kfactor*(100*res-p)/100);
     57 	exit(0);
     58 }