Preprocessor Solution

  • + 7 comments

    Your welcome baba! It's a pleasure for me if i can help you ;).

    As you know, #define is a word to word translator, i.e. if we write something like this:

    #define meysam unsigned
    #define pg int
    
    void main() {
    	meysam pg name;
    }
    

    the compiler translate it to this first:

    #define meysam unsigned
    #define pg int
    
    void main() {
    	unsigned int name;
    }
    

    and then compile it. Now look at my macro:

    #define FUNCTION(name,operator) inline void name(int &current, int candidate) {!(current operator candidate) ? current = candidate : false;}
    

    On code editor of submit page, we have something like this:

    FUNCTION(minimum, <)
    FUNCTION(maximum, >)
    

    So as first example of this replay, compiler will translate this lines to this:

    inline void minimum(int &current, int candidate) {!(current < candidate) ? current = candidate : false;}
    inline void maximum(int &current, int candidate) {!(current > candidate) ? current = candidate : false;}
    

    Why? on #define we have FUNCTION(name, operator), so when we write FUNCTION(minimum, <) on editor, all name on

    #define FUNCTION(name,operator) inline void name(int &current, int candidate) {!(current operator candidate) ? current = candidate : false;}
    

    will translated to minimum, and this is same for <.

    Now we have

    inline void minimum(int &current, int candidate) {!(current < candidate) ? current = candidate : false;}
    inline void maximum(int &current, int candidate) {!(current > candidate) ? current = candidate : false;}
    

    on top of main function and we can call it later.

    Any question are welcome :).