static void Main(string[] args) { Console.WriteLine("please input your number"); var userInput = Console.ReadLine(); try { for (int i = 0; i < userInput.Length; i++) { var c = userInput[i]; CheckCharFun = null; var type = GetCharType(c); if (CheckCharFun.Invoke(c, i)) { CheckedTypes.Add(type); } else { throw new Exception("not a valid number"); } } Console.WriteLine("success"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }
static CharType GetCharType(char c) { if (Array.IndexOf(DigitalChar, c) > -1) { CheckCharFun += CheckDigital; return(CharType.Digital); } if (Array.IndexOf(PointChar, c) > -1) { CheckCharFun += CheckPoint; return(CharType.Point); } if (Array.IndexOf(EChar, c) > -1) { CheckCharFun += CheckE; return(CharType.E); } if (Array.IndexOf(PlusChar, c) > -1) { CheckCharFun += CheckPlus; return(CharType.Plus); } else { throw new Exception("not a valid number"); } }
static private int GetToken(string s, ref int i, ref List<string> words, CheckChar func, bool ifTrue) { if (i == -1) { return i; } string word = ""; if (ifTrue) { while (i < s.Length && func(s[i])) { word += s[i++]; } } else if (!ifTrue) { while (i < s.Length && !func(s[i])) { word += s[i++]; } } else { } if (word.Length > 0) { words.Add(word); } return i < s.Length ? i : -1; }