/* 작성자 : sleep4725.tistory.com */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define AND &&
#define L 200
#define POSITIVE 0
#define NEGATIVE 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
typedef struct str_To_Num
{
 char strData[200]; // " [12, 23, 34] "
 char tmpData[200]; // "e.g : '12 + 23 + 34'"
 int totalSum; // int (12 + 23 + 34)
}str_To_Num, *ptrSum;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ptrSum creatNode();
void _dataInput_(str_To_Num** param);
void _parsing_(str_To_Num** param);
void _UnitSumReturn_(str_To_Num** param, char* strNumber, int length);
void _tmp_(str_To_Num** param);
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int main(void) {
 ptrSum s = creatNode();
 _dataInput_(&s);
 _parsing_(&s);
 _tmp_(&s);
 free(s);
 return 0;
} // end of main function
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ptrSum creatNode()
{
 ptrSum node = (ptrSum)malloc(sizeof(str_To_Num));
 if (node == NULL) { exit(1); }
 else
 {
  memset(node, 0, sizeof(str_To_Num));
  strcat(node->tmpData, " = ");
  return node;
 }
} // end of creatNode function
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void _dataInput_(str_To_Num** param)
{
 fgets((** param).strData, L, stdin);
 int length = 0;
 length = strlen((** param).strData);
 (** param).strData[length - 1] = '\0';
} // end of _dataInput_ function
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void _parsing_(str_To_Num** param)
{
 char tmpStr[50] = { 0, };
 int k = 0;
 for (int i = strlen((**param).strData)-1; i >= 0; i--)
 {
  if ((**param).strData[i] != '[' AND
   (**param).strData[i] != ']' AND
   (**param).strData[i] != ',')
  {
   tmpStr[k] = (**param).strData[i];
   k = (k + 1) % 50; // 0, 1, 2, ..., 49
  }
  else
  {
   /*
    (**param).strData[i] == '[' OR
    (**param).strData[i] == ']' OR
    (**param).strData[i] == ','
   */
   if (tmpStr[0] != '\0')
   {    
    _UnitSumReturn_(param, tmpStr, i);
    memset(tmpStr, 0, sizeof(char)* 50);
    k = 0;
   }
  }
 }
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void _UnitSumReturn_(str_To_Num** param, char* strNumber, int length)

 int t_num = 0;
 char t_sNum[50] = { 0, };
 int i;
 int flag = POSITIVE;
 int sum_unit = 0;
 for (i = 0; i < strlen(strNumber); i++)
 {
  // number control
  if (((*(strNumber + i) - 48) >= 0) AND
   ((*(strNumber + i) - 48) <= 9))
  {
   t_num = *(strNumber + i) - 48; // from char => to int
   sum_unit += ((int)(pow(10, i)) * t_num);
   t_sNum[i] = *(strNumber + i);
  }
  else // sign control
  { // '-' : negative , '+' : positive
   switch (*(strNumber + i))
   {
    case '-': flag = NEGATIVE; break;
    case '+': flag = POSITIVE;
    default : flag = POSITIVE; break;
   }
  }
 }
 
 t_sNum[i] = '\0';
 strcat((** param).tmpData, t_sNum);
 if (flag == NEGATIVE)
 {
 
  strcat((** param).tmpData, "-");
  sum_unit *= -1;
  (** param).totalSum += sum_unit;
 }
 else
 {
  if (length != 0)
  {
   strcat((** param).tmpData, "+");
  }
  (** param).totalSum += sum_unit;
 }
 printf("%s\n", (** param).tmpData);
} // end of _UnitSumReturn_ function
void _tmp_(str_To_Num** param)

 for (int i = strlen((** param).tmpData) - 1; i >= 0; i--)
 {
  printf("%c", (** param).tmpData[i]);
 }
 printf("%d \n", (** param).totalSum);
}

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

c언어 암호화 코드 시저  (0) 2017.10.23
c언어 반복문 없이 가장 큰 숫자 찾기  (0) 2017.10.22
#네이버 문제 풀이  (0) 2017.10.22
어셈블리 구조체 접근  (0) 2017.10.21
# 네이버 _ 2017_10_21  (0) 2017.10.21