博客 / 詳情

返回

手搓一個數字類的變量的類型檢查器

正在手搓 NumType 類型檢查器, 它可以通過輸入文本信息來判斷這個文本它可以轉換成什麼類型, 挺複雜的, 在此過程瞭解了一些我沒有見過的變量類型, 然後這兩天又是為着邊境檢查, 選取範圍感到苦惱, 不過現在已經把基礎代碼給寫好了, 勉強能用, 暫時沒有出現什麼大Bug (但願吧).

/// <summary>
/// 判斷數字類型
/// </summary>
/// <param name="input">輸入文本</param>
/// <returns>返回類型: null, string, int, long, float, double, decimal, biginteger</returns>
public static string NumType(string input)
{
    //忽略 Null
    if (input == null)
    {
        return "null";
    }
    else if(input.Length == 0)
    {
        return "string";
    }

    string type = "string";
    char[] nums = {'1','2','3','4','5','6','7','8','9','0' };
    bool kexuejishufa = false; //是不是科學計數發
    for(int i = 0; i < input.Length; i++ ) //遍歷文本
    {
        char t = input[i];
        if (i == 0 && (t == '-' || t == '+')) //是不是正負數
        {
            continue;
        }
        bool yes = false;
        foreach(char tt in nums) //誒個看看單個字是不是數字
        {
            if (tt == t)
            {
                yes = true;
                break;
            }
        }
        if (t == 'e' || t == 'E') //科學計數發
        {
            if (kexuejishufa == true) //不可能有兩個E
            {
                return "string";
            }
            else
            {
                yes = true;
                type = "float";
                kexuejishufa = true;
            }
        }
        else if(t == '-' || t == '+') //正負數的前面是不是 'E'
        {
            if (input[i - 1] == 'E' || input[i - 1] == 'e')
            {
                yes = true;
            }
        }
        else if (t == '.' && type != "float") //小數點
        {
            yes = true;
            type = "float";
        }
        if (yes == false) //都不是那隻能是文本
        {
            return "string";
        }
    }
    //都跑到這裏來的,説明是數字
    if (type != "float")
    {
        type = "int";
    }
    //然後判斷大小
    if(type == "int") //這是整數
    {
        int int1 = 0;
        long long1 = 0;
        decimal decimal1 = 0;
        if (int.TryParse(input,out int1) == true) //32位整數
        {
            return "int";
        }
        else if (long.TryParse(input,out long1) == true) //64位整數
        {
            return "long";
        }
        else if (decimal.TryParse(input,out decimal1) == true) //128位數字
        {
            return "decimal";
        }
        else //超級無敵大
        {
            return "biginteger";
        }
    }
    else //這是浮點數
    {
        double double1 = 0;
        //去除符號
        if (input[0] == '-')
        {
            input = input.Substring(1);
        }
        //科學計數法展開
        if (kexuejishufa == true)
        {
            int E = input.IndexOf("E", StringComparison.InvariantCultureIgnoreCase);
            //檢查 E 旁邊有沒有數字
            if (E == 0 || E == input.Length - 1) // 1e
            {
                return "string";
            }
            else if(E == input.Length - 2 && (input[input.Length - 1] == '-' || input[input.Length - 1] == '+')) //1e+
            {
                return "string";
            }
            //獲取 E 後邊的數
            E = int.Parse(input.Substring(E + 1, input.Length - 1 - E));
            input = input.Substring(0, input.IndexOf("E", StringComparison.InvariantCultureIgnoreCase));
            int potindex = input.IndexOf(".");
            input = input.Replace(".", "");
            if (E < 0) //加小數點
            {
                int newpot = E + potindex;
                int length = input.Length;
                if (length > newpot) {
                    for (int t = 0; t < newpot * -1 + 1; t++)
                    {
                        input = "0" + input;
                    }
                }
            }
            else //減小數點
            {
                int newpot = E + potindex;
                int length = input.Length;
                if (newpot > length) {
                    for (int t = 0; t < newpot - length; t++)
                    {
                        input = input + "0";
                    }
                }
                //好像沒必要再把小數點填回去, 後面只比對有多少位有效數字
            }
            //ConsoleLog(input);
        }

        //if (input.Substring(input.IndexOf("."), endindex).Length > 16 ||
        //    input.Substring(0, input.IndexOf(".")).Length > 16) //高精度浮點
        input = input.Replace(".","");
        //繼續比對
        if (input.Length > 17) //高精度浮點
        {
            return "decimal";
        }
        else
        {
            if (double1 > float.MaxValue) //雙精度浮點
            {
                return "double";
            }
            //else if (input.Substring(input.IndexOf("."), endindex).Length > 7 ||
            //    input.Substring(0, input.IndexOf(".")).Length > 7) //超過單精度範圍還是雙精度浮點
            else if (input.Length > 8)
            {
                return "double";
            }
            else //單精度浮點
            {
                return "float";
            }
        }
    }
}
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.