뀨읭
접속 : 5024   Lv. 59

Category

Profile

Counter

  • 오늘 : 25 명
  • 전체 : 2042070 명
  • Mypi Ver. 0.3.1 β
[MFC] 문자열 문제 해결함 ㅋㅋ (3) 2023/11/15 PM 02:00
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include<afx.h>
#include<afxwin.h>
#include<math.h>
 
 
CString GetStrDataValue(double dataValue)
{
    CString strResult = "";
 
    if (dataValue == 0)
    {
        strResult.Format("0");
    }
    else if (dataValue - floor(dataValue) <= DBL_EPSILON)
    {    // 정수
        strResult.Format("%.0lf", dataValue);
    }
    else
    {    // 정수가 아닌 실수
 
        if (dataValue < pow(10.f, -6))
        {    // 10e-6보다 작은 실수일때
            for (int i = -10; i < 0; i++)
            {
                if (abs(dataValue - floor(dataValue)) < pow(10.f, i))
                {
                    CString strFormat = "";
                    strFormat.Format("%%.%dlf"6 - i);
                    strResult.Format(strFormat, dataValue);
                    break;
                }
            }
        }
        else
        {
            strResult.Format("%lf", dataValue);
        }
        
        
        int strLen = strResult.GetLength();
        int nDecimalPoint = strResult.Find("."0);
        
        for (int strPos = strLen-1; strPos > nDecimalPoint; strPos--)
        {
            if (strResult.GetAt(strPos) != '0')
            {
                strResult = strResult.Left(strPos+1);
                break;
            }
        }
    }
 
    return strResult;
}
 
int main()
{
    CString strFormat = "";
    CString strResult = "";
 
    strResult = GetStrDataValue(2.5);
    printf("CString: %s\t\t\t\t\t\tdouble: %lf\n", strResult, 2.5);
    strResult = GetStrDataValue(59.99999);
    printf("CString: %s\t\t\t\t\tdouble: %lf\n", strResult, 59.99999);
    for (int i = 0; i < 10; i++)
    {
        strResult = GetStrDataValue(pow(10.f, -i));
        switch (i)
        {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
            printf("CString: %s\t\t\t\t\t\tdouble: %lf\n", strResult, pow(10.f, -i));
            break;
        case 5:
            printf("CString: %s\t\t\t\t\tdouble: %lf\n", strResult, pow(10.f, -i));
            break;
        default:
            printf("CString: %s\t\t\t\t\tdouble: %lf\n", strResult, pow(10.f, -i));
            break;
        }
        
    }
    strResult = GetStrDataValue(pow((double)2-32));
    printf("CString: %s\t\t\t\tdouble: %lf\n", strResult, pow((double)2-32));
    strResult = GetStrDataValue(6400);
    printf("CString: %s\t\t\t\t\t\tdouble: %lf\n", strResult, (double)6400);
    strResult = GetStrDataValue(4294967295);
    printf("CString: %s\t\t\t\t\tdouble: %lf\n", strResult, (double)4294967295);
    strResult = GetStrDataValue(-1571);
    printf("CString: %s\t\t\t\t\t\tdouble: %lf\n", strResult, (double)-1571);
    strResult = GetStrDataValue(FLT_MAX);
    printf("CString: %s\tdouble: %lf\n", strResult, FLT_MAX);
    strResult = GetStrDataValue(-3.402823466e+38F);
    printf("CString: %s\tdouble: %lf\n", strResult, -3.402823466e+38F);
}
cs




img/23/11/15/18bd1585666ffb0a.png



흡-족 ㅋㅋ. 더 쉬운 방법이 있을지도... =ㅅ=



이틀동안 머리깨지는 줄 알앗내 ㅡㅡ


신고

 

잇힝읏흥    친구신청

CString 쓰실거면
strString.TrimRight('0');
strString.TrimRight('.');
이러면 자투리 다 짤리지 않던가요?
과제용이면 뭐 직접 만드시는게 답이시겠지만...

뀨읭    친구신청

? 왜 됨

열혈청년한군    친구신청

지나가다가.... 코딩 공부에 도움이 되실까해서..
GetStrDataValue 함수를 가독성을 리팩토링 하시는게 좋지 않나 싶네요. ^_^
요즘 트랜드(?)는 if else 보다는 if return 방식으로..
if (dataValue == 0)
{
strResult.Format("0");
return strResult;
}

if (dataValue - floor(dataValue) <= DBL_EPSILON)
{ // 정수
strResult.Format("%.0lf", dataValue);
return strResult;
}
...
...
이렇게 return 하면서 else를 줄이는게 어떨까 싶네요. ^_^
이미 아시는 정보면... 참견해서 죄송합니다. ㅠ_ㅠ)/
X