標籤:collect 判斷 result 演算法設計 控制 math adl image 階段
鎮場詩:
———大夢誰覺,水月中建部落格。百千磨難,才知世事無常。
———今持佛語,技術無量願學。願盡所學,鑄一良心部落格。
——————————————————————————————————————————
1 code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication15 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //這個字典是為了遇到16進位到10進位轉換時遇到字母準備的 14 //如果是字母,那麼運行字典轉換成十進位數 15 Dictionary<char, int> tableOf16_10 = new Dictionary<char, int>(); 16 tableOf16_10.Add(‘a‘, 10); 17 tableOf16_10.Add(‘b‘, 11); 18 tableOf16_10.Add(‘c‘, 12); 19 tableOf16_10.Add(‘d‘, 13); 20 tableOf16_10.Add(‘e‘, 14); 21 tableOf16_10.Add(‘f‘, 15); 22 23 24 25 Console.WriteLine("your turn:"); 26 string value = Console.ReadLine(); 27 28 //分離字串,以小數點為界限 29 string[] values = value.Split(new char[] { ‘.‘ }, StringSplitOptions.RemoveEmptyEntries); 30 31 //儲存小數部分的字串 32 string decimalsPart = values[1]; 33 //儲存整數部分的字串 34 string integerPart = values[0]; 35 36 //小數部分出來的10進位數字一定是double類型的 37 double sumOfDecimalsPart = 0.0; 38 39 char temp; 40 for (int i = 0; i < decimalsPart.Length; i++) 41 { 42 //decimalsPart[i]是char類型,所以要加一個tostring 43 //在演算法設計的初級階段,不要寫出一個有多個變數的長式子, 44 //要把式子的每個變數都用單獨命名,然後調試的時候可以檢測到是哪一個變數出了問題 45 //int itemp = Convert.ToInt32(decimalsPart[i].ToString()); 46 47 temp = decimalsPart[i]; 48 49 //判斷decimalsPart[i]是否是字母 50 if (char.IsLetter(temp)) 51 { 52 if(char.IsUpper(temp)) 53 { 54 temp =Convert.ToChar(Convert.ToInt32(temp)+32); 55 56 } 57 //如果是字母,就用字典來轉換 58 sumOfDecimalsPart += Convert.ToDouble(tableOf16_10[temp] * Math.Pow(16, -(i + 1))); 59 } 60 else 61 { 62 //如果不是字母正常對待 63 sumOfDecimalsPart += Convert.ToDouble(Convert.ToInt32(decimalsPart[i].ToString()) * Math.Pow(16, -(i + 1))); 64 } 65 66 } 67 68 69 int sumOfintegerPart = 0; 70 for (int i = 0; i < integerPart.Length; i++) 71 { 72 //decimalsPart[i]是char類型,所以要加一個tostring 73 //在演算法設計的初級階段,不要寫出一個有多個變數的長式子, 74 //要把式子的每個變數都用單獨命名,然後調試的時候可以檢測到是哪一個變數出了問題 75 //int itemp = Convert.ToInt32(decimalsPart[i].ToString()); 76 temp = integerPart[i]; 77 //判斷decimalsPart[i]是否是字母 78 if (char.IsLetter(temp)) 79 { 80 if (char.IsUpper(temp)) 81 { 82 temp = Convert.ToChar(Convert.ToInt32(temp) + 32); 83 84 } 85 //如果是字母,先轉換成小寫字母,再用字典來轉換 86 sumOfintegerPart += Convert.ToInt32(tableOf16_10[char.ToLower(temp)] * Math.Pow(16, i)); 87 } 88 else 89 { 90 //如果不是字母正常對待 91 sumOfintegerPart += Convert.ToInt32(Convert.ToInt32(integerPart[i].ToString()) * Math.Pow(16,i)); 92 } 93 } 94 95 96 Console.Write("decimalsPart:"); 97 Console.WriteLine(decimalsPart); 98 99 Console.Write("sumOfDecimalsPart:");100 Console.WriteLine(sumOfDecimalsPart);101 102 Console.Write("integerPart:");103 Console.WriteLine(integerPart);104 105 Console.Write("sumOfintegerPart:");106 Console.WriteLine(sumOfintegerPart);107 108 Console.Write("result:");109 110 // 這裡實現的是整數部分與小數部分的拼接111 Console.WriteLine(sumOfintegerPart+sumOfDecimalsPart.ToString().Substring(1));112 Console.ReadKey();113 }114 }115 }
2 show1
show2
C#控制台基礎 無符號十六進位小數轉換為十進位