十六进制算式字符串如何计算 我等到一个string,是一个十六进制的算式,如何计算如str2 = "((((((((((0x0fffffff&107)&105)|105)^108)^110)|105)^101)^101)&104)^103)";如何计算出结果 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.cnblogs.com/skyivben/archive/2005/10/31/265861.html // Expression.cs - 动态生成数学表达式并计算其值 // 表达式使用 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 { class Demo { static void Main() { string str2 = "((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103)"; double a = (new Expression(str2)).Compute(0); Console.WriteLine(a); } } 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 }); } } } 怎么不一样,计算和验证的结果都是:15// Expression.cs - 动态生成数学表达式并计算其值 // 表达式使用 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 { class Demo { static void Main() { string str2 = "((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103)"; double a = (new Expression(str2)).Compute(0); Console.WriteLine("计算: {0}", a); int b = ((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103); Console.WriteLine("验证: {0}", b); } } /* 程序输出: 计算: 15 验证: 15 */ 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 }); } } } timer 如何释放内存 为什么结构里面可以定义方法,却不能访问结构的方法? 智能提示的sqlserver查询分析器 VS2003中的程序在VS2005中继续,加了一个BUTTON控件,却看不见不知道为什么?也是最上层! 有关xml文件问题 如何使控件能随窗体的大小的改变而自动适应???? 爬虫:html中script标签的解析 Ajax与ViewState DriveInfo 类的属性 有谁懂SAPI的吗?怎样才能读一个单词啊? 100分献给大虾,一个窗体传值的问题搞得我郁闷了一晚上!求助。。 datagridview多行删除不正常
// 表达式使用 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
{
class Demo
{
static void Main()
{
string str2 = "((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103)";
double a = (new Expression(str2)).Compute(0);
Console.WriteLine(a);
}
} 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 });
}
}
}
怎么不一样,计算和验证的结果都是:15// Expression.cs - 动态生成数学表达式并计算其值
// 表达式使用 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
{
class Demo
{
static void Main()
{
string str2 = "((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103)";
double a = (new Expression(str2)).Compute(0);
Console.WriteLine("计算: {0}", a); int b = ((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103);
Console.WriteLine("验证: {0}", b);
}
}
/* 程序输出:
计算: 15
验证: 15
*/ 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 });
}
}
}