c언어
C언어 - 두 단어의 크기를 비교하기(숫자일경우 숫자로 비교) 2
pi92
2021. 3. 5. 08:52
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h> // isdigit() 함수 사용하기 위해
#include <string.h>
#include <stdlib.h>
#define NUM 1 // 상태가 숫자 문자열인 경우
#define CHARCT 2 // 상태가 숫자 문자열이 아닌 경우
int numcmp(const char*, const char*); // 함수 numcmp 선언
int (*fcmp(char*, char*))(const char* , const char* );
int main()
{
int (*ptr)(const char*, const char*);
char s[80], t[80];
int i = 0, j = 0;
// 변수 ptr 선언
printf("입력 값(s) :");
scanf("%s", s);
printf("입력 값(t) :");
scanf("%s", t);
ptr = fcmp(s, t); // 함수 fcmp 콜
printf("%d\n", (*ptr)(s,t));
return 0;
}
int numcmp(const char* ps, const char* pt) // 함수 numcmp 정의
{
float a, b;
a = atof(ps);
b = atof(pt);
if (a > b)
return 1;
else if (a < b)
return -1;
else
return 0;
}
int (*fcmp(char*s, char*t))(const char* , const char*) // 함수 fcmp 정의
{
int cond;
char* ps = s;
char* pt = t;
int pointcheck = 0;
cond = NUM;
if (*ps == '-')
ps++;
if (*ps == '.')
cond = CHARCT;
while ((*ps != '\0') && (cond == NUM))
{
if (isdigit(*ps) != 0 || (*ps == '.')) {
ps++;
}
else {
cond = CHARCT;
}
}
if (*pt == '-')
pt++;
while ((*pt != '\0') && (cond == NUM))
{
if (isdigit(*pt) != 0 || (*pt == '.')) {
pt++;
}
else {
cond = CHARCT;
}
}
if (cond == NUM)
return numcmp;
else
return strcmp;
}