根据公式计算结果!!!

huyanjiang 2006-05-10 09:24:04
下面是数据库表结构:

应发工资(a1) 应扣工资(a2) 实发工资(a3) 基本工资(p1) 奖金(p2) 补助(p3)
1000 100 900 800 200 100

其中 a1、a2、a3、p1、p2、p3是字段名。
下面有三个公式:
a1 = p1 + p2

a2 = p3

a3 = a1-a2

公式是在输入数据之前,设定好的,然后把公式存在一个字段中。输入数据后
根据这个公式自动计算出应发工资、应扣工资、实发工资是多少。我现在的问题是
不知道怎样通过这个公式计算出结果。请各位高手们帮帮忙!!!解决马上给分。
...全文
503 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
emanlee 2007-01-04
  • 打赏
  • 举报
回复
mark
huyanjiang 2006-05-12
  • 打赏
  • 举报
回复
谢谢大家的帮助,问题已解决。不过我用的是另一个类,觉得这个简单易用。也是在网上找的,贴出来跟大家分享。散分!
http://community.csdn.net/Expert/topic/4610/4610686.xml?temp=.6926996

string str = "(2+8)/2.5-1";
double result;
result = (double) WebApplication1.CalculateExpression.Calculate(str);
Label1.Text = result.ToString();

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;

namespace WebApplication1
{
/// <summary>
/// CalculateExpression 的摘要说明。
/// </summary>
public class CalculateExpression
{
/// <summary>
/// 接受一个string类型的表达式并计算结果,返回一个object对象,静态方法
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static object Calculate(string expression)
{
string className = "Calc";
string methodName = "Run";
expression = expression.Replace("/", "*1.0/");

// 创建编译器实例。
ICodeCompiler complier = (new CSharpCodeProvider().CreateCompiler());

// 设置编译参数。
CompilerParameters paras = new CompilerParameters();
paras.GenerateExecutable = false;
paras.GenerateInMemory = true;

// 创建动态代码。
StringBuilder classSource = new StringBuilder();
classSource.Append("public class " + className + "\n");
classSource.Append("{\n");
classSource.Append(" public object " + methodName + "()\n");
classSource.Append(" {\n");
classSource.Append(" return " + expression + ";\n");
classSource.Append(" }\n");
classSource.Append("}");

// 编译代码。
CompilerResults result = complier.CompileAssemblyFromSource(paras, classSource.ToString());

// 获取编译后的程序集。
Assembly assembly = result.CompiledAssembly;

// 动态调用方法。
object eval = assembly.CreateInstance(className);
MethodInfo method = eval.GetType().GetMethod(methodName);
object reobj = method.Invoke(eval, null);
GC.Collect();
return reobj;
}

}
}
xiaohutushen 2006-05-11
  • 打赏
  • 举报
回复
第一种方法: select "表达式" 发送到数据库中执行就可以了.
第二种方法: 动态编译一个类:
需引用:using System.CodeDom.Compiler;
using System.Reflection;
方法如下
/// <summary>
/// 将字符串转换为表达式
/// </summary>
/// <param name="cCharpCode">一个字符串表达示,如"11.0+23.0/34.0"</param>
/// <returns>返加表达式的值</returns>
public double Eval(string cCharpCode)
{
CSharpCodeProvider provider = new CSharpCodeProvider();

ICodeCompiler compiler = provider.CreateCompiler();

CompilerParameters cp = new CompilerParameters();

cp.ReferencedAssemblies.Add("system.dll");

cp.CompilerOptions = "/t:library";

cp.GenerateInMemory = true;

StringBuilder myCode = new StringBuilder();

myCode.Append("using System;");

myCode.Append("namespace MyEval{");

myCode.Append("class myLib { private double i="+cCharpCode+"; public double myMethod(){return i;}}");

myCode.Append("}");

CompilerResults cr = compiler.CompileAssemblyFromSource(cp,myCode.ToString());

Assembly assembly = cr.CompiledAssembly;

object tmp = assembly.CreateInstance("MyEval.myLib");

Type type = tmp.GetType();

MethodInfo mi = type.GetMethod("myMethod");

double result = Convert.ToDouble(mi.Invoke(tmp,null));

return result;
}
你自己可以试试,这也都是从网上找的
ChengKing 2006-05-11
  • 打赏
  • 举报
