2016-09-20 5 views
-5

함수가 아닌 매크로를 사용하여 다른 숫자에 포함 된 숫자를 추출하는 방법.숫자 추출 번호

예 의사 코드 :

#define extract_number(int number, // original number 
        int pos, // start position from behind (1 == first digit) 
        int len) // count of digits 
{ 
    ... 
} 

main() { 
    int number = 0; 
    int result = 0; 

    number = 123456789; 
    result = extract_number(number, 2, 3); // result: 678 

    number = 987123456; 
    result = extract_number(number, 4, 4); // result: 7123 
} 

편집 2016년 9월 21일 : 내가 기압 할 것은 다음과 같다 :

int number = 123456789; 
int result = number/10 % 1000; 

@Frzn Flms에 의해 게시 기능을 사용하는 버전이 방법은 내가 것입니다 함수에서 그것을했다. 컴파일 할 때 매크로를 사용하는 방법이 있습니까?

+4

이 질문은 분명히 내 코드 요청이 아닌 질문을 작성하기 때문에 주제를 벗어난 것으로 닫습니다. 먼저 [Ask] 페이지를 읽으십시오. :) –

+0

지금까지 귀하의 연구/디버깅 노력을 보여주십시오. 먼저 [Ask] 페이지를 읽으십시오. –

+0

그 번호의 주요 요인을 찾으시겠습니까? – Olaf

답변

0

: 2 (10)로 대체되어야하며, (3)는 승압을하여 다음과 같이 기록 된 1000

의해 대체되어야한다.

#include <stdio.h> 
#include <boost/preprocessor/control/while.hpp> 
#include <boost/preprocessor/arithmetic/add.hpp> 
#include <boost/preprocessor/tuple/elem.hpp> 
#include <boost/preprocessor/cat.hpp> 

#define PRED(d, state) BOOST_PP_TUPLE_ELEM(2, 0, state) 

#define OP(d, state) (BOOST_PP_DEC(BOOST_PP_TUPLE_ELEM(2, 0, state)), BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(2, 1, state), 0)) 

#define pow0(n) BOOST_PP_TUPLE_ELEM(2, 1, BOOST_PP_WHILE(PRED, OP, (BOOST_PP_ADD(n,1), 1))) 

int main(void) { 
    int n = pow0(2);//1000: "1" + "0"✕(n+1) 
    printf("%d\n", n); 
    return 0; 
}