字串轉整數(C#)

來源:互聯網
上載者:User

標籤:style   color   io   for   cti   ar   line   new   

using System;using System.Collections.Generic;namespace StrToInt_CS{    class Program    {        static void Main(string[] args)        {             string str = "";             while (!String.IsNullOrEmpty(str = Console.ReadLine()))             {                 KeyValuePair<int, bool> ret = Program.StrToInt(str);                 if (ret.Value)                 {                     Console.WriteLine("Number for {0} is {1}", str, ret.Key);                 }                 else                 {                     Console.WriteLine("The input {0} is invalid", str);                 }             }        }        public static KeyValuePair<int, bool> StrToInt(string s)        {            bool isMinus = false;            bool isValid = false;            int num = 0;            int k = 0;            // if contain leading space, then invalid            if (s[k] == ‘ ‘)            {                new KeyValuePair<int, bool>(num, isValid);            }            // if contain operator            if (s[k] == ‘-‘)            {                isMinus = true;                k++;            }            if (k < s.Length && s[k] == ‘+‘)            {                isMinus = false;                k++;            }             int flag = isMinus ? -1 : 1;            for (int i = k; i < s.Length; i++)            {                if (s[i] >= ‘0‘ && s[i] <= ‘9‘)                {                    try                    {                        // check number is not overflow                        checked                        {                            num = num * 10 + flag * (s[i] - ‘0‘);                        }                    }                    catch (Exception)                    {                        isValid = false;                        break;                    }                }                else                {                    break;                }                // if complete traverse the whole string, then is valid                 if (i + 1 == s.Length)                 {                    isValid = true;                }             }            return new KeyValuePair<int,bool>( num, isValid);        }    }}

Test cases:

null
The input null is invalid
""
The input "" is invalid
" "
The input " " is invalid

The input    is invalid
123
Number for 123 is 123
+123
Number for +123 is 123
-123
Number for -123 is -123
1a3333
The input 1a3333 is invalid
+0
Number for +0 is 0
-0
Number for -0 is 0
2147483647
Number for 2147483647 is 2147483647
2147483648
The input 2147483648 is invalid
-2147483648
Number for -2147483648 is -2147483648
-2147483649
The input -2147483649 is invalid
+
The input + is invalid
-
The input - is invalid
++
The input ++ is invalid
__
The input __ is invalid
--
The input -- is invalid

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.