惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
爱范儿
爱范儿
罗磊的独立博客
V
V2EX
量子位
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
Jina AI
Jina AI
博客园 - 叶小钗
小众软件
小众软件
博客园 - 【当耐特】
Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 空军

过多边形边上某点的任意直线等分面积 VisualAPL Installer for Visual Studio 2008 [ZT] Create a Microsoft Access Database Using ADOX and Visual Basic .NET 数的分解,据说是清华的一道复试上机题 为System.Windows.Forms.FontDialog类添加Location属性 f(f(x)) = -x [zt]〖Math〗构造函数使得任意小的区间所对应的值域都是整个实数域 [zt]鸟巢、水立方:同一个地方,同一梦想 〖Math〗据传,任意锐角等于0° 爱因斯坦的超级问题(谁养鱼)SQL解法 “槑囧圐圙”您认得这些汉字吗? 根据URL提取页面的Title,根据网页的charset自动判断Encoding MSDN“MidpointRounding 枚举”中文翻译有误 C# 2.0 新特性(泛型、可空类型)应用一例 Google速记 Google中国编程挑战赛第一轮 Google中国编程挑战赛资格赛 数学家 数据库访问模块
表达式计算器
空军 · 2007-08-21 · via 博客园 - 空军

一个表达式计算器,用C#写的,利用了VB编译器的强劲性能,可以计算任何合法的VB表达式,可以有一个自变量(x),也可以不要自变量。


  1// Calc.cs - 表达式计算器
  2// 编译方法: csc /t:winexe Calc.cs VBExpression.cs
  3
  4using System;
  5using System.Windows.Forms;
  6using Skyiv.Util;
  7
  8namespace Skyiv
  9{
 10  class Calc : Form
 11  {
 12    TextBox tbxA1, tbxA2, tbxA3;
 13
 14    Calc()
 15    {
 16      Text              = "表达式计算器";
 17      StartPosition     = FormStartPosition.CenterScreen;
 18      Width             = 400;
 19      Height            = 200;
 20
 21      Label lblA1       = new Label();
 22      lblA1.Text        = "表达式(&E)";
 23      lblA1.Parent      = this;
 24      lblA1.Top         = 23;
 25      lblA1.Left        = 10;
 26      lblA1.AutoSize    = true;
 27
 28      tbxA1             = new TextBox();
 29      tbxA1.Parent      = this;
 30      tbxA1.Top         = 20;
 31      tbxA1.Left        = 80;
 32      tbxA1.Width       = 300;
 33      tbxA1.BorderStyle = BorderStyle.FixedSingle;
 34
 35      Label lblA2       = new Label();
 36      lblA2.Text        = "自变量(&X)";
 37      lblA2.Parent      = this;
 38      lblA2.Top         = 48;
 39      lblA2.Left        = 10;
 40      lblA2.AutoSize    = true;
 41
 42      tbxA2             = new TextBox();
 43      tbxA2.Parent      = this;
 44      tbxA2.Top         = 45;
 45      tbxA2.Left        = 80;
 46      tbxA2.Width       = 300;
 47      tbxA2.BorderStyle = BorderStyle.FixedSingle;
 48
 49      Button btnA3      = new Button();
 50      btnA3.Text        = "计算(&C)";
 51      btnA3.Parent      = this;
 52      btnA3.Top         = 70;
 53      btnA3.Left        = 10;
 54      btnA3.Width       = 62;
 55      btnA3.Click      += new EventHandler(Calc_Clicked);
 56
 57      tbxA3             = new TextBox();
 58      tbxA3.Parent      = this;
 59      tbxA3.Top         = 70;
 60      tbxA3.Left        = 80;
 61      tbxA3.Width       = 300;
 62      tbxA3.BorderStyle = BorderStyle.FixedSingle;
 63      tbxA3.ReadOnly    = true;
 64
 65
 66      TextBox tbxA4     = new TextBox();
 67      tbxA4.Text        = @"
 68表达式使用 Visual Baisc 语法,可带一个的自变量(x)
 69可使用 pi、e 等常量,sin、cos、tan、log、sqrt 等函数
 70例子:x * cos(x * pi / sqrt(25 * 6^4)) + log(E^10)";
 71      tbxA4.Parent      = this;
 72      tbxA4.Top         = 95;
 73      tbxA4.Left        = 10;
 74      tbxA4.Width       = 370;
 75      tbxA4.Height      = 65;
 76      tbxA4.BorderStyle = BorderStyle.None;
 77      tbxA4.Multiline   = true;
 78      tbxA4.ReadOnly    = true;
 79    }

 80
 81    void Calc_Clicked(object sender, EventArgs ea)
 82    {
 83      (sender as Control).Enabled = false;
 84      try
 85      {
 86        double x = 0;
 87        if (tbxA2.Text.Trim().Length != 0)
 88        {
 89          try
 90          {
 91            x = double.Parse(tbxA2.Text);
 92          }

 93          catch
 94          {
 95            try
 96            {
 97              x = (new Expression(tbxA2.Text)).Compute(0);
 98            }

 99            catch (Exception ex)
100            {
101              MessageBox.Show(ex.Message, "自变量出错");
102            }

103          }

104        }

105        tbxA3.Text = (new Expression(tbxA1.Text)).Compute(x).ToString();
106      }

107      catch (Exception ex)
108      {
109        MessageBox.Show(ex.Message, "表达式出错");
110      }

111      finally
112      {
113        (sender as Control).Enabled = true;
114      }

115    }

116
117    [STAThread]
118    static void Main(string [] args)
119    {
120      Application.Run(new Calc());
121    }

122  }

123}

124

计算表达式的代码用的是“银河”的代码(VB版):
http://www.cnblogs.com/skyivben/archive/2005/10/31/265861.html

 1// VBExpression.cs - 动态生成数学表达式并计算其值
 2// 表达式使用 Visual Baisc 语法,可带一个的自变量(x)
 3// 可使用 pi、e 等常量,sin、cos、tan、log、sqrt 等函数
 4// 例子:e + sqrt(log(pi ^ e) * x) + sin(x * pi / 180)
 5
 6using System;
 7using System.CodeDom.Compiler;
 8using Microsoft.VisualBasic;
 9using System.Reflection;
10using System.Text;
11using System.Globalization;
12
13namespace Skyiv.Util
14{
15  sealed class Expression
16  {
17    object instance;
18    MethodInfo method;
19
20    public Expression(string expression)
21    {
22      if (expression.ToUpper(CultureInfo.InvariantCulture).IndexOf("RETURN"< 0) expression = "Return " + expression;
23      string className = "Expression";
24      string methodName = "Compute";
25      CompilerParameters p = new CompilerParameters();
26      p.GenerateInMemory = true;
27      CompilerResults cr = new VBCodeProvider().CompileAssemblyFromSource
28      (
29        p,
30        string.Format
31        (
32          @"Option Explicit Off
33          Option Strict Off
34          Imports System, System.Math, Microsoft.VisualBasic
35          NotInheritable Class {0}
36          Public Function {1}(x As Double) As Double
37          {2}
38          End Function
39          End Class",
40          className, methodName, expression
41        )
42      );
43      if(cr.Errors.Count > 0)
44      {
45        string msg = "Expression(\"" + expression + "\"): \n";
46        foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n";
47        throw new Exception(msg);
48      }

49      instance = cr.CompiledAssembly.CreateInstance(className);
50      method = instance.GetType().GetMethod(methodName);
51    }

52
53    public double Compute(double x)
54    {
55      return (double)method.Invoke(instance, new object [] { x });
56    }

57  }

58}

59