Posted By

garciacarmonaam on 09/10/15


Tagged

system cpp voting c++ politics elections sainte-lague


Versions (?)

An algorithm of Sainte-Laguë's method


 / Published in: C++
 

URL: https://github.com/garciacarmonaam/saintelague

Really, this is part of a set of classes that is stored in a Git repository and under MIT license. The function I'll expose is Circunscription::distributeSeats().

  1. void distributeSeats()
  2. {
  3. for (int i = 0; i < this->seats; i++)
  4. {
  5. int mostvoted_code = 0;
  6. int votes = 0;
  7. for (int j = 0; j < this->candidatures_number; j++)
  8. {
  9. if (distribution[i][j] > votes)
  10. {
  11. mostvoted_code = j;
  12. votes = distribution[i][j];
  13. }
  14. }
  15.  
  16. Candidature *mostvoted = NULL;
  17. bool hasItBeenFound = false;
  18. this->candidatures->point1stCandidature();
  19. while (!this->candidatures->isCandidaturesListEndPointed() && !hasItBeenFound)
  20. {
  21. this->candidatures->checkPointedCandidature(mostvoted);
  22. if (mostvoted->getCode() == mostvoted_code)
  23. hasItBeenFound = true;
  24. else
  25. this->candidatures->moveCandidaturesPointer();
  26. }
  27.  
  28. int current_seats = mostvoted->getObtainedSeats();
  29. mostvoted->setObtainedSeats(current_seats + 1);
  30. this->candidatures->point1stCandidature();
  31.  
  32. if (i + 1 < this->seats)
  33. {
  34. distribution[i + 1][mostvoted_code] = distribution[0][mostvoted_code] / (2 * mostvoted->getObtainedSeats() + 1);
  35. int k = 0;
  36. while (k < this->candidatures_number)
  37. {
  38. if (k != mostvoted_code)
  39. distribution[i + 1][k] = distribution[i][k];
  40. k++;
  41. }
  42. }
  43. }
  44. }

Report this snippet  

You need to login to post a comment.