在文本框输入一个公式,怎么能让程序中的数据按照这个公式计算?
是这样的,公式是每次都会变化的,是人为输入的。
是这样的,公式是每次都会变化的,是人为输入的。
解决方案 »
- [color=#FF0000]100分 想拿分的进来回答问题 [/color]
- 有没有人知道DotNetBar8.3中Schedule的CalendarView如何进行数据绑定?
- 请问C#中如何用Read()读取一个整数,帮忙看一下这个小程序!
- IEnumerable枚举之说??我不太理解,求教!!
- 谁有物业管理系统的需求分析及相关文档?
- 请问如何更新接收官网数据
- Math.Round()能取整数么?
- 怎么把#343434这样的色标转换成c#的?
- 如何把字符串首尾的空格去掉?有例子说明,在线等,谢谢.
- 谁有项目做啊?
- [攒分贴]用c#读取并分析sql2005日志
- GridView1_RowUpdating问题
// 表达式使用 C# 语法,可带一个的自变量(x)。
// 表达式的自变量和值均为(double)类型。
// 使用举例:
// Expression expression = new Expression("Math.Sin(x)");
// Console.WriteLine(expression.Compute(Math.PI / 2));
// expression = new Expression("double u = Math.PI - x;" +
// "double pi2 = Math.PI * Math.PI;" +
// "return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;");
// Console.WriteLine(expression.Compute(0));
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Text;
namespace Skyiv.Util
{
sealed class Expression
{
object instance;
MethodInfo method;
public Expression(string expression)
{
if (expression.IndexOf("return") < 0) expression = "return " + expression + ";";
string className = "Expression";
string methodName = "Compute";
CompilerParameters p = new CompilerParameters();
p.GenerateInMemory = true;
CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, string.
Format("using System;sealed class {0}{{public double {1}(double x){{{2}}}}}",
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 });
}
}
} // ExpressionTest.cs - 动态生成数学表达式并计算其值的测试程序
// 编译方法: csc ExpressionTest.cs Expression.cs
using System;
using Skyiv.Util;
namespace Skyiv.Test
{
class ExpressionTest
{
static void Main(string [] args)
{
try
{
if (args.Length > 0)
{
Console.WriteLine("f(x): {0}", args[0]);
Expression expression = new Expression(args[0]);
for (int i = 1; i < args.Length; i++)
{
double x = double.Parse(args[i]);
Console.WriteLine("f({0}) = {1}", x, expression.Compute(x));
}
}
else Console.WriteLine("Usage: ExpressionTest expression [ parameters ]");
}
catch (Exception ex)
{
Console.WriteLine("错误: " + ex.Message);
}
}
}
}
// 表达式使用 Visual Baisc 语法
// 可使用 pi、e 等常量,sin、cos、tan、log、sqrt 等函数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.Replace(Environment.NewLine, " ");
}
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} 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()
{
return (double)method.Invoke(instance, null);
}
}
}// SuperCalc.cs - 超级计算器
// 编译方法: csc /t:winexe SuperCalc.cs VBExpression.csusing System;
using System.Windows.Forms;
using Skyiv.Util;namespace Skyiv
{
class Calc : Form
{
TextBox tbxA1;
TextBox tbxA3; Calc()
{
Text = "Super Calculator";
StartPosition = FormStartPosition.CenterScreen;
Width = 300;
Height = 300; tbxA1 = new TextBox();
tbxA1.Parent = this;
tbxA1.Multiline = true;
tbxA1.WordWrap = false;
tbxA1.Dock = DockStyle.Fill;
tbxA1.BorderStyle = BorderStyle.FixedSingle; Panel pnlA1 = new Panel();
pnlA1.Parent = this;
pnlA1.Height = 22;
pnlA1.Dock = DockStyle.Top; tbxA3 = new TextBox();
tbxA3.Parent = pnlA1;
tbxA3.Dock = DockStyle.Fill;
tbxA3.BorderStyle = BorderStyle.FixedSingle;
tbxA3.ReadOnly = true; Button btnA3 = new Button();
btnA3.Text = "&Calculate";
btnA3.Parent = pnlA1;
btnA3.Width = 80;
btnA3.Dock = DockStyle.Left;
btnA3.Click += new EventHandler(Calc_Clicked);
} void Calc_Clicked(object sender, EventArgs ea)
{
(sender as Control).Enabled = false;
try
{
tbxA3.Text = (new Expression(tbxA1.Text)).Compute().ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
(sender as Control).Enabled = true;
}
} [STAThread]
static void Main(string [] args)
{
Application.Run(new Calc());
}
}
}