Test/C

2018_2_pad

kiostory 2019. 4. 27. 18:39






#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pad.h"

void rotateArr(int numberPad[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int arraySize, int direction, int isUpAndDown);

/**
 * 이동문자열을 추출하는 기능
 *
 * @param[out]  rotationStr  이동문자열
 * @param[in]   inputData  입력데이터(문자열)
 */
void extractRotationStr(char rotationStr[MAX_DATA_LENGTH], char inputData[MAX_DATA_LENGTH]) {

 memset(rotationStr, 0, sizeof(char) * MAX_DATA_LENGTH);

 ////////////////////////// ---------------->

 int idx = 0;
 for(int i=1; i<strlen(inputData); i=i+2) {
  if(inputData[i] == 'U' || inputData[i] == 'D' || inputData[i] == 'L' || inputData[i] == 'R') {
   if(inputData[i-1] != '0') {
    strncpy(&rotationStr[idx], &inputData[i-1], sizeof(char)*2);
    idx = idx + 2;
   }
  }
 }

 /////////////////////////// <--------------

}

/**
 * 숫자패드를 이동시키는 기능
 *
 * @param[in,out]   numberPad  숫자패드/이동된 숫자패드
 * @param[in]    arraySize  배열 크기
 * @param[in]    rotationStr  이동문자열
 */
void rotateNumberPad(int numberPad[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int arraySize, char rotationStr[MAX_DATA_LENGTH]) {

 ////////////////////////// ---------------->

 // 최종 이동할 개수 계산
 int moveRowCnt = 0;
 int moveColCnt = 0;
 for(int i=1; i<strlen(rotationStr); i=i+2) {
  switch(rotationStr[i]) {
   case 'U': moveRowCnt = moveRowCnt - (rotationStr[i-1] -'0'); break;
   case 'D': moveRowCnt = moveRowCnt + (rotationStr[i-1] -'0'); break;
   case 'L': moveColCnt = moveColCnt - (rotationStr[i-1] -'0'); break;
   case 'R': moveColCnt = moveColCnt + (rotationStr[i-1] -'0'); break;
  }
 }

 // 각 원소별 이동위치 계산
 int tmpNumberPad[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];
 memset(tmpNumberPad, 0, sizeof(int)*MAX_ARRAY_SIZE*MAX_ARRAY_SIZE);
 memcpy(tmpNumberPad, numberPad, sizeof(int)*MAX_ARRAY_SIZE*MAX_ARRAY_SIZE);

 int row = 0;
 int col = 0;
 for(int i=0; i<arraySize; i++) {
  for(int j=0; j<arraySize; j++) {
   // 최종 행위치
   row = i + moveRowCnt;
   if (row < 0)
    row = row + arraySize;
   else if (row >= arraySize)
    row = row % arraySize;

   // 최종 열위치
   col = j + moveColCnt;
   if (col < 0)
    col = col + arraySize;
   else if (col >= arraySize)
    col = col % arraySize;

   numberPad[row][col] = tmpNumberPad[i][j];
  }
 }

 /////////////////////////// <--------------

}

/**
 * 배열을 이동시키는 기능 (솔루션용 기능, 제공파일에 없음)
 *
 * @param[in,out]  numberPad 배열/이동된 배열
 * @param[in]   arraySize  배열 크기
 * @param[in]   direction  방향(+또는-)
 * @param[in]   isUpAndDown  위,아래,왼쪽,오른쪽(0또는1)
 */

void rotateArr(int numberPad[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int arraySize, int direction, int isUpAndDown) {

 int tmpNumberPad[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];
 memcpy(tmpNumberPad, numberPad, sizeof(int) * MAX_ARRAY_SIZE * MAX_ARRAY_SIZE);
 for(int i=0; i<arraySize; i++) {
  for(int j=0; j<arraySize; j++) {
   if(isUpAndDown == 1) numberPad[(arraySize*arraySize+direction+i)%arraySize][j] = tmpNumberPad[i][j];
   else numberPad[i][(arraySize*arraySize+direction+j)%arraySize] = tmpNumberPad[i][j];
  }
 }

}