回复
计算机类似的表达式("(28-9)*2.3+0.236*52")示例程序:

http://blog.csdn.net/ChengKing/archive/2005/11/06/524162.aspx
中第五个程序.
smile9961 2006-05-11
  • 打赏
  • 举报
回复
引用:Microsoft.Vsa.dll
引用:Microsoft.JScript.dll

public static object EvalExpression(string strExpression)
{
Microsoft.JScript.Vsa.VsaEngine engine =
Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
return Microsoft.JScript.Eval.JScriptEvaluate(strExpression, engine);
}

调用示例:
EvalExpression("(28-9)*2.3+0.236*52");

webwait 2006-05-11
  • 打赏
  • 举报
回复
string str1="a1=p1+p2";
string str2="a2=p3";
string str3="a3=a1-a2";
string str="update 表 set "+str1+","+str2+","+str3; //+" where ....."
feng_cx 2006-05-11
  • 打赏
  • 举报
回复
saucer(思归)

-----------
三级钻石
太牛B了
DennyZhang 2006-05-11
  • 打赏
  • 举报
回复
select 公式字符串 from table
strReturn=replace all 变量 with 值
Evaluator.EvalToDouble(strReturn);
DennyZhang 2006-05-11
  • 打赏
  • 举报
回复
Evaluator.EvalToDouble("(9/6) + 3");

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;

namespace OdeToCode.Utility
{
public class Evaluator
{
public static int EvalToInteger(string statement)
{
string s = EvalToString(statement);
return int.Parse(s.ToString());
}

public static double EvalToDouble(string statement)
{
string s = EvalToString(statement);
return double.Parse(s);
}

public static string EvalToString(string statement)
{
object o = EvalToObject(statement);
return o.ToString();
}

public static object EvalToObject(string statement)
{
return _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluator,
new object[] { statement }
);
}

static Evaluator()
{
ICodeCompiler compiler;
compiler = new JScriptCodeProvider().CreateCompiler();

CompilerParameters parameters;
parameters = new CompilerParameters();
parameters.GenerateInMemory = true;

CompilerResults results;
results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);

Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");

_evaluator = Activator.CreateInstance(_evaluatorType);
}

private static object _evaluator = null;
private static Type _evaluatorType = null;
private static readonly string _jscriptSource =

@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String) : String
{
return eval(expr);
}
}
}";
}
}

卧_槽 2006-05-11
  • 打赏
  • 举报
回复
如果是对数据库操作建议不要在程序里面做,直接些到sql语句里面,简单,高效

如select p1+p2 as a1 from table
或者 select a1=p1+p2 from table
laoluop415 2006-05-10
  • 打赏
  • 举报
回复
思归信誉值怎么会有413,纳闷
run_pig 2006-05-10
  • 打赏
  • 举报
回复
我也有这样的问题,不知道思归兄能不能讲得透彻点,该怎么解决
webwait 2006-05-10
  • 打赏
  • 举报
回复
我正在考虑呢,呵呵
huyanjiang 2006-05-10
  • 打赏
  • 举报
回复
to:webwait(webwei)
能说的具体一点吗?
最好有代码,谢谢!
huyanjiang 2006-05-10
  • 打赏
  • 举报
回复
to:saucer(思归)
谢谢您的帮助,利用JS中的eval方法应该可以实现。
不过还要上机试一下。实现后在跟大家分享。
webwait 2006-05-10
  • 打赏
  • 举报
回复
把公式拆分,然后把+-*/替换成实际的符号,再拼接成sql语句
saucer 2006-05-10
  • 打赏
  • 举报
回复
see

http://www.google.com/search?hl=en&lr=&q=C%23+eval
webwait 2006-05-10
  • 打赏
  • 举报
回复
mark
huyanjiang 2006-05-10
  • 打赏
  • 举报
回复
其实简装点说就是,有这样一个字符串公式 “3+2-3*5/4”
我该怎样计算它的值?
huyanjiang 2006-05-10
  • 打赏
  • 举报
回复
to:saucer(思归)
英文不好,我得要先看一下。
加载更多回复(6)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