makrusak

qsort_cards

Dec 10th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. const char pr[] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A', 'c', 's', 'h', 'd' };
  4.  
  5. inline int prior(char c) {
  6.   int ans = 0;
  7.   while (pr[ans]!=c) ans++;
  8.   return ans;
  9. }
  10.  
  11. int comp(const void *p1, const void *p2) {
  12.   const struct Card *a = (Card *)p1;    
  13.   const struct Card *b = (Card *)p2;
  14.   if (a->suit == b->suit) {
  15.     if (prior(a->rank)<prior(b->rank)) return -1;
  16.     return 1;
  17.   }
  18.   if (prior(a->suit)<prior(b->suit)) return -1;
  19.   return 1;
  20. }
  21.  
  22. void sort_card(int n, Card card[]) {
  23.   qsort(card, n, sizeof(Card), comp);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment