Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
dwzcalculator/dwzcalculator.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
166 lines (129 sloc)
3.01 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<math.h> | |
#include<time.h> | |
#include<stdio.h> | |
#include<stdlib.h> | |
#define min(a,b) (a < b ? a : b) | |
/*#define nelem(x) (sizeof(x) / sizeof((x)[0]))*/ | |
void usage(void) | |
{ | |
printf("usage: dwzcalculator DWZ INDEX YEAR_OF_BIRTH NO_OPPONENTS OPPONENTS_DWZ[] POINTS\n"); | |
} | |
int get_J(age) | |
int age; | |
{ | |
int J; | |
if(age <= 20) | |
J = 5; | |
else if (age <= 25) | |
J = 10; | |
else | |
J = 15; | |
return J; | |
} | |
int get_E(age, dwz, index, J, W, We) | |
int age; | |
int dwz; | |
int index; | |
int J; | |
double W; | |
double We; | |
{ | |
double a0, a = 1, B, E0; | |
int E; | |
if (age < 20){ | |
a0 = dwz / 2000.0; | |
if (a0 < 0.5 || a0 > 1) | |
a = a0; | |
} | |
E0 = pow(dwz / 1000.0, 4) + J; | |
if (dwz < 1300 && (double) W <= We) | |
B = exp((1300 - dwz) / 150) - 1; | |
else | |
B = 0; | |
E = round(a * E0 + B); | |
if (B == 0) | |
E = min(E, min(30, 5*index)); | |
else | |
E = min(E, 150); | |
return E; | |
} | |
int get_age(year_of_birth) | |
int year_of_birth; | |
{ | |
int age, year; | |
time_t now; | |
struct tm *mt; | |
if (time(&now) != (time_t)(-1)){ | |
mt = localtime(&now); | |
year = mt->tm_year + 1900; | |
} else { | |
return 26; | |
} | |
age = year - year_of_birth; | |
return age; | |
} | |
int calculate_dwz(dwz, index, year_of_birth, nopp, opponent_dwz, points) | |
int dwz; | |
int index; | |
int year_of_birth; | |
int nopp; | |
int* opponent_dwz; | |
double points; | |
{ | |
double W = points, We; | |
double opponent_average = 0.0; | |
int age, new_dwz, D, E, J; | |
int n = nopp; | |
int i; | |
if (points == 0) | |
exit(EXIT_FAILURE); | |
for (i = 0; i < n; ++i) | |
opponent_average += opponent_dwz[i]; | |
opponent_average /= n; | |
D = dwz - opponent_average; | |
We = n * (1 / (1 + pow(10.0, (double)(-D / 400.0)))); | |
age = get_age(year_of_birth); | |
J = get_J(age); | |
E = get_E(age, dwz, index, J, W, We); | |
new_dwz = dwz + (800 / (E + n)) * (W - We); | |
return new_dwz; | |
} | |
int main(argc, argv) | |
int argc; | |
char** argv; | |
{ | |
double points = -1.0; | |
int dwz, index, year, nopp, *opp = NULL; | |
int new_dwz, i; | |
if(argc < 7){ | |
fprintf(stderr, "ERROR: not enough parameters!!!\n"); | |
usage(); | |
exit(EXIT_FAILURE); | |
} | |
dwz = atoi(argv[1]); | |
index = atoi(argv[2]); | |
year = atoi(argv[3]); | |
nopp = atoi(argv[4]); | |
if (nopp <= 0 || nopp > 10){ | |
fprintf(stderr, "ERROR: no possible number of opponents!!!\n"); | |
usage(); | |
exit(EXIT_FAILURE); | |
} | |
if (nopp + 6 != argc){ | |
fprintf(stderr, "ERROR: wrong number of arguments!!!\n"); | |
usage(); | |
exit(EXIT_FAILURE); | |
} | |
opp = (int *) calloc(nopp, sizeof(int)); | |
if (opp == NULL){ | |
fprintf(stderr, "ERROR: could not allocate enough memory!!!\n"); | |
exit(EXIT_FAILURE); | |
} | |
for (i = 0; i < nopp; ++i) | |
opp[i] = atoi(argv[5+i]); | |
points = atof(argv[argc-1]); | |
new_dwz = calculate_dwz(dwz, index, year, nopp, opp, points); | |
free(opp); | |
printf("DWZ %d --> %d(%d)\n", dwz, new_dwz, new_dwz-dwz); | |
return EXIT_SUCCESS; | |
} | |