Advertisement
Guest User

steve

a guest
Nov 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <math.h>
  10. #include <limits.h>
  11. #include "manfut.h"
  12.  
  13. #define GetPorter(j) (Jugadors[j])
  14. #define GetDefensor(j) (Jugadors[NPorters+j])
  15. #define GetMitg(j) (Jugadors[NPorters+NDefensors+j])
  16. #define GetDelanter(j) (Jugadors[NPorters+NDefensors+NMitjos+j])
  17.  
  18. char *color_red = "\033[01;31m";
  19. char *color_green = "\033[01;32m";
  20. char *color_blue = "\033[01;34m";
  21. char *end_color = "\033[00m";
  22.  
  23. // Definition functions prototype
  24. void LlegirFitxerJugadors(char *pathJugadors);
  25. void CalcularEquipOptim(long int presupostFitxatges, PtrJugadorsEquip MillorEquip);
  26. TBoolean ObtenirJugadorsEquip (TEquip equip, PtrJugadorsEquip jugadors);
  27. TEquip GetEquipInicial();
  28. TBoolean JugadorsRepetits(TJugadorsEquip jugadors);
  29. int CostEquip(TJugadorsEquip equip);
  30. int PuntuacioEquip(TJugadorsEquip equip);
  31. void error(char *str);
  32. unsigned int Log2(unsigned long long int n);
  33. void PrintJugadors();
  34. void PrintEquipJugadors(TJugadorsEquip equip);
  35.  
  36. // Global variables definition
  37. TJugador Jugadors[DMaxJugadors];
  38. int NJugadors, NPorters, NDefensors, NMitjos, NDelanters;
  39. char cad[256];
  40.  
  41. int main(int argc, char *argv[])
  42. {
  43. TJugadorsEquip MillorEquip, AuxEquip;
  44. long int presupostFitxatges;
  45. float IntervalBegin=-1, IntervalEnd=-1;
  46.  
  47. if (argc<3)
  48. error("Error in arguments: ManFut <presupost> <fitxer_jugadors>");
  49.  
  50. if (argc>1)
  51. presupostFitxatges = atoi(argv[1]);
  52.  
  53. if (argc>2)
  54. LlegirFitxerJugadors(argv[2]);
  55.  
  56. // Calculate the best team.
  57. CalcularEquipOptim(presupostFitxatges, &MillorEquip);
  58. write(1,color_blue,strlen(color_blue));
  59. write(1,"-- Best Team -------------------------------------------------------------------------------------\n",strlen("-- Best Team -------------------------------------------------------------------------------------\n"));
  60. PrintEquipJugadors(MillorEquip);
  61. sprintf(cad," Cost %d, Points: %d.\n", CostEquip(MillorEquip), PuntuacioEquip(MillorEquip));
  62. write(1,cad,strlen(cad));
  63. write(1,"-----------------------------------------------------------------------------------------------------\n",strlen("-----------------------------------------------------------------------------------------------------\n"));
  64. write(1,end_color,strlen(end_color));
  65.  
  66. exit(0);
  67. }
  68.  
  69.  
  70. // Read file with the market players list (each line containts a plater: "Id.;Name;Position;Cost;Team;Points")
  71. void LlegirFitxerJugadors(char *pathJugadors)
  72. {
  73. char buffer[256], tipus[10];
  74. int fdin;
  75. int nofi;
  76.  
  77. if ((fdin=open(pathJugadors, O_RDONLY)) < 0)
  78. error("Error opening input file.");
  79.  
  80. // Read players.
  81. NJugadors=NPorters=NDefensors=NMitjos=NDelanters=0;
  82. do
  83. {
  84.  
  85. int x=0,i,f;
  86.  
  87. while((nofi=read(fdin,&buffer[x],1))!=0 && buffer[x++]!='\n');
  88. buffer[x]='\0';
  89.  
  90. if (buffer[0]=='#') continue;
  91.  
  92. // Player's identificator
  93. i=0;
  94. for (f=0;buffer[f]!=';';f++);
  95. buffer[f]=0;
  96. Jugadors[NJugadors].id = atoi(&(buffer[i]));
  97.  
  98. // Player's name
  99. i=++f;
  100. for (;buffer[f]!=';';f++);
  101. buffer[f]=0;
  102. strcpy(Jugadors[NJugadors].nom,&(buffer[i]));
  103.  
  104. // Player's position
  105. i=++f;
  106. for (;buffer[f]!=';';f++);
  107. buffer[f]=0;
  108. if (strcmp(&(buffer[i]),"Portero")==0)
  109. {
  110. NPorters++;
  111. Jugadors[NJugadors].tipus=JPorter;
  112. }
  113. else if (strcmp(&(buffer[i]),"Defensa")==0)
  114. {
  115. NDefensors++;
  116. Jugadors[NJugadors].tipus=JDefensor;
  117. }
  118. else if (strcmp(&(buffer[i]),"Medio")==0)
  119. {
  120. NMitjos++;
  121. Jugadors[NJugadors].tipus=JMitg;
  122. }
  123. else if (strcmp(&(buffer[i]),"Delantero")==0)
  124. {
  125. NDelanters++;
  126. Jugadors[NJugadors].tipus=JDelanter;
  127. }
  128. else error("Error player type.");
  129.  
  130.  
  131. // Player's cost
  132. i=++f;
  133. for (f=0;buffer[f]!=';';f++);
  134. buffer[f]=0;
  135. Jugadors[NJugadors].cost = atoi(&(buffer[i]));
  136.  
  137. // Player's team
  138. i=++f;
  139. for (f=0;buffer[f]!=';';f++);
  140. buffer[f]=0;
  141. strcpy(Jugadors[NJugadors].equip,&(buffer[i]));
  142.  
  143. // Player's points
  144. i=++f;
  145. for (f=0;buffer[f]!='\n';f++);
  146. buffer[f]=0;
  147. Jugadors[NJugadors].punts = atoi(&(buffer[i]));
  148.  
  149. NJugadors++;
  150. }
  151. while(nofi);
  152.  
  153. sprintf(cad,"Number of players: %d, Port:%d, Def:%d, Med:%d, Del:%d.\n",NJugadors, NPorters, NDefensors, NMitjos, NDelanters);
  154. write(1,cad,strlen(cad));
  155.  
  156. close(fdin);
  157. }
  158.  
  159.  
  160.  
  161. void CalcularEquipOptim(long int presupostFitxatges, PtrJugadorsEquip MillorEquip)
  162. {
  163. printf("presupost: %li\n", presupostFitxatges);
  164. unsigned int maxbits;
  165. TEquip equip, primerEquip, ultimEquip, first, end;
  166. int MaxPuntuacio=-1;
  167.  
  168. // Calculated number of bits required for all teams codification.
  169. maxbits=Log2(NPorters)*DPosPorters+Log2(NDefensors)*DPosDefensors+Log2(NMitjos)*DPosMitjos+Log2(NDelanters)*DPosDelanters;
  170. if (maxbits>Log2(ULLONG_MAX))
  171. error("The number of player overflow the maximum width supported.");
  172.  
  173. // Calculate first and end team that have to be evaluated.
  174. first=primerEquip=GetEquipInicial();
  175. end=ultimEquip=pow(2,maxbits);
  176.  
  177. // Evaluating different teams/combinations.
  178. sprintf (cad,"Evaluating form %llXH to %llXH (Maxbits: %d). Evaluating %lld teams...\n",first,end, maxbits,end-first);
  179. write(1,cad,strlen(cad));
  180. for (equip=first;equip<=end;equip++)
  181. {
  182. TJugadorsEquip jugadors;
  183.  
  184. // Get playes from team number. Returns false if the team is not valid.
  185. if (!ObtenirJugadorsEquip(equip, &jugadors))
  186. continue;
  187.  
  188. sprintf(cad,"Team %lld ->",equip);
  189. write(1,cad,strlen(cad));
  190.  
  191. // Reject teams with repeated players.
  192. if (JugadorsRepetits(jugadors))
  193. {
  194. sprintf(cad,"%s Invalid.\r%s", color_red, end_color);
  195. write(1,cad,strlen(cad));
  196. continue; // Equip no valid.
  197. }
  198.  
  199. // Chech if the team points is bigger than current optimal team, then evaluate if the cost is lower than the available budget
  200. if (PuntuacioEquip(jugadors)>MaxPuntuacio && CostEquip(jugadors)<presupostFitxatges)
  201. {
  202. // We have a new partial optimal team.
  203. MaxPuntuacio=PuntuacioEquip(jugadors);
  204. memcpy(MillorEquip,&jugadors,sizeof(TJugadorsEquip));
  205. sprintf(cad,"%s Cost: %d Points: %d. %s\n", color_green, CostEquip(jugadors), PuntuacioEquip(jugadors), end_color);
  206. write(1,cad,strlen(cad));
  207. }
  208. else
  209. {
  210. sprintf(cad," Cost: %d Points: %d. \r", CostEquip(jugadors), PuntuacioEquip(jugadors));
  211. write(1,cad,strlen(cad));
  212. }
  213. }
  214. }
  215.  
  216. // Calculate the initial team combination.
  217. TEquip
  218. GetEquipInicial()
  219. {
  220. int p;
  221. TEquip equip=0, equip2=0;
  222. unsigned bitsPorters, bitsDefensors, bitsMitjos, bitsDelanters;
  223.  
  224. bitsPorters = Log2(NPorters);
  225. bitsDefensors = Log2(NDefensors);
  226. bitsMitjos = Log2(NMitjos);
  227. bitsDelanters = Log2(NDelanters);
  228.  
  229. for (p=DPosDelanters-1;p>=0;p--)
  230. {
  231. equip+=p;
  232. equip = equip << bitsDelanters;
  233. }
  234.  
  235. for (p=DPosMitjos-1;p>=0;p--)
  236. {
  237. equip+=p;
  238. equip = equip << bitsMitjos;
  239. }
  240.  
  241. for (p=DPosDefensors-1;p>=0;p--)
  242. {
  243. equip+=p;
  244. equip = equip << bitsDefensors;
  245. }
  246.  
  247. for (p=DPosPorters-1;p>0;p--)
  248. {
  249. equip+=p;
  250. equip = equip << bitsPorters;
  251. }
  252.  
  253. return (equip);
  254. }
  255.  
  256.  
  257. // Convert team combinatio to an struct with all the player by position.
  258. // Returns false if the team is not valid.
  259.  
  260. TBoolean
  261. ObtenirJugadorsEquip (TEquip equip, PtrJugadorsEquip jugadors)
  262. {
  263. int p;
  264. unsigned bitsPorters, bitsDefensors, bitsMitjos, bitsDelanters;
  265.  
  266. bitsPorters = Log2(NPorters);
  267. bitsDefensors = Log2(NDefensors);
  268. bitsMitjos = Log2(NMitjos);
  269. bitsDelanters = Log2(NDelanters);
  270.  
  271. for (p=0;p<DPosPorters;p++)
  272. {
  273. jugadors->Porter[p]=(equip>>(bitsPorters*p)) & ((int)pow(2,bitsPorters)-1);
  274. if (jugadors->Porter[p]>=NPorters)
  275. return False;
  276. }
  277.  
  278. for (p=0;p<DPosDefensors;p++)
  279. {
  280. jugadors->Defensors[p]=(equip>>((bitsPorters*DPosPorters)+(bitsDefensors*p))) & ((int)pow(2,bitsDefensors)-1);
  281. if (jugadors->Defensors[p]>=NDefensors)
  282. return False;
  283. }
  284.  
  285. for (p=0;p<DPosMitjos;p++)
  286. {
  287. jugadors->Mitjos[p]=(equip>>((bitsPorters*DPosPorters)+(bitsDefensors*DPosDefensors)+(bitsMitjos*p))) & ((int)pow(2,bitsMitjos)-1);
  288. if (jugadors->Mitjos[p]>=NMitjos)
  289. return False;
  290. }
  291.  
  292. for (p=0;p<DPosDelanters;p++)
  293. {
  294. jugadors->Delanters[p]=(equip>>((bitsPorters*DPosPorters)+(bitsDefensors*DPosDefensors)+(bitsMitjos*DPosMitjos)+(bitsDelanters*p))) & ((int)pow(2,bitsDelanters)-1);
  295. if (jugadors->Delanters[p]>=NDelanters)
  296. return False;
  297. }
  298.  
  299. return True;
  300. }
  301.  
  302.  
  303. // Check if the team have any repeated player.
  304. // Returns true if the team have repeated players.
  305.  
  306. TBoolean
  307. JugadorsRepetits(TJugadorsEquip jugadors)
  308. {
  309. // Returns True if the equip have some repeated players (is not valid).
  310. int i,j;
  311.  
  312. // Porters.
  313. for(i=0;i<DPosPorters-1;i++)
  314. for(j=i+1;j<=DPosPorters-1;j++)
  315. if (jugadors.Porter[i]==jugadors.Porter[j])
  316. return True;
  317.  
  318. // Defensors.
  319. for(i=0;i<DPosDefensors-1;i++)
  320. for(j=i+1;j<=DPosDefensors-1;j++)
  321. if (jugadors.Defensors[i]==jugadors.Defensors[j])
  322. return True;
  323.  
  324. // Mitjos.
  325. for(i=0;i<DPosMitjos-1;i++)
  326. for(j=i+1;j<=DPosMitjos-1;j++)
  327. if (jugadors.Mitjos[i]==jugadors.Mitjos[j])
  328. return True;
  329.  
  330. // Delanters
  331. for(i=0;i<DPosDelanters-1;i++)
  332. for(j=i+1;j<=DPosDelanters-1;j++)
  333. if (jugadors.Delanters[i]==jugadors.Delanters[j])
  334. return True;
  335.  
  336. return False;
  337. }
  338.  
  339.  
  340. // Calculates the team cost adding the individual cost of all team players.
  341. // Returns the cost.
  342.  
  343. int
  344. CostEquip(TJugadorsEquip equip)
  345. {
  346. int x;
  347. int cost=0;
  348.  
  349. for(x=0;x<DPosPorters;x++)
  350. cost += GetPorter(equip.Porter[x]).cost;
  351.  
  352. for(x=0;x<DPosDefensors;x++)
  353. cost += GetDefensor(equip.Defensors[x]).cost;
  354.  
  355. for(x=0;x<DPosMitjos;x++)
  356. cost += GetMitg(equip.Mitjos[x]).cost;
  357.  
  358. for(x=0;x<DPosDelanters;x++)
  359. cost += GetDelanter(equip.Delanters[x]).cost;
  360.  
  361. return (cost);
  362. }
  363.  
  364.  
  365.  
  366. // Calculates the team points adding the individual points of all team players.
  367. // Returns the points.
  368.  
  369. int
  370. PuntuacioEquip(TJugadorsEquip equip)
  371. {
  372. int x;
  373. int punts=0;
  374.  
  375. for(x=0;x<DPosPorters;x++)
  376. punts += GetPorter(equip.Porter[x]).punts;
  377.  
  378. for(x=0;x<DPosDefensors;x++)
  379. punts += GetDefensor(equip.Defensors[x]).punts;
  380.  
  381. for(x=0;x<DPosMitjos;x++)
  382. punts += GetMitg(equip.Mitjos[x]).punts;
  383.  
  384. for(x=0;x<DPosDelanters;x++)
  385. punts += GetDelanter(equip.Delanters[x]).punts;
  386.  
  387. return(punts);
  388. }
  389.  
  390.  
  391. // Prints an error message.ç
  392.  
  393. void error(char *str)
  394. {
  395. char s[255];
  396.  
  397. sprintf(s, "[%d] ManFut: %s (%s))\n", getpid(), str,strerror(errno));
  398. write(2, s, strlen(s));
  399. exit(1);
  400. }
  401.  
  402.  
  403. // Rounded log2
  404.  
  405. unsigned int Log2(unsigned long long int n)
  406. {
  407. return(ceil(log2((double)n)));
  408. }
  409.  
  410.  
  411. // Prints all market players information,
  412.  
  413. void PrintJugadors()
  414. {
  415. int j;
  416.  
  417. for(j=0;j<NJugadors;j++)
  418. {
  419. sprintf(cad,"Jugador: %s (%d), Posició: %d, Cost: %d, Puntuació: %d.\n", Jugadors[j].nom, Jugadors[j].id, Jugadors[j].tipus, Jugadors[j].cost, Jugadors[j].punts);
  420. write(1,cad,strlen(cad));
  421. }
  422. }
  423.  
  424.  
  425.  
  426. // Prints team players.
  427. void PrintEquipJugadors(TJugadorsEquip equip)
  428. {
  429. int x;
  430.  
  431. write(1," Porters: ",strlen(" Porters: "));
  432. for(x=0;x<DPosPorters;x++)
  433. {
  434. sprintf(cad,"%s (%d/%d), ",GetPorter(equip.Porter[x]).nom, GetPorter(equip.Porter[x]).cost, GetPorter(equip.Porter[x]).punts);
  435. write(1,cad,strlen(cad));
  436. }
  437. write(1,"\n",strlen("\n"));
  438.  
  439. write(1," Defenses: ",strlen(" Defenses: "));
  440. for(x=0;x<DPosDefensors;x++)
  441. {
  442. sprintf(cad,"%s (%d/%d), ",GetDefensor(equip.Defensors[x]).nom, GetDefensor(equip.Defensors[x]).cost, GetDefensor(equip.Defensors[x]).punts);
  443. write(1,cad,strlen(cad));
  444. }
  445. write(1,"\n",strlen("\n"));
  446.  
  447. write(1," Mitjos: ",strlen(" Mitjos: "));
  448. for(x=0;x<DPosMitjos;x++)
  449. {
  450. sprintf(cad,"%s (%d/%d), ",GetMitg(equip.Mitjos[x]).nom, GetMitg(equip.Mitjos[x]).cost, GetMitg(equip.Mitjos[x]).punts);
  451. write(1,cad,strlen(cad));
  452. }
  453. write(1,"\n",strlen("\n"));
  454.  
  455. write(1," Delanters: ",strlen(" Delanters: "));
  456. for(x=0;x<DPosDelanters;x++)
  457. {
  458. sprintf(cad,"%s (%d/%d), ",GetDelanter(equip.Delanters[x]).nom, GetDelanter(equip.Delanters[x]).cost, GetDelanter(equip.Delanters[x]).punts);
  459. write(1,cad,strlen(cad));
  460. }
  461. write(1,"\n",strlen("\n"));
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement