#include <stdio.h>
#include <stdlib.h>
enum Suite {
Spade,
Heart,
Club,
Dia
};
struct CARD {
enum Suite suite;
int rank;
};
int randRange(int min, int max);
enum Suite suiteRand(void);
int rankRand(void);
int suiteCompare(enum Suite s1, enum Suite s2);
int rankCompare(int r1, int r2);
int cardCompare(struct CARD c1, struct CARD c2);
void cardsSort(struct CARD cards[]);
void cardsPrint(struct CARD cards[]);
int main(void) {
struct CARD cards[13];
int i;
for (i = 0; i < 13; i++) {
cards[i].suite = suiteRand();
cards[i].rank = rankRand();
}
cardsPrint(cards);
cardsSort(cards);
cardsPrint(cards);
return 0;
}
int randRange(int min, int max) {
int range = max - min + 1;
int limit = RAND_MAX - ((RAND_MAX - range + 1) % range);
int number;
do {
} while (limit < number);
return (number % range) + min;
}
enum Suite suiteRand() {
switch (randRange(1, 4)) {
case 1:return Spade;
case 2:return Club;
case 3:return Heart;
default:return Dia;
}
}
int rankRand() {
return randRange(1, 13);
}
int suiteCompare(enum Suite s1, enum Suite s2) {
if (s1 < s2) {
return -1;
} else if (s1 > s2) {
return 1;
} else {
return 0;
}
}
int rankCompare(int r1, int r2) {
if (r1 < r2) {
return -1;
} else if (r1 > r2) {
return 1;
} else {
return 0;
}
}
int cardCompare(struct CARD c1, struct CARD c2) {
int c;
c = suiteCompare(c1.suite, c2.suite);
if (c == 0) {
c = rankCompare(c1.rank, c2.rank);
}
return c;
}
void cardsSort(struct CARD cards[]) {
int i;
int j;
int k;
struct CARD t;
i = 12;
do {
k = 0;
for (j = 0; j < i; j++) {
if (cardCompare(cards[j], cards[j + 1]) > 0) {
t = cards[j];
cards[j] = cards[j + 1];
cards[j + 1] = t;
k = j;
}
}
i = k;
} while (0 < i);
}
void cardsPrint(struct CARD cards[]) {
int i;
char *suite;
for (i = 0; i < 13; i++) {
switch (cards[i].suite) {
case Spade:
suite = "スペード";
break;
case Club:
suite = "クラブ";
break;
case Heart:
suite = "ハート";
break;
case Dia:
suite = "ダイヤ";
break;
}
printf("%sの%d", suite
,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