티스토리 뷰

Test/C

isdigit(), sprintf(), atoi()

kiostory 2019. 4. 16. 22:40

isdigit()



sprintf()



atoi()



----------------------------------------------------------------------------------------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "check.h"

/**
 * 체크수를 생성하는 기능
 *
 * @param[in] inputData 입력데이터(숫자)
 * @return     체크수
 */
int getCheckNum(int inputData) {

 int checkNum = 0;

 //////////////////////////  ---------------->
 int count = 0;
 int digit = 0;
 int sum = 0;

 // 1. 각 자릿수의 수를 곱셈 연산
 while(inputData != 0) {
  digit = inputData % 10;  // 자릿수
  count++;     // 곱셈에 사용되는 수
  sum = sum + digit * count;
  inputData = inputData/10;
 }

 // 2. 체크수 생성
 if(sum % count != 0)
  checkNum = sum % count;
 else
  checkNum = sum / count;

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

 return checkNum;

}

/**
 * 새로운 수를 생성하는 기능
 *
 * @param[in] inputData 입력데이터(숫자)
 * @param[in] checkNum 체크수
 * @return     새로운 수
 */
int generateNewNum(int inputData, int checkNum) {

 int newNum = 0;

 //////////////////////////  ---------------->
 char strNew[10] = {0, };

 if (checkNum < 10) {
  sprintf(strNew, "%d%d", inputData, checkNum);
 }
 else {
  int n1, n2;

  n1 = checkNum / 10;
  n2 = checkNum % 10;

  if (n1 > n2)
   sprintf(strNew, "%d%d%d", n1, inputData, n2);
  else
   sprintf(strNew, "%d%d%d", n2, inputData, n1);
 }

 newNum = atoi(strNew);

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

 return newNum;

}

'Test > C' 카테고리의 다른 글

center number (크기 기준)  (0) 2019.04.28
2018_2_pad  (0) 2019.04.27
MingGW 컴파일러 데이터타입 싸이즈  (0) 2019.04.07
파일 입출력  (0) 2019.01.07
구조체와 공용체  (0) 2019.01.05
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함