Programming/C

[C] 포켓몬 연습 문제 2

graygreat 2017. 4. 19. 09:55
728x90
반응형


 - 포켓몬 이름은 "치코리타, 이상해씨, 파이리, 리자몽, 꼬부기, 야도란" 등 중 하나를 임의로 설정

 - 포켓몬 이름과 타입이 일치하게 해줍시다.

   // 함수 원형 : Pokemon *create();


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

// Pokemon 구조체 선언
typedef struct pokemon{
    char *name;
    char *type;
    int old;
    int hp; 
} Pokemon;

// Pokemon 타입의 create 함수
Pokemon *create(){
    srand((unsigned)time(NULL));
         
    char c_name[][13] = {"치코리타", "이상해씨", "파이리", "리자몽", "꼬부기", "야도란"};
    char c_type[][4] = {"풀", "불", "물"};  
    int c_old = rand() % 100;
    int c_hp = rand() % 100;
    int r = rand() % 6;
        
    // Pokemon 구조체 동적할당
    Pokemon *p; 
    p = (Pokemon *)malloc(sizeof(Pokemon) * 1); 
         
    // Pokemon 구조체의 name, type 동적할당
    p->name = (char *)malloc(sizeof(char) * 30);
    p->type = (char *)malloc(sizeof(char) * 30);
        
    strcpy(p->name, c_name[r]);
    strcpy(p->type, c_type[r / 2]);
    p->old = c_old;
    p->hp = c_hp;

    return p;
}


void main(){
    int num = 1;

    while(num != 0){
        Pokemon *a = create();
        scanf("%d", &num);
        printf("===============\n");
        printf("이름 : %s\n", a->name);
        printf("타입 : %s\n", a->type);
        printf("나이 : %d\n", a->old);
        printf("체력 : %d\n", a->hp);
        printf("===============\n");

        free(a);
    }
}



반응형