还是我太敏感了,感觉周末弹吉他被那个什么了,好吧,带上耳机调到50%以上的音乐听歌也是不错的选择,暂时找不到想看的电影电视,也不敢再去找电视看了,太多了,也太长了,刚买回来的书还没开始捂热乎呢,那就打开vs敲代码吧,先去弄个基础点的东西,不过个人觉得人为计算这个没有多大实际意义,当然这个跟我菜鸟身份有关,对编程感兴趣的同学可以看看,这次就以一种非正规的编写习惯上吧,计算公式部分和逻辑就不给出注释了
static void Main(string[] args) { Input(); } /// <summary> /// 录入数据 /// </summary> private static void Input() { Console.WriteLine("请选择一种转换方式:"); Console.WriteLine("A:十进制转二进制"); Console.WriteLine("B:二进制转十进制"); Console.WriteLine("C:十进制转十六进制"); Console.WriteLine("D:十六进制转十进制"); string selectType = Console.ReadLine().Trim().ToUpper(); Console.WriteLine("请录入要转换的数据:"); string input = Console.ReadLine(); string result = ""; switch (selectType) { case "A": result = Convert10ToOther(input, 2); break; case "B": result = ConvertOtherTo10(input, 2); break; case "C": result = Convert10ToOther(input, 16); break; case "D": result = ConvertOtherTo10(input, 16); break; default: break; } if (String.IsNullOrEmpty(result)) { Input(); return; } Console.WriteLine("结果为:{0}", result); Console.WriteLine("按'Y'继续,任意键退出:"); if (Console.ReadLine().Trim().ToUpper() == "Y") { Input(); } } /// <summary> /// 将十进制转成其他进制 /// </summary> /// <param name="input"></param> /// <param name="baseNumber"></param> /// <returns></returns> private static string Convert10ToOther(string input, int baseNumber) { int source; if (!int.TryParse(input, out source)) { Console.WriteLine("输入格式有误,请重试:"); return ""; } List<int> target = new List<int>(); while (source > 0) { target.Add(source % baseNumber); source = Convert.ToInt32(Math.Floor(source / Convert.ToDecimal(baseNumber))); } target.Reverse(); StringBuilder sbResult = new StringBuilder(); foreach (int item in target) { sbResult.Append(Get16NumEng(item)); } return sbResult.ToString(); } /// <summary> /// 将其他进制转成十进制 /// </summary> /// <param name="input"></param> /// <param name="baseNumber"></param> /// <returns></returns> private static string ConvertOtherTo10(string input, int baseNumber) { char[] source = input.ToCharArray(); List<string> liSour = new List<string>(); for (int i = source.Length - 1; i > -1; i--) { liSour.Add(source[i].ToString()); } int result = 0; for (int i = 0; i < liSour.Count; i++) { result += Convert.ToInt32(Convert.ToInt32(Get16NumInt(liSour[i])) * Math.Pow(baseNumber, i)); } return result.ToString(); } /// <summary> /// 十六进制英文对应的数字 /// </summary> /// <param name="sou"></param> /// <returns></returns> private static string Get16NumInt(string sou) { string result; switch (sou.ToUpper()) { case "A": result = "10"; break; case "B": result = "11"; break; case "C": result = "12"; break; case "D": result = "13"; break; case "E": result = "14"; break; case "F": result = "15"; break; default: result = sou.ToString(); break; } return result; } /// <summary> /// 十六进制数字对应的英文 /// </summary> /// <param name="sou"></param> /// <returns></returns> private static string Get16NumEng(int sou) { string result; switch (sou) { case 10: result = "A"; break; case 11: result = "B"; break; case 12: result = "C"; break; case 13: result = "D"; break; case 14: result = "E"; break; case 15: result = "F"; break; default: result = sou.ToString(); break; } return result; }