Advertisement
Guest User

Untitled

a guest
Jun 14th, 2020
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.36 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. // Max number of candidates
  6. #define MAX 9
  7.  
  8. // preferences[i][j] is number of voters who prefer i over j
  9. int preferences[MAX][MAX];
  10.  
  11. // locked[i][j] means i is locked in over j
  12. bool locked[MAX][MAX];
  13. bool circle;
  14. // Each pair has a winner, loser
  15. typedef struct
  16. {
  17.     int winner;
  18.     int loser;
  19. } pair;
  20.  
  21. // Array of candidates
  22. string candidates[MAX];
  23. pair pairs[MAX * (MAX - 1) / 2];
  24.  
  25. int pair_count;
  26. int candidate_count;
  27.  
  28. // Function prototypes
  29.  
  30. bool vote(int rank, string name, int ranks[]);
  31. void record_preferences(int ranks[]);
  32. void add_pairs(void);
  33. void sort_pairs(void);
  34. void lock_pairs(void);
  35. void print_winner(void);
  36. // check the graph making cycle
  37. bool iscircle();
  38. bool gointo(bool visited[], int to);
  39.  
  40. int main(int argc, string argv[])
  41. {
  42.     // Check for invalid usage
  43.     if (argc < 2)
  44.     {
  45.         printf("Usage: tideman [candidate ...]\n");
  46.         return 1;
  47.     }
  48.  
  49.     // Populate array of candidates
  50.     candidate_count = argc - 1;
  51.     if (candidate_count > MAX)
  52.     {
  53.         printf("Maximum number of candidates is %i\n", MAX);
  54.         return 2;
  55.     }
  56.     for (int i = 0; i < candidate_count; i++)
  57.     {
  58.         candidates[i] = argv[i + 1];
  59.     }
  60.  
  61.     // Clear graph of locked in pairs
  62.     for (int i = 0; i < candidate_count; i++)
  63.     {
  64.         for (int j = 0; j < candidate_count; j++)
  65.         {
  66.             locked[i][j] = false;
  67.         }
  68.     }
  69.  
  70.     pair_count = 0;
  71.     int voter_count = get_int("Number of voters: ");
  72.  
  73.     // Query for votes
  74.     for (int i = 0; i < voter_count; i++)
  75.     {
  76.         // ranks[i] is voter's ith preference
  77.         int ranks[candidate_count];
  78.  
  79.         // Query for each rank
  80.         for (int j = 0; j < candidate_count; j++)
  81.         {
  82.             string name = get_string("Rank %i: ", j + 1);
  83.  
  84.             if (!vote(j, name, ranks))
  85.             {
  86.                 printf("Invalid vote.\n");
  87.                 return 3;
  88.             }
  89.         }
  90.  
  91.         record_preferences(ranks);
  92.  
  93.         printf("\n");
  94.     }
  95.  
  96.     add_pairs();
  97.     sort_pairs();
  98.     lock_pairs();
  99.     print_winner();
  100.     return 0;
  101. }
  102.  
  103. // Update ranks given a new vote
  104. bool vote(int rank, string name, int ranks[])
  105. {
  106.     for (int k = 0; k < candidate_count; k++)
  107.     {
  108.         if (!strcmp(name, candidates[k]))
  109.         {
  110.             ranks[rank] = k;
  111.             return true;
  112.         }
  113.     }
  114.     return false;
  115. }
  116.  
  117. // Update preferences given one voter's ranks
  118. void record_preferences(int ranks[])
  119. {
  120.     for (int m = 0; m < candidate_count; m++)
  121.     {
  122.         for (int n = 0; n < candidate_count; n++)
  123.         {
  124.             if (m < n)
  125.             {
  126.                 ++preferences[ranks[m]][ranks[n]];
  127.             }
  128.         }
  129.     }
  130.  
  131.     return;
  132. }
  133.  
  134. // Record pairs of candidates where one is preferred over the other
  135. void add_pairs(void)
  136. {
  137.     for (int i = 0; i < candidate_count; i++)
  138.     {
  139.         for (int j = 0; j < candidate_count; j++)
  140.         {
  141.             if (preferences[i][j] == 0)
  142.             {
  143.                 continue;
  144.             }
  145.             if (preferences[i][j] > preferences[j][i])
  146.             {
  147.                 pairs[pair_count].winner = i;
  148.                 pairs[pair_count].loser = j;
  149.                 pair_count++;
  150.             }
  151.         }
  152.     }
  153.     return;
  154. }
  155.  
  156. // Sort pairs in decreasing order by strength of victory
  157. void sort_pairs(void)
  158. {
  159.     for (int i = 0; i < pair_count; i++)
  160.     {
  161.         for (int j = 0; j < pair_count; j++)
  162.         {
  163.             if (pairs[i].winner < pairs[j].winner)
  164.             {
  165.                 pair tmp = pairs[i];
  166.                 pairs[i] = pairs[j];
  167.                 pairs[j] = tmp;
  168.             }
  169.         }
  170.     }
  171.     return;
  172. }
  173.  
  174. // Lock pairs into the candidate graph in order, without creating cycles
  175. void lock_pairs(void)
  176. {
  177.     for (int k = 0; k < pair_count; k++)
  178.     {
  179.         for (int i = 0; i < candidate_count; i++)
  180.         {
  181.         //lock pairs strarting with pair have the biggest winner
  182.             if (pairs[k].winner == i)
  183.             {
  184.                 locked[pairs[k].winner][pairs[k].loser] = true;
  185.             }
  186.         //check if creates a cycle then unlock the pair
  187.             if (check_cycle(pairs[k].winner,pairs[k].loser))
  188.             {
  189.                 locked[pairs[k].winner][pairs[k].loser] = false;
  190.             }
  191.         }
  192.     }
  193.     return;
  194. }
  195.  
  196. // Print the winner of the election
  197. void print_winner(void)
  198. {
  199.     // TODO
  200.     for (int i = 0, k = 0; i < candidate_count; i++)
  201.     {
  202.         if (k == candidate_count)
  203.         {
  204.             printf("%s\n", candidates[i]);
  205.         }
  206.  
  207.         for (int j = 0; j < candidate_count; j++)
  208.         {
  209.             if (!locked[j][i])
  210.             {
  211.                 ++k;
  212.             }
  213.         }
  214.     }
  215.     return;
  216. }
  217. bool iscircle()
  218. {
  219.     bool visited[candidate_count];
  220.     circle = false;
  221.     return gointo(visited, 0);
  222. }
  223. bool gointo(bool visited[], int to)
  224. {
  225. // start trip form one node in the graph check if it have been visited ( Cycle )
  226.     visited[to] = true;
  227.     for (int n = 0; n < candidate_count; n++)
  228.     {
  229.         if (locked[to][n])
  230.         {
  231.             if (visited[n])
  232.             {
  233.                 circle = true;
  234.             }
  235.             else
  236.             {
  237.                 gointo(visited, n);
  238.             }
  239.             break;
  240.         }
  241.     }
  242.  
  243.     return circle;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement