zoukankan      html  css  js  c++  java
  • 字符串转整数(C#)

    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

  • 相关阅读:
    luogu P1630 求和(枚举暴力)
    luogu P3414 SAC#1
    luogu P1869 愚蠢的组合数(质因数+瞎搞)
    luogu P1586 四方定理(背包)
    luogu P3795 钟氏映射(递推)
    2017.8.15 [Haoi2016]字符合并 区间dp+状压dp
    [NOI2002] 荒岛野人 扩展欧几里得算法
    [Noi2002]Savage 扩展欧几里得
    bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡
    bzoj 3505: [Cqoi2014]数三角形
  • 原文地址:https://www.cnblogs.com/pengpenghappy/p/3886830.html
Copyright © 2011-2022 走看看