언어/c언어

네이버 지식인 풀이

파아랑새 2018. 11. 7. 07:45

https://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=313858105

# include <stdio.h>

# include <string.h> 

# include <stdlib.h>

# include <Windows.h>

# include <conio.h>

# define LEN 60

# define ERROR_ 1

typedef struct {

char my_passwd[LEN];

char input_passwd[LEN];

}PASSWRD;

// function prototype

// =========================================

PASSWRD* ret_new_node();

void init(PASSWRD** param);

void password_write(PASSWRD** param);

void compare(PASSWRD** param);

// =========================================

int main(void) {

PASSWRD* mPass = NULL;

mPass = ret_new_node();

if (mPass == NULL) {

printf("malloc fail ... !!!\n");

exit(ERROR_);

}

init(&mPass);

password_write(&mPass);

compare(&mPass);

free(mPass);

return 0;

} // end of main function

PASSWRD* ret_new_node() { // function (1)

PASSWRD* tmpNode = NULL;

tmpNode = (PASSWRD*)malloc(sizeof(PASSWRD));

return tmpNode;

} // end of ret_new_node function

// =========================================

void init(PASSWRD** param) { // function (2)

// 데이터 초기화 

strcpy((*param)->input_passwd, "\0");

strcpy((*param)->my_passwd, "\0");

// 패스워드 설정

strcpy((*param)->my_passwd, "hello1234");

} // end of init function

// =========================================

void password_write(PASSWRD** param) {

int i = 0; // index

printf("PassWord:  ");

while (1) {


if (_kbhit()) {

(*param)->input_passwd[i] = _getch();

if ((int)((*param)->input_passwd[i]) == 13) {

break;

}

i += 1;

printf("*");

//rewind(stdout);

}

}

printf("\n");

(*param)->input_passwd[i] = '\0';

printf("%s\n", (*param)->input_passwd);

} // end of password_write function 

// =========================================

void compare(PASSWRD** param) {

if (!strcmp((*param)->input_passwd, (*param)->my_passwd)) {

printf("same \n");

}

else {

printf("not same\n");

}

} // end of compare function