fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. enum Suite {
  5. Spade,
  6. Heart,
  7. Club,
  8. Dia
  9. };
  10.  
  11. struct CARD {
  12. enum Suite suite;
  13. int rank;
  14. };
  15.  
  16. int randRange(int min, int max);
  17. enum Suite suiteRand(void);
  18. int rankRand(void);
  19. int suiteCompare(enum Suite s1, enum Suite s2);
  20. int rankCompare(int r1, int r2);
  21. int cardCompare(struct CARD c1, struct CARD c2);
  22. void cardsSort(struct CARD cards[]);
  23. void cardsPrint(struct CARD cards[]);
  24.  
  25. int main(void) {
  26. struct CARD cards[13];
  27. int i;
  28. for (i = 0; i < 13; i++) {
  29. cards[i].suite = suiteRand();
  30. cards[i].rank = rankRand();
  31. }
  32. cardsPrint(cards);
  33. cardsSort(cards);
  34. cardsPrint(cards);
  35.  
  36. return 0;
  37. }
  38.  
  39. int randRange(int min, int max) {
  40. int range = max - min + 1;
  41. int limit = RAND_MAX - ((RAND_MAX - range + 1) % range);
  42. int number;
  43. do {
  44. number = rand();
  45. } while (limit < number);
  46. return (number % range) + min;
  47. }
  48.  
  49. enum Suite suiteRand() {
  50. switch (randRange(1, 4)) {
  51. case 1:return Spade;
  52. case 2:return Club;
  53. case 3:return Heart;
  54. default:return Dia;
  55. }
  56. }
  57.  
  58. int rankRand() {
  59. return randRange(1, 13);
  60. }
  61.  
  62. int suiteCompare(enum Suite s1, enum Suite s2) {
  63. if (s1 < s2) {
  64. return -1;
  65. } else if (s1 > s2) {
  66. return 1;
  67. } else {
  68. return 0;
  69. }
  70. }
  71.  
  72. int rankCompare(int r1, int r2) {
  73. if (r1 < r2) {
  74. return -1;
  75. } else if (r1 > r2) {
  76. return 1;
  77. } else {
  78. return 0;
  79. }
  80. }
  81.  
  82. int cardCompare(struct CARD c1, struct CARD c2) {
  83. int c;
  84.  
  85. c = suiteCompare(c1.suite, c2.suite);
  86. if (c == 0) {
  87. c = rankCompare(c1.rank, c2.rank);
  88. }
  89. return c;
  90. }
  91.  
  92. void cardsSort(struct CARD cards[]) {
  93. int i;
  94. int j;
  95. int k;
  96. struct CARD t;
  97. i = 12;
  98. do {
  99. k = 0;
  100. for (j = 0; j < i; j++) {
  101. if (cardCompare(cards[j], cards[j + 1]) > 0) {
  102. t = cards[j];
  103. cards[j] = cards[j + 1];
  104. cards[j + 1] = t;
  105. k = j;
  106. }
  107. }
  108. i = k;
  109. } while (0 < i);
  110. }
  111.  
  112. void cardsPrint(struct CARD cards[]) {
  113. int i;
  114. char *suite;
  115. for (i = 0; i < 13; i++) {
  116. switch (cards[i].suite) {
  117. case Spade:
  118. suite = "スペード";
  119. break;
  120. case Club:
  121. suite = "クラブ";
  122. break;
  123. case Heart:
  124. suite = "ハート";
  125. break;
  126. case Dia:
  127. suite = "ダイヤ";
  128. break;
  129. }
  130. printf("%sの%d", suite, cards[i].rank);
  131. if (i < 12) {
  132. printf("→");
  133. } else {
  134. printf("\n");
  135. }
  136. }
  137. }
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
ダイヤの10→クラブの5→クラブの2→ハートの4→クラブの8→ハートの4→ハートの5→ダイヤの2→スペードの11→スペードの12→ダイヤの9→ダイヤの4→ハートの6
スペードの11→スペードの12→ハートの4→ハートの4→ハートの5→ハートの6→クラブの2→クラブの5→クラブの8→ダイヤの2→ダイヤの4→ダイヤの9→ダイヤの10