//数字检验 private static bool IsNum(string input) { input = input.Trim(); if (input[0] == '+' || input[0] == '-') { input = input.Remove(0, 1); } if (input.Split('.').Length > 2) { var e = new BasicCalculatorException("多于一个的小数点!"); throw e; } foreach (var c in input) { if (c == '.') { continue; } if (!Char.IsNumber(c)) { var e = new BasicCalculatorException("存在非数字!"); throw e; } } return(true); }
public static float InterpretInput(string input) { try { IsNum(input); return((float)Convert.ToDecimal(input)); } catch (System.FormatException e) { BasicCalculatorException et = new BasicCalculatorException("存在非数字!(来自报错System.FormatException)"); throw et; } }
public List <float> GetRoot() { //判别式你 float delta = FictionB * FictionB - 4 * FictionA * FictionC; //判别式小于零,无解 if (delta < 0) { BasicCalculatorException e = new BasicCalculatorException("二次方程无解!"); throw e; } this.Result.Add(((-FictionB) + (float)Math.Sqrt(delta)) / (2 * FictionC)); //判别式大于零,两个解 if (delta > 0) { this.Result.Add(((-FictionB) - (float)Math.Sqrt(delta)) / (2 * FictionC)); } return(Result); }