65.9K
CodeProject 正在变化。 阅读更多。
Home

数学解析器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.49/5 (24投票s)

2005 年 7 月 29 日

CPOL
viewsIcon

153612

downloadIcon

5220

求解数学方程

引言

这是一个使用正则表达式来求解数学表达式的解析器。

如何使用?

MathParser.Parser p = new MathParser.Parser();
if( p.Evaluate( "-(5-10)^(-1)(3+2(cos(3Pi)+(2+ln(exp(1)))^3))" ) ) 
    Console.WriteLine( p.Result );
else Console.WriteLine( "Error." );

特点

  • 函数
    • abs
    • acos
    • asin
    • atan
    • cos
    • cosh
    • floor
    • ln
    • log
    • 符号
    • sin
    • sinh
    • sqrt
    • tan
    • tanh
  • 运算符:+, -, *, /, ^, !
  • 常量:Pi

选择 RAD、DEG 和 GRAD 模式。

p.Mode = MathParser.Mode.RAD;
p.Mode = MathParser.Mode.DEG;
p.Mode = MathParser.Mode.GRAD;

或者

Parser p = new Parser( MathParser.Mode.DEG );

2(1+3(1e3+1)) = 2*(1+3*(1000+1)) = 2*(1+3(1000+1)) = 2(1+3*(10^3+1)) = 6008

2(1+4(5Pi+2,2e3)(5+2)^3)2*(1+4*(5*Pi+2200)*(5+2)^3) 相同。

已知问题

我在德国操作系统 (WinXP) 上测试了这个解析器。
如果我想将一个 字符串 转换为 双精度浮点数 (例如 "3.123"),小数点必须是 "," (Convert.ToDouble("3.123") 会抛出异常)。
我不确定这在英文版本的 WinXP 上是否有效。

如果小数点存在任何问题,请报告,我会修复它。

Bug 修复

  • 修复了一些指数相关的问题
© . All rights reserved.