using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {                 
            Console.WriteLine("Key In Something:");          /*輸入字串*/
            string keyin = string.Format(Console.ReadLine());
            Console.WriteLine("resutl:" + parse(keyin));
            Console.Read();
        }
        static double parse(String str)
        {
            int sp = 0, len = 0;
            double result = 0.0;/*Num->前一個不是數字就false Dot->前一個不是.就false*/
            bool frontIsNum = false, frontIsDot = false;     /*宣告布林值*/
            Console.WriteLine("strlen:" + str.Length);       /*顯示字串長度*/
           
            try
            {
                int i;
                for (i = 0; i < str.Length; i++)
                {
                    if (str[i] >= '0' && str[i] <= '9') /*判斷是否為數字*/
                    {
                        len++;                          /*字串長度+1 */
                        frontIsNum = true;              /*Num = 1 */
                        frontIsDot = false;             /*Dot = 0 */
                    }
                    else if (str[i] == '.')             /*判斷是否為.*/
                    {
                        if (frontIsDot)
                        {
                            throw new dotException();   /*若是點就丟出例外 到Catch*/
                        }
                        else
                        {
                            if (frontIsNum)
                            {
                                Console.WriteLine(str.Substring(sp, len));
                                result += Double.Parse(str.Substring(sp, len));
                            }
                            len = 0;
                            sp = i;
                            frontIsNum = false;
                            len++;
                            frontIsDot = true;
                        }
                    }
                    else
                    {
                        if (frontIsNum)
                        {
                            Console.WriteLine(str.Substring(sp, len));
                            result += Double.Parse(str.Substring(sp, len));
                        }
                        len = 0;
                        sp = i + 1;
                        frontIsDot = false;
                        frontIsNum = false;
                    }
                }
                Console.WriteLine(" sp:" + sp + " len:" + len + " i:" + i);
                if (frontIsNum)
                {
                    Console.WriteLine(str.Substring(sp, len));
                    result += Double.Parse(str.Substring(sp, len));
                }
            }
            catch (dotException ex)
            {
                Console.WriteLine(ex.ToString());
                return 0.0;
            }
            return result;
        }
    }
    class dotException : System.Exception
    {
        public dotException()
            : base()
        {
        }
        public override string ToString()
        {
            return "連續兩個小數點..";
        }
    }
}



arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Joy 的頭像
    Joy

    正Man's World

    Joy 發表在 痞客邦 留言(0) 人氣()