表达式计算器

2018-06-23 23:22:29来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

  工作一段时间了,第一次写博客,只是记录一些平时写的练习代码。这次是记录一个练习Stack的例子,代码写的有点乱,以后慢慢优化吧。

  第一次写,写的不好还请多多指教。

 

  思路:遍历字符串,遇到操作符,就把数字入栈。遇到 * 或 / ,如果下一个是数字,就出栈前一个数字,计算结果后再入栈,如果是 + 或 - ,计算之前的结果后入栈。

一个栈处理的有点乱,有时间会继续优化。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 
  5 namespace CountDLL
  6 {
  7     public class FormulaCount
  8     {
  9         /// <summary>
 10         /// 暂时只计算简单的加减乘除和括号
 11         /// </summary>
 12         /// <param name="Formula"></param>
 13         /// <returns></returns>
 14         public static double GetResult(string Formula)
 15         {
 16             char[] chars = { '+', '-', '*', '/', '(', ')' };
 17             char[] chars1 = { '*', '/' };
 18             char[] chars2 = { '+', '-' };//遇到这些符号就计算之前的结果,把计算结果入栈
 19 
 20             string[] str_arr = Formula.Split(chars, StringSplitOptions.RemoveEmptyEntries);
 21 
 22             Stack<string> stack_char = new Stack<string>();
 23 
 24             string num = "";
 25             double d_pre = 0.0;
 26             double d_next = 0.0;
 27             double d_result = 0.0;
 28 
 29             char c = '0';
 30 
 31             int length = Formula.Length;
 32             for (int i = 0; i <= length; i++)
 33             {
 34                 if (i < length)
 35                 {
 36                     char s = Formula[i];
 37                     if (chars.Contains(s))
 38                     {
 39                         if (s == '(')
 40                         {
 41                             if (c != '0')
 42                             {
 43                                 stack_char.Push(c.ToString());
 44                             }
 45                             stack_char.Push(s.ToString());
 46                             num = "";
 47                             c = '0';
 48                             continue;
 49                         }
 50 
 51                         if (chars1.Contains(c))
 52                         {
 53                             d_pre = Convert.ToDouble(stack_char.Pop());
 54 
 55                             d_next = Convert.ToDouble(num);
 56 
 57                             if (c == '/' && d_next == 0.0)
 58                             {
 59                                 throw new Exception("被除数为0");
 60                             }
 61                             d_result = Count(d_pre, d_next, c);
 62 
 63                             d_pre = d_result;
 64 
 65                             stack_char.Push(d_result.ToString());
 66                             num = "";
 67                             c = '0';
 68                         }
 69 
 70                         if (num != "")
 71                         {
 72                             stack_char.Push(num);
 73                             d_pre = 0.0;
 74                             num = "";
 75                         }
 76 
 77                         if (chars1.Contains(s))//乘除直接计算
 78                         {
 79                             c = s;
 80                             continue;
 81                         }
 82                         else if (chars2.Contains(s))//遇到加减,计算这个符号前面的结果
 83                         {
 84                             if (stack_char.Count >= 3)
 85                             {
 86                                 d_next = Convert.ToDouble(stack_char.Pop());
 87                                 char c1 = stack_char.Pop()[0];
 88                                 if (c1 == '(')
 89                                 {
 90                                     stack_char.Push(c1.ToString());
 91                                     stack_char.Push(d_next.ToString());
 92                                     stack_char.Push(s.ToString());
 93                                     continue;
 94                                 }
 95 
 96                                 d_pre = Convert.ToDouble(stack_char.Pop());
 97 
 98                                 d_result = Count(d_pre, d_next, c1);
 99 
100                                 stack_char.Push(d_result.ToString());
101                             }
102                             stack_char.Push(s.ToString());
103                         }
104                         else if (s == ')')
105                         {
106                             d_next = Convert.ToDouble(stack_char.Pop());
107                             char c1 = stack_char.Pop()[0];
108                             while (c1 != '(')
109                             {
110                                 d_pre = Convert.ToDouble(stack_char.Pop());
111 
112                                 d_next = Count(d_pre, d_next, c1);
113                                 c1 = stack_char.Pop()[0];
114                             }
115                             stack_char.Push(d_next.ToString());
116                         }
117                     }
118                     else
119                     {
120                         num += s;
121                         d_pre = 0.0;
122                     }
123                 }
124                 else
125                 {
126                     if (!string.IsNullOrEmpty(num))
127                     {
128                         d_next = Convert.ToDouble(num);
129                     }
130                     else
131                     {
132                         d_next = Convert.ToDouble(stack_char.Pop());
133                     }
134                     while (stack_char.Count >= 2)
135                     {
136                         char c1 = stack_char.Pop()[0];
137                         d_pre = Convert.ToDouble(stack_char.Pop());
138 
139                         d_next = Count(d_pre, d_next, c1);
140                     }
141                     stack_char.Push(d_next.ToString());
142                 }
143             }
144 
145             return Convert.ToDouble(stack_char.Pop());
146         }
147 
148         private static double Count(double d_pre, double d_next, char c)
149         {
150             double d_result = 0.0;
151 
152             switch (c)
153             {
154                 case '+':
155                     d_result = d_pre + d_next;
156                     break;
157                 case '-':
158                     d_result = d_pre - d_next;
159                     break;
160                 case '*':
161                     d_result = d_pre * d_next;
162                     break;
163                 case '/':
164                     d_result = d_pre / d_next;
165                     break;
166                 default: break;
167             }
168 
169             return d_result;
170         }
171     }
172 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java实现邮箱找回密码 --转载

下一篇:5.怎么以域名的形式来浏览网站(内网 + 外网)?