110,535
社区成员
发帖
与我相关
我的任务
分享
// VBExpression.cs - 动态生成数学表达式并计算其值
// 表达式使用 Visual Baisc 语法,可带一个的自变量(x)
// 可使用 pi、e 等常量,sin、cos、tan、log、sqrt 等函数
// 例子:e + sqrt(log(pi ^ e) * x) + sin(x * pi / 180)
using System;
using System.CodeDom.Compiler;
using Microsoft.VisualBasic;
using System.Reflection;
using System.Text;
using System.Globalization;
namespace Skyiv.Util
{
sealed class Expression
{
object instance;
MethodInfo method;
public Expression(string expression)
{
if (expression.ToUpper(CultureInfo.InvariantCulture).IndexOf("RETURN") < 0) expression = "Return " + expression;
string className = "Expression";
string methodName = "Compute";
CompilerParameters p = new CompilerParameters();
p.GenerateInMemory = true;
CompilerResults cr = new VBCodeProvider().CompileAssemblyFromSource
(
p,
string.Format
(
@"Option Explicit Off
Option Strict Off
Imports System, System.Math, Microsoft.VisualBasic
NotInheritable Class {0}
Public Function {1}(x As Double) As Double
{2}
End Function
End Class",
className, methodName, expression
)
);
if(cr.Errors.Count > 0)
{
string msg = "Expression(\"" + expression + "\"): \n";
foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n";
throw new Exception(msg);
}
instance = cr.CompiledAssembly.CreateInstance(className);
method = instance.GetType().GetMethod(methodName);
}
public double Compute(double x)
{
return (double)method.Invoke(instance, new object [] { x });
}
}
}
// Calc.cs - 表达式计算器
// 编译方法: csc /t:winexe Calc.cs VBExpression.cs
using System;
using System.Windows.Forms;
using Skyiv.Util;
namespace Skyiv
{
class Calc : Form
{
TextBox tbxA1, tbxA2, tbxA3;
Calc()
{
Text = "表达式计算器";
StartPosition = FormStartPosition.CenterScreen;
Width = 400;
Height = 200;
Label lblA1 = new Label();
lblA1.Text = "表达式(&E)";
lblA1.Parent = this;
lblA1.Top = 23;
lblA1.Left = 10;
lblA1.AutoSize = true;
tbxA1 = new TextBox();
tbxA1.Parent = this;
tbxA1.Top = 20;
tbxA1.Left = 80;
tbxA1.Width = 300;
tbxA1.BorderStyle = BorderStyle.FixedSingle;
Label lblA2 = new Label();
lblA2.Text = "自变量(&X)";
lblA2.Parent = this;
lblA2.Top = 48;
lblA2.Left = 10;
lblA2.AutoSize = true;
tbxA2 = new TextBox();
tbxA2.Parent = this;
tbxA2.Top = 45;
tbxA2.Left = 80;
tbxA2.Width = 300;
tbxA2.BorderStyle = BorderStyle.FixedSingle;
Button btnA3 = new Button();
btnA3.Text = "计算(&C)";
btnA3.Parent = this;
btnA3.Top = 70;
btnA3.Left = 10;
btnA3.Width = 62;
btnA3.Click += new EventHandler(Calc_Clicked);
tbxA3 = new TextBox();
tbxA3.Parent = this;
tbxA3.Top = 70;
tbxA3.Left = 80;
tbxA3.Width = 300;
tbxA3.BorderStyle = BorderStyle.FixedSingle;
tbxA3.ReadOnly = true;
TextBox tbxA4 = new TextBox();
tbxA4.Text = @"
表达式使用 Visual Baisc 语法,可带一个的自变量(x)
可使用 pi、e 等常量,sin、cos、tan、log、sqrt 等函数
例子:x * cos(x * pi / sqrt(25 * 6^4)) + log(E^10)";
tbxA4.Parent = this;
tbxA4.Top = 95;
tbxA4.Left = 10;
tbxA4.Width = 370;
tbxA4.Height = 65;
tbxA4.BorderStyle = BorderStyle.None;
tbxA4.Multiline = true;
tbxA4.ReadOnly = true;
}
void Calc_Clicked(object sender, EventArgs ea)
{
(sender as Control).Enabled = false;
try
{
double x = 0;
if (tbxA2.Text.Trim().Length != 0)
{
try
{
x = double.Parse(tbxA2.Text);
}
catch
{
try
{
x = (new Expression(tbxA2.Text)).Compute(0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "自变量出错");
}
}
}
tbxA3.Text = (new Expression(tbxA1.Text)).Compute(x).ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "表达式出错");
}
finally
{
(sender as Control).Enabled = true;
}
}
[STAThread]
static void Main(string [] args)
{
Application.Run(new Calc());
}
}
}