C#读取一个双精度浮点数Double方法-自已封装
生活随笔
收集整理的這篇文章主要介紹了
C#读取一个双精度浮点数Double方法-自已封装
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#讀取一個雙精度浮點數Double方法-自已封裝
封裝一個靜態方法ReadDouble()
// 優化讀取Double雙精度浮點數 // 空白字符結尾 public static double readDouble() {double r1 = 0, r2 = 0, k = 10;bool flag = false;int c;do{c = Console.Read() - 48;if (c >= 0 && c <= 9){if (!flag){r1 = r1 * 10 + (double)c;}else{r2 += (double)c / k;k *= 10;}}else if (c + 48 == '.'){flag = true;}} while ((c >= 0 && c <= 9) || c + 48 == '.');return r1 + r2; } // 換行結尾 public static double readDoubleLine() {double r1 = 0, r2 = 0, k = 10;bool flag = false;int c;do{c = Console.Read() - 48;if (c >= 0 && c <= 9){if (!flag){r1 = r1 * 10 + (double)c;}else{r2 += (double)c / k;k *= 10;}}else if (c + 48 == '.'){flag = true;}} while (c + 48 != '\n');return r1 + r2; }測試
double a, b, c, p; a = readDoubleLine(); b = readDoubleLine(); c = readDoubleLine(); p = (a + b + c) / 2; Console.WriteLine("a=" + a); Console.WriteLine("b=" + b); Console.WriteLine("c=" + c); Console.WriteLine("p=" + p);輸出
123.456 234.567 456.789 a=123.456 b=234.567 c=456.789 p=407.406總結
以上是生活随笔為你收集整理的C#读取一个双精度浮点数Double方法-自已封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++Primer_Chap16_模板和
- 下一篇: 面试:1.C#中的委托是什么?事件是一种