澳门至尊网站-首页

您的位置:澳门至尊网站 > 搜索引擎 > 控制台语音计算器

控制台语音计算器

2019-10-26 05:06

回想上高中时,给人当会计,帮助结账月度薪资;用的正是带语音功效的总结器! 那时候用起来倍儿爽,于是速度加倍,效用加速;结果让业主赔了许多钱!

就是因为那一个,才对语音总括器有了深入影象!恐怕是那货坑了自个儿!哼~!

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

近期在做二个品种,有个大约的效率,正是将文件调换到语音。

琢磨了那么些效果后,抽空顺带做了个语音计算器!

“来银啊,上代码!”

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

“哎哎小编去,非逼笔者说粗话,来个货,把代码码上来!”

“好的,老大!”

哈哈,我正是极其带点儿逗比的搬砖员儿!可以吗,代码来了,小主,等急了呢?

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 AssembliesMicrosoftFramework.NETFrameworkv4.5

寻寻找觅,消声匿迹,凄悲凉惨戚戚!什么?没搜索到?

好吧,教你意气风发招

图片 1

 

开荒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 有意者迎接打扰,感谢!

 

本文由澳门至尊网站发布于搜索引擎,转载请注明出处:控制台语音计算器

关键词: