본문 바로가기

공부/c언어

구조체와 열거형 연습(

반응형


=========================================================================================================================================
card.h

#include <stdio.h>
#include <time.h>

enum enumsuit {Hearts, Diamonds, Clubs, Spades};
enum enumface {Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King};

typedef enum eumsuit enumsuit;
typedef enum enumface enumface;

struct card {
 enumsuit suit;
 enumsuit face;
};
typedef struct card card;

char *strface[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
char *strsuit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};

void filldeck(card *deck);
void shuffle(card *deck);
void deal(card *deck, int cnt);

=======================================================================================================================================
card.c

#include "card.h"

int main()
{
 card deck[52];
 int cnt = 2;

 filldeck(deck);
 shuffle(deck);
 printf("카드 게임에 몇 사람이 참가합니까? >> ");
 scanf("%d", &cnt);
 printf("\n");
 deal(deck, cnt);
 return 0;
}

void filldeck(card *deck)
{
 int i;

 for(i=0; i<= 51; i++){
  deck[i].face = i%13;
  deck[i].suit = i/13;
 }
}

void shuffle(card *deck)
{
 int i, j;
 card temp;

 srand(time(NULL));
 for(i = 0; i<=51; i++){
  j = rand() % 52;
  temp = deck[i];
  deck[i] = deck[j];
  deck[j] = temp;
 }
}

void deal(card *deck, int cnt)
{
 int i;
 int total = 7*cnt;

 for(i=0; i<cnt; i++)
  printf("%13s%-5d", "사람", i+1);
 printf("\n");
 
 for(i=0;i<total;i++)
  printf("%5s of %-8s%c", strface[deck[i].face], strsuit[deck[i].suit], (i+1) % cnt ? ' ' :'\n');
}

반응형