언어/c언어

c언어 문제 lavida

파아랑새 2017. 10. 2. 11:13

문제 : https://lavida.us/problem.php?id=1005

# include <stdio.h>

# include <stdlib.h>

# define LEN 100


typedef struct strongBox {

char strRandom[100];

int targetNumber[4];

char resultPassword[5];

} strongBox, *ptrStrongBox;


// function prototype

ptrStrongBox createNode(); // [1]

void init(strongBox** param); // [2]

void passwdNumber(strongBox** param); // [3]

void selectNumber(strongBox** param); // [4]

void resultNumber(strongBox** param); // [5]

void dataPrintf(strongBox** param); // [6]


int main(void) {

ptrStrongBox sBox = NULL;

sBox = createNode(); // [1]

init(&sBox); // [2]

passwdNumber(&sBox); // [3]

selectNumber(&sBox); // [4]

resultNumber(&sBox); // [5] 

dataPrintf(&sBox); // [6]

free(sBox);

return 0;

} // end of main function


ptrStrongBox createNode() { // [1]

ptrStrongBox newNode = (ptrStrongBox)malloc(sizeof(strongBox));

return newNode;

} // end of createNode function


void init(strongBox** param) { // [2]

if ((*param) == NULL) {

exit(1);

else { // (*param) != NULL

strcpy((** param).resultPassword, "\0");

strcpy((** param).strRandom, "\0");

for (int i = 0; i < 4; i++) {

(** param).targetNumber[i] = 0;

}

}

} // end of init function


void passwdNumber(strongBox** param) { // [3]

fgets((** param).strRandom, LEN, stdin);

} // end of passwdNumber function 


void selectNumber(strongBox** param) { // [4]

for (int i = 0; i < 4; i++) {

scanf("%d", &((**param).targetNumber[i]));

}

} // end of selectNumber function


void resultNumber(strongBox** param) { // [5]

int i;

for (i = 0; i < 4; i++) {

(**param).resultPassword[i] = (**param).strRandom[(**param).targetNumber[i]];

}

(**param).resultPassword[i] = '\0';


} // end of resultNumber function 


void dataPrintf(strongBox** param) { // [6]

printf("%s\n", (**param).resultPassword);

} // end of dataPrintf function