Native/C

함수 포인터 (비트방식)

aucd29 2013. 10. 2. 18:51
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#define MAX 10

void fcmp(int (**pf)(const char*, const char*), char*, char*);
int numcmp(const char*, const char*);

void main(void)
{
    int (*pfunc)(const char*, const char*);
    char f[MAX], s[MAX];

///    printf("first : %d\n",pf);
    
    printf("Input : ");
    scanf("%s %s",f, s);
    fcmp(&pfunc,f,s);

///    printf("second : %d\n",pf);
///    printf("func %d, %d\n",strcmp,numcmp);

    // -----------------------------------
    printf("Result : %d \n", pfunc(f,s));    
    // -----------------------------------
}

void fcmp(int (**pf)(const char*, const char*), char* f, char* s)
//void fcmp(int *pf, char* f, char* s)
{
    int iNumChk = 0;
    char *tmpf = f, *tmps = s;
    int iRes =0;

    while(*tmpf)
    {
        if(!isdigit(*tmpf) && *tmpf != '.' && *tmpf != '-')
            iNumChk = 1;
        tmpf++;
    }

    while(*tmps)
    {
        if(!isdigit(*tmps) && *tmps != '.' && *tmps != '-')
            iNumChk = 1;
        tmps++;
    }
    
    if(iNumChk)
    {
        printf("strcmp ");        
        *pf = strcmp;
    }
    else
    {
        printf("numcmp ");
        *pf = numcmp;
    }
}

int numcmp(const char* f, const char* s)
{
    double ff = atof(f);
    double fs = atof(s);

    if(ff > fs)
        return 1;
    else if(ff < fs)
        return -1;
    else
        return 0;
}