日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 控制台语音计算器

發(fā)布時間:2024/8/26 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 控制台语音计算器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

記得上高中時,給人當(dāng)會計(jì),幫忙結(jié)算月度工資;用的就是帶語音功能的計(jì)算器! 當(dāng)時用起來倍兒爽,于是速度加倍,效率加速;結(jié)果讓老板賠了不少錢!

就是因?yàn)檫@個,才對語音計(jì)算器有了深刻印象!可能是這貨坑了我!哼~!

好吧,閑言少敘,直入正題吧!

最近在做一個項(xiàng)目,有個簡單的功能,就是將文本轉(zhuǎn)換成語音。

研究了這個功能后,抽空順帶做了個語音計(jì)算器!

“來銀啊,上代碼!”

“老大,木有銀,上不了”

“哎呀我去,非逼我說粗話,來個貨,把代碼碼上來!”

“好的,老大!”

哈哈哈,俺就是那個帶點(diǎn)兒逗比的搬磚員兒!好吧,代碼來了,小主,等急了吧?

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 = "語音計(jì)算器";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();}//判斷用戶輸入的內(nèi)容是否合法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 != "";}//計(jì)算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);}} }

ok,這就是全部代碼了,請容在下粗略的介紹下實(shí)現(xiàn)步驟:

1.既然是語音計(jì)算器,首先要解決的就是如何實(shí)現(xiàn)語音?

其實(shí),語音功能實(shí)現(xiàn)起來并不復(fù)雜,微軟早就封裝好了一個,將文本轉(zhuǎn)換成語音功能的類庫,現(xiàn)在你要做的就是:找到它,然后把它引用到你的項(xiàng)目里!

什么?這個類庫叫啥?

表急嘛,心急吃不了“熱豆腐”,有可能“豆腐”還跑了呢,要耐心點(diǎn)兒,才能約到手!是吧?

好吧,介紹下這個類庫,它叫“System.Speech.dll”。.net FrameWork 的安裝目錄里就有它的身影!裝了.net FrameWork 的童鞋,可以通過以下目錄去尋覓它!

大概是這個:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5

尋尋覓覓,冷冷清清,凄凄慘慘戚戚!什么?沒尋覓到?

好吧,教你一招

?

打開C盤,直接搜索!

沒裝的,那就去網(wǎng)上搜吧,自己下崽兒!哈哈~!愿意裝的也可以裝一下!

找到的,直接引用到項(xiàng)目里就行了哈!

2.其次,別忘了引用下這兩個命名空間:

  using System.Speech.Synthesis;  //語音類庫

  using System.Collections.Generic;  //數(shù)組集合類庫

3.最后,簡單介紹下文本轉(zhuǎn)換成語音的功能!

其實(shí),也就幾句話!

SpeechSynthesizer spvoice = new SpeechSynthesizer(); //語音類 spvoice.Rate = 1;  //使用 spvoice 設(shè)置朗讀頻率 [范圍 -10 ~ 10] spvoice.Volume = 80; //使用 spvoice 設(shè)置朗讀音量 [范圍 0 ~ 100] spvoice.SpeakAsync("大家好,我是博主小白!"); //開始讀

好了,其他的代碼就要看小主的內(nèi)力了,在下就不多說了!

看小主骨骼驚奇,“英語非凡”,想是定能習(xí)得搬磚精髓,打遍天下需求,殺掉世間bug;然后走上淫僧巔峰,贏取大白美眉的!哈哈哈~!

?

拙文一篇,望各位海涵!

技術(shù)討論群:225443677 有意者歡迎騷擾,謝謝!

?

轉(zhuǎn)載于:https://www.cnblogs.com/LittleBai/p/5897255.html

總結(jié)

以上是生活随笔為你收集整理的C# 控制台语音计算器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。