네이버 문제

언어/c언어2017. 11. 23. 17:17

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

 

# include <stdio.h>
# include <string.h>
# include <Windows.h>
//___________________________________________
// 구조체
typedef struct Displacement_cipher {
 int k; // secret key
 char plainText[50];
 char cipherText[50];
 char decryptText[50];
}Displacement_cipher, *ptr_dis_cipher;
//___________________________________________
// 함수 원형
ptr_dis_cipher createNode();
void plainText_Write(Displacement_cipher** param);
void secretKey_input(Displacement_cipher** param);
void encrypt_text(Displacement_cipher** param);
void decrypt_text(Displacement_cipher** param);
//___________________________________________
int main(void) {
 ptr_dis_cipher node = NULL;
 node = createNode();
 plainText_Write(&node);
 secretKey_input(&node);
 encrypt_text(&node);
 decrypt_text(&node);
 free(node); // 동적 할당 해제
 return 0;
} // end of main function
ptr_dis_cipher createNode() { // function _1
 ptr_dis_cipher node = (ptr_dis_cipher)malloc(sizeof(Displacement_cipher));
 if (node == NULL) { ExitProcess(1); }
 else {
  // node != NULL
  memset(node, 0, sizeof(Displacement_cipher));
  return node;
 }
} // end of createNode function
void plainText_Write(Displacement_cipher** param) { // function _2
 printf("plainText write :  ");
 fgets((** param).plainText, 50, stdin);
 (** param).plainText[strlen((** param).plainText) - 1] = '\0';
 system("cls");
 printf("PlainText is => [%s]\n", (** param).plainText);
} // end of plainText_Write function

void secretKey_input(Displacement_cipher** param) { // function _3
 printf("K value Input (Plz : 0 ~ 25):  ");
 scanf("%d", &(** param).k);
 (** param).k = (** param).k % 26;
 system("cls");
 printf("SecretKey is => [%d]\n", (** param).k);
} // end of secretKey_input function

void encrypt_text(Displacement_cipher** param) { // function _4
 unsigned int i; // index
 int j = 0;
 int t_enc_indx = 0;
 for (i = 0; i < strlen((** param).plainText); i++) {
  // 만일 대문자라면
  if ((int)(** param).plainText[i] >= 0x41 && (int)(** param).plainText[i] <= 0x5A) {
   t_enc_indx = (int)(** param).plainText[i] - 0x41;
   t_enc_indx = (t_enc_indx + (** param).k) % 26;
   t_enc_indx += 0x41;
   (** param).cipherText[j++] = (char)t_enc_indx;
  }
  // 만일 소문자라면
  else if ((int)(** param).plainText[i] >= 0x61 && (int)(** param).plainText[i] <= 0x7A) {
   t_enc_indx = (int)(** param).plainText[i] - 0x61;
   t_enc_indx = (t_enc_indx + (** param).k) % 26;
   t_enc_indx += 0x61;
   (** param).cipherText[j++] = (char)t_enc_indx;
  }
  // 그 어디에도 속해있지 않다면
  else {
   (** param).cipherText[j++] = (** param).plainText[i];
  }
 }
 (** param).cipherText[j] = '\0';
 printf("cipherText is [%s]\n", (** param).cipherText);
} // end of encrypt_text function

void decrypt_text(Displacement_cipher** param) { // function _5
 unsigned int i; // index
 int j = 0;
 int t_dec_indx = 0;
 for (i = 0; i < strlen((** param).cipherText); i++) {
  // 만일 대문자라면
  if ((int)(** param).cipherText[i] >= 0x41 && (int)(** param).cipherText[i] <= 0x5A) {
   t_dec_indx = (int)(** param).cipherText[i] - 0x41;
   t_dec_indx = (t_dec_indx - (** param).k);
   if (t_dec_indx < 0) { t_dec_indx += 26; }
   t_dec_indx += 0x41;
   (** param).decryptText[j++] = (char)t_dec_indx;
  }
  // 만일 소문자라면
  else if ((int)(** param).cipherText[i] >= 0x61 && (int)(** param).cipherText[i] <= 0x7A) {
   t_dec_indx = (int)(** param).cipherText[i] - 0x61;
   t_dec_indx = (t_dec_indx - (** param).k);
   if (t_dec_indx < 0) { t_dec_indx += 26; }
   t_dec_indx += 0x61;
   (** param).decryptText[j++] = (char)t_dec_indx;
  }
  // 그 어디에도 속해있지 않다면
  else {
   (** param).decryptText[j++] = (** param).cipherText[i];
  }
 }
 (** param).decryptText[j] = '\0';
 printf("decryptText is => [%s]\n", (** param).decryptText);
} // end of decrypt_text function

'언어 > c언어' 카테고리의 다른 글

sprintf  (0) 2017.11.30
c언어 문제  (0) 2017.11.27
네이버 문제  (0) 2017.11.22
조건문  (0) 2017.11.21
네이버 문제  (0) 2017.11.21