2-String to Integer (atoi)

實現atoi這個函數, public int atoi(String str),傳入字符串str能夠返回整數,請仔細考慮一下字符串的各類狀況!html

String to Integer: Case分析ide

  1. 正常數字

Sample:」123」,」0」,」1」 ,"-1"函數

  1. 普通特殊字符:

Sample: "000","001","-001"spa

  1. 正負號問題
    1. 含有正負號

Sample: " -123", " +123", "-123", "+123","--123","++123","  -004500"code

  1. 空格問題
    1. 含有空格

Sample: " 123","123 123","123 "htm

  1. 包含特殊符號
    1. 字母,其它特殊符號

Sample: "*123","*abc","~123","123~", "a123","12a3", "12+3","12-3"blog

  1. 空的字符串

Sample: string.Empty,null,""," "字符串

  1. 邊界問題
    1. 正常或者超過Int32最大和最小值,輸出最大 或最小Int32

Sample: "-2147483648","2147483647"string

Sample: "-214748364800","214748364700"it

Snapshot:

                       

Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//http://www.cnblogs.com/binyao/p/5026406.html

namespace StringtoInteger
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = { string.Empty,null,""," ",
                            "0","1","123","000","001","-1","-001",
                            " 123","123 123","123 ",
                            " -123", " +123",
                            "-123", "+123","--123","++123","  -004500",
                            "*123","*abc","~123","123~",
                            "a123","12a3",
                            "12+3","12-3",
                            "-2147483648","2147483647",
                            "-214748364800","214748364700" };
            StringtoInteger(str);
        }

        public static void StringtoInteger(string[] str)
        {            
            Console.WriteLine("StringtoInteger Result is:");
            foreach (string s in str)
            {
                Console.Write("Test Data:{0}", s);
                Console.WriteLine("  Result:{0}", StringtoInteger(s));
            }
        }

        public static int StringtoInteger(string str)
        {
            int sign = 0;
            int i = 0;
            int result = 0;

            if (string.IsNullOrEmpty(str))
            {
                return result;
            }

            while (i < str.Length && ((str[i] >= '0' && str[i] <= '9') || str[i] == ' ' || str[i] == '-' || str[i] == '+'))
            {
                if (str[i] == ' ' && (result == 0 && sign == 0))
                {
                    i++;
                }
                else if (str[i] == '+' && (result == 0 && sign == 0))
                {
                    sign = 1;
                    i++;
                }
                else if (str[i] == '-' && (result == 0 && sign == 0))
                {
                    sign = -1;
                    i++;
                }
                else if (str[i] >= '0' && str[i] <= '9')
                {
                    if (result > (int.MaxValue - (str[i] - '0')) / 10)
                    {
                        if (sign == 0 || sign == 1)
                            return int.MaxValue;
                        return int.MinValue;
                    }

                    result = result * 10 + str[i] - '0';
                    i++;
                }
                else
                {
                    if (sign == 0)
                        return result;
                    return result * sign;
                }
            }

            if (sign == 0)
                return result;
            return result * sign;
        }
    }
}
View Code
相關文章
相關標籤/搜索