你的位置:首页 > ASP.net教程

[ASP.net教程]C# 控制台语音计算器


记得上高中时,给人当会计,帮忙结算月度工资;用的就是带语音功能的计算器! 当时用起来倍儿爽,于是速度加倍,效率加速;结果让老板赔了不少钱!

就是因为这,我才对语音计算器有了深刻印象!可能是这货坑了我!哼~!

好吧,闲言少叙,直入正题吧!

最近在做一个项目,有个简单的功能,就是将文本转换成语音。

研究了这个功能后,抽空顺带做了个语音计算器!

“来银啊,上代码!”

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

“哎呀我去,非逼我说粗话,来个货,把代码码上来!”

“好的,老大!”

哈哈哈,俺就是那个带点儿逗比的搬砖员儿!好吧,代码来了,小主,等急了吧?

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);    }  }}

ok,这就是全部代码了,请容在下粗略的介绍下实现步骤:

1.既然是语音计算器,首先要解决的就是如何实现语音?

其实,语音功能实现起来并不复杂,微软早就封装好了一个,将文本转换成语音功能的类库,现在你要做的就是:找到它,然后把它引用到你的项目里!

什么?这个类库叫啥?

表急嘛,心急吃不了“热豆腐”,有可能“豆腐”还跑了呢,要耐心点儿,才能约到手!是吧?

好吧,介绍下这个类库,它叫“System.Speech.dll”。.net FrameWork 的安装目录里就有它的身影!装了.net FrameWork 的童鞋,可以通过以下目录去寻觅它!

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

寻寻觅觅,冷冷清清,凄凄惨惨戚戚!什么?没寻觅到?

好吧,教你一招

 

打开C盘,直接搜索!

没装的,那就去网上搜吧,自己下崽儿!哈哈~!愿意装的也可以装一下!

找到的,直接引用到项目里就行了哈!

2.其次,别忘了引用下这两个命名空间:

  using System.Speech.Synthesis;  //语音类库

  using System.Collections.Generic;  //数组集合类库

3.最后,简单介绍下文本转换成语音的功能!

其实,也就几句话!

SpeechSynthesizer spvoice = new SpeechSynthesizer(); //语音类spvoice.Rate = 1;       //使用 spvoice 设置朗读频率 [范围 -10 ~ 10] spvoice.Volume = 80;     //使用 spvoice 设置朗读音量 [范围 0 ~ 100] spvoice.SpeakAsync("大家好,我是博主小白!");  //开始读

好了,其他的代码就要看小主的内力了,在下就不多说了!

看小主骨骼惊奇,“英语非凡”,想是定能习得搬砖精髓,打遍天下需求,杀掉世间bug;然后走上淫僧巅峰,赢取大白美眉的!哈哈哈~!

 

拙文一篇,望各位海涵!

技术讨论群:225443677 有意者欢迎骚扰,谢谢!