写给父亲的语音计算器('('')'算法参与运算c#,二)

这一节把字符串变为操作数,操作符,然后按优先级顺序,一步一步自动计算出结果,即,先内层括号计算,后外层,先乘除,后加减。

首先,制定规则,看完程序就明白:

a,如果是操作符,textBox1.Text += "\r\n" + "+" + "\r\n";

b,如果是操作数,   textBox1.Text += "3";

其次,通过"\r\n"分割符拆分到  string[] lines ,代码如下:

 char[] separator = { '\r', '\n' };
            string[] lines = textBox1.Text.Split(separator);

            for (int i = 0; i < lines.Length; i++)
            {
                if ("" == lines[i]) continue;
                string temp2 = lines[i];
                lineslist观察.Add(temp2);
            }

最后,调用括号优先级处理递归算法,这个算法在此做了调整,加入了运算,从内到外,逐层去掉了两个?,函数的参数也不再是字符串,而是 lineslist观察(List<string> tempstr)。代码如下:

  List<string> lineslist观察 = new List<string>();

   public void 括号递归处理(ref   List<string> tempstr)
       {
           List<string> subpstr1 = new List<string>();
           for (int n = 0; n < tempstr.Count; n++)//*****
           {
               
               subpstr1.Add(tempstr[n]);//需要测试1//测试ok
              
               if (tempstr[n] == "(")
               {
                   //截取字符串,调用自己
                  // string subpstr = tempstr.Substring(n + 1, tempstr.Length - n - 1);
                  // string subpstr1 = tempstr.Substring(0, n) + "?";//翻译完成,需要测试1,2
                   subpstr1.RemoveAt(n); subpstr1.Insert(n, "?");

                   List<string> subpstr = new List<string>();
                   for (int i = n + 1; i < tempstr.Count; i++)
                   {
                       subpstr.Add(tempstr[i]);//需要测试2//测试ok
                   }
                   括号递归处理(ref subpstr);
                   for (int i = 0; i < subpstr.Count; i++)
                   {
                       if (subpstr[i] == ")")
                       {

                           //char[] tempchar = subpstr.ToCharArray(); 
                           //需要测试3,先验证递归    //测试ok                    
                           //tempchar[i] = '?';
                           subpstr.RemoveAt(i); subpstr.Insert(i, "?");
                           //可以调用加减乘除函数了
                           List<string> temp = new List<string>();
                           for (int t = 0; t < i; t++)
                           {
                               temp.Add(subpstr[t]); //subpstr.RemoveAt(0);
                           }
                           try { 加减乘除(ref temp); }
                           catch (Exception e) { }
                           for (int t = 0; t < i; t++)
                           {
                               subpstr.RemoveAt(0);
                           }
                           subpstr.RemoveAt(0);
                           subpstr.Insert(0, temp[0]);
                           //string char2str = "";
                           //for (int i1 = 0; i1 < tempchar.Length; i1++)
                           //{
                           //    char2str += tempchar[i1];
                           //}
                           //tempstr = subpstr1 + char2str;
                           tempstr.Clear();
                           subpstr1.RemoveAt(subpstr1.Count - 1);
                           tempstr.AddRange(subpstr1);
                           tempstr.AddRange(subpstr);
                           i = subpstr.Count;
                       }

                   }
               }
           }
       }

注,加减乘除函数下一节解释,一次搞定一个问题,短小精悍

待续(慢慢来!...........)每天一点小改变☺

我的邮箱[email protected];[email protected]

发布了66 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ganggangwawa/article/details/102729806