2015년 8월 7일 금요일

[ C# ] 지정한 문자열에 포함되는 값 가져 오기 (Val)

public sealed class Validation {
  #region Val 메소드 (+2)
    /// ----------------------------------------------- ------------------------------
    /// <summary>
    /// 지정된 문자열에 포함 된 숫자를 변환하여 반환합니다. </ summary>
    /// <param name = "stTarget">
    /// 유효한 문자열. </ param>
    /// <returns>
    /// 지정된 문자열에 포함 된 숫자. </ returns>
    /// ----------------------------------------------- ------------------------------
    public static double Val (string stTarget) {
        // Null 값의 경우는 0을 반환
        if (stTarget == null) {
            return 0D;
        }
        int iCurrent = 0;
        int iLength = stTarget.Length;
        // 평가되지 않는 문자 건너 뛰기
        for (iCurrent = 0; iCurrent <iLength; iCurrent ++) {
            char chOne = stTarget [iCurrent];
            if ((chOne! = '') && (chOne! = '') && (chOne! = '\ t') && (chOne! = '\ n') && (chOne! = '\ r')) {
                break;
            }
        }
        // 종료까지 유효한 문자가 발견되지 않았던 경우는 0을 반환
        if (iCurrent> = iLength) {
            return 0D;
        }
        bool bMinus = false;
        // 위에있는 부호를 판정
        switch (stTarget [iCurrent]) {
            case '-':
                bMinus = true;
                iCurrent ++;
                break;
            case + ':
                iCurrent ++;
                break;
        }
        int ValidLength = 0;
        int Priod = 0;
        double dReturn = 0D;
        bool bDecimal = false;
        bool bShisuMark = false;
        // 1 문자 씩 유효한 문자 여부 판정
        while (iCurrent <iLength) {
            char chCurrent = stTarget [iCurrent];
            if ((chCurrent == '') || (chCurrent == '') || (chCurrent == '\ t') || (chCurrent == '\ n') || (chCurrent == '\ r' )) {
                iCurrent ++;
            } else if (chCurrent == '0') {
                iCurrent ++;
                if ((iValidLength! = 0) || bDecimal) {
                    iValidLength ++;
                    dReturn = (dReturn * 10) + double.Parse (chCurrent.ToString ());
                }
            } else if ((chCurrent> = '1') && (chCurrent <= '9')) {
                iCurrent ++;
                iValidLength ++;
                dReturn = (dReturn * 10) + double.Parse (chCurrent.ToString ());
            } else if (chCurrent == '') {
                iCurrent ++;
                if (bDecimal) {
                    break;
                }
                bDecimal = true;
                iPriod = iValidLength;
            } else if ((chCurrent == 'e') || (chCurrent == 'E') || (chCurrent == 'd') || (chCurrent == 'D')) {
                bShisuMark = true;
                iCurrent ++;
                break;
            } else {
                break;
            }
        }
        int iDecimal = 0;
        // 소수점이 판정 된 경우
        if (bDecimal) {
            iDecimal = iValidLength - iPriod;
        }
        // 지수가 판정 된 경우
        if (bShisuMark) {
            bool bShisuValid = false;
            bool bShisuMinus = false;
            double dCoef = 0D;
            // 지수를 확인하는
            while (iCurrent <iLength) {
                char chCurrent = stTarget [iCurrent];
                if ((chCurrent == '') || (chCurrent == '') || (chCurrent == '\ t') || (chCurrent == '\ n') || (chCurrent == '\ r' )) {
                    iCurrent ++;
                } else if ((chCurrent> = '0') && (chCurrent <= '9')) {
                    dCoef = (dCoef * 10) + double.Parse (chCurrent.ToString ());
                    iCurrent ++;
                } else if (chCurrent == + ') {
                    if (bShisuValid) {
                        break;
                    }
                    ShisuValid = true;
                    iCurrent ++;
                } else if ((chCurrent! = '-') || bShisuValid) {
                    break;
                } else {
                    bShisuValid = true;
                    bShisuMinus = true;
                    iCurrent ++;
                }
            }
            // 지수의 부호에 따라 거듭 제곱하는
            if (bShisuMinus) {
                dCoef + = iDecimal;
                dReturn * = System.Math.Pow (10 - dCoef);
            } else {
                dCoef - = iDecimal;
                dReturn * = System.Math.Pow (10, dCoef);
            }
        } else if (bDecimal && (iDecimal! = 0)) {
            dReturn / = System.Math.Pow (10, iDecimal);
        }
        // 무한대의 경우는 0을 반환
        if (double.IsInfinity (dReturn)) {
            return 0D;
        }
        // 마이너스 판정의 경우는 마이너스로 반환
        if (bMinus) {
            return -dReturn;
        }
        return dReturn;
    }
    /// ----------------------------------------------- ------------------------------
    /// <summary>
    /// 지정된 문자에 포함 된 숫자를 변환하여 반환합니다. </ summary>
    /// <param name = "chTarget">
    /// 유효한 문자. </ param>
    /// <returns>
    /// 지정된 문자에 포함 된 수치. </ returns>
    /// ----------------------------------------------- ------------------------------
    public static int Val (char chTarget) {
        return (int) Val (chTarget.ToString ());
    }
    /// ----------------------------------------------- ------------------------------
    /// <summary>
    /// 지정된 개체에 포함 된 숫자를 변환하여 반환합니다. </ summary>
    /// <param name = "oTarget">
    /// 유효한 오브젝트. </ param>
    /// <returns>
    /// 지정된 개체에 포함 된 수치. </ returns>
    /// ----------------------------------------------- ------------------------------
    public static double Val (object oTarget) {
        if (oTarget! = null) {
            return Val (oTarget.ToString ());
        }
        return 0D;
    }
  #endregion

}

댓글 없음:

댓글 쓰기