标签:
using System; using System.Speech.Synthesis; using System.Collections.Generic; namespace ReadTxt { class Program { static string num1 = "", firchar = "", lastchar = "", tempRe = ""; static void Main(string[] args) { Console.Title = "语音计算器"; bool jump = true; do { ConsoleKeyInfo info = Console.ReadKey(); switch (info.Key) { case ConsoleKey.Escape: Environment.Exit(0); jump = false; break; case ConsoleKey.NumPad0: GetKeyRead("0"); break; case ConsoleKey.NumPad1: GetKeyRead("1"); break; case ConsoleKey.NumPad2: GetKeyRead("2"); break; case ConsoleKey.NumPad3: GetKeyRead("3"); break; case ConsoleKey.NumPad4: GetKeyRead("4"); break; case ConsoleKey.NumPad5: GetKeyRead("5"); break; case ConsoleKey.NumPad6: GetKeyRead("6"); break; case ConsoleKey.NumPad7: GetKeyRead("7"); break; case ConsoleKey.NumPad8: GetKeyRead("8"); break; case ConsoleKey.NumPad9: GetKeyRead("9"); break; case ConsoleKey.Add: GetKeyRead("加"); break; case ConsoleKey.Subtract: GetKeyRead("减"); break; case ConsoleKey.Multiply: GetKeyRead("乘"); break; case ConsoleKey.Divide: GetKeyRead("除"); break; case ConsoleKey.Enter: if (!string.IsNullOrEmpty(num1) && GetSignIsTrue(num1)) { SetValue(num1); num1 = ""; } else { num1 = ""; if (!string.IsNullOrEmpty(num1)) { Console.Beep(); Console.WriteLine("Error."); } } break; default: break; } } while (jump); Console.Read(); } //判断用户输入的内容是否合法 static void GetKeyRead(string str) { SpeechSynthesizer spvoice = new SpeechSynthesizer(); spvoice.Rate = 1; spvoice.Volume = 100; if (!string.IsNullOrEmpty(num1)) { firchar = num1.Substring(0, 1); lastchar = num1.Substring(num1.Length - 1, 1); } switch (str) { case "加": if (firchar != "加" && lastchar != "加") { if (lastchar != "减" && lastchar != "乘" && lastchar != "除") { num1 += str; } else { num1 = num1.Remove(num1.Length - 1); num1 += str; } } break; case "减": if (firchar != "减" && lastchar != "减") { if (lastchar != "加" && lastchar != "乘" && lastchar != "除") { num1 += str; } else { num1 = num1.Remove(num1.Length - 1); num1 += str; } } break; case "乘": if (firchar != "乘" && lastchar != "乘") { if (lastchar != "加" && lastchar != "减" && lastchar != "除") { num1 += str; } else { num1 = num1.Remove(num1.Length - 1); num1 += str; } } break; case "除": if (firchar != "除" && lastchar != "除") { if (lastchar != "加" && lastchar != "减" && lastchar != "乘") { num1 += str; } else { num1 = num1.Remove(num1.Length - 1); num1 += str; } } break; default: num1 += str; break; } spvoice.SpeakAsync(str); Console.Clear(); Console.Write(tempRe + num1.Replace(‘加‘, ‘+‘).Replace(‘减‘, ‘-‘).Replace(‘乘‘, ‘*‘).Replace(‘除‘, ‘/‘).Replace("等于", "=")); } static bool GetSignIsTrue(string num1) { if (!string.IsNullOrEmpty(num1)) { firchar = num1.Substring(0, 1); lastchar = num1.Substring(num1.Length - 1, 1); } return firchar != "加" && lastchar != "加" && firchar != "减" && lastchar != "减" && firchar != "乘" && lastchar != "乘" && firchar != "除" && lastchar != "除"; } //计算 static void SetValue(string num1) { List<double> array = new List<double>(); List<string> sign = new List<string>(); num1 = num1.Replace(‘加‘, ‘+‘).Replace(‘减‘, ‘-‘).Replace(‘乘‘, ‘*‘).Replace(‘除‘, ‘/‘); string tempCap = ""; char[] MyChar = num1.ToCharArray(); for (int i = 0; i < MyChar.Length; i++) { if (MyChar[i].ToString() == "+" || MyChar[i].ToString() == "-" || MyChar[i].ToString() == "*" || MyChar[i].ToString() == "/") { array.Add(Convert.ToDouble(tempCap)); tempCap = ""; sign.Add(MyChar[i].ToString()); } else { if (i == MyChar.Length - 1) { tempCap += MyChar[i].ToString(); array.Add(Convert.ToDouble(tempCap)); } else { tempCap += MyChar[i].ToString(); } } } double resultTemp = 0; for (int i = 0; i < sign.ToArray().Length; i++) { if (sign[i] == "*") { resultTemp = array[i] * array[i + 1]; array[i] = resultTemp; array.Remove(array[i + 1]); sign.Remove(sign[i]); resultTemp = 0; i--; } else if (sign[i] == "/") { resultTemp = array[i] / array[i + 1]; array[i] = resultTemp; array.Remove(array[i + 1]); sign.Remove(sign[i]); resultTemp = 0; i--; } } for (int i = 0; i < sign.ToArray().Length; i++) { if (sign[i] == "+") { resultTemp = array[i] + array[i + 1]; array[i] = resultTemp; array.Remove(array[i + 1]); sign.Remove(sign[i]); resultTemp = 0; i--; } else if (sign[i] == "-") { resultTemp = array[i] - array[i + 1]; array[i] = resultTemp; array.Remove(array[i + 1]); sign.Remove(sign[i]); resultTemp = 0; i--; } } double answ = array[0]; array.Clear(); sign.Clear(); tempRe += num1.Replace(‘加‘, ‘+‘).Replace(‘减‘, ‘-‘).Replace(‘乘‘, ‘*‘).Replace(‘除‘, ‘/‘) + "= " + answ + "\n"; GetKeyRead("等于" + answ); } } }
标签:
原文地址:http://www.cnblogs.com/UstimdanKulma/p/5918190.html