字符串数学公式计算类(C#)
来源:互联网 发布:php namespace 编辑:程序博客网 时间:2024/05/20 01:45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Com.MyVPSC.BLL
{
/// <summary>
/// 数学公式计算类,Design by Shani
/// </summary>
public class Calc : ICalc
{
private string _token; /*全局变量*/
private string _inputString = string.Empty;
private IList<string> _inputStringList = new List<string>();
private int _index = 0;
public double CalcMath(string input)
{
if (string.IsNullOrEmpty(input)) throw new ArgumentNullException("input", "输入的数学公式字符串不能为空");
this._inputString = input.Trim();
this._inputStringList = new List<string>();
this._index = 0;
double result;
#region 字符串解释
Regex reg = new Regex("\\+|\\-|\\*|/|\\(|\\)|\\[|\\]|\\{|\\}",RegexOptions.Singleline);
string[] strList = reg.Split(this._inputString);
MatchCollection mc = reg.Matches(this._inputString);
if (mc.Count > 0)
{
for (int i = 0; i < strList.Length; i++)
{
if (!string.IsNullOrEmpty(strList[i]))
this._inputStringList.Add(strList[i]);
if (i < mc.Count)
{
Match mat = mc[i];
if (mat.Success)
{
string s = mat.Value;
if (!string.IsNullOrEmpty(s))
this._inputStringList.Add(s);
}
}
}
}
#endregion
this._token = this.GetNext(); /*载入第一个单元*/
result = this.Low(); /*进行计算*/
return result;
}
/*对当前的标志进行匹配*/
private void MatchMarker(string expectedToken)
{
if (this._token == expectedToken)
{
this._token = this.GetNext();/*匹配成功,获取下一个*/
}
else
{
throw new ArgumentException("运算符不匹配","token");
}
}
/*用于计算表达式中的+、-运算*/
private double Low()
{
double result = this.Mid();
while (this._token == "+" || this._token == "-")
if (this._token == "+")
{
this.MatchMarker("+");
result += this.Mid();
}
else if (this._token == "-")
{
this.MatchMarker("-");
result -= this.Mid();
}
return result;
}
/*用于计算表达式中的*、/运算*/
private double Mid()
{
double div;
double result = this.High();
while (this._token == "*" || this._token == "/")
if (this._token == "*")
{
this.MatchMarker("*");
div = this.High();
result *= div;
}
else if (this._token == "/")
{
this.MatchMarker("/");
div = this.High();
if (div == 0) div = 1;
result /= div;
}
return result;
}
/*用于计算表达式中带{}的运算*/
private double High()
{
double result = this.High2();
if (this._token == "{")
{
this.MatchMarker("{");
result = this.Low(); /*递归计算表达式*/
this.MatchMarker("}");
}
return result;
}
/*用于计算表达式中带[]的运算*/
private double High2()
{
double result = this.High3();
if (this._token == "[")
{
this.MatchMarker("[");
result = this.Low(); /*递归计算表达式*/
this.MatchMarker("]");
}
return result;
}
/*用于计算表达式中带()的运算*/
private double High3()
{
double result = this.MathInput();
if (this._token == "(")
{
this.MatchMarker("(");
result = this.Low(); /*递归计算表达式*/
this.MatchMarker(")");
}
return result;
}
/*获取计算数值*/
private double MathInput()
{
double result = 0;
if (double.TryParse(this._token, out result))
{
this._token = this.GetNext();
}
return result;
}
private string GetNext()
{
string ret = string.Empty;
if (this._index < this._inputStringList.Count)
{
ret = this._inputStringList[this._index];
this._index++;
}
return ret;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Com.MyVPSC.BLL
{
/// <summary>
/// 数学公式计算类,Design by Shani
/// </summary>
public class Calc : ICalc
{
private string _token; /*全局变量*/
private string _inputString = string.Empty;
private IList<string> _inputStringList = new List<string>();
private int _index = 0;
public double CalcMath(string input)
{
if (string.IsNullOrEmpty(input)) throw new ArgumentNullException("input", "输入的数学公式字符串不能为空");
this._inputString = input.Trim();
this._inputStringList = new List<string>();
this._index = 0;
double result;
#region 字符串解释
Regex reg = new Regex("\\+|\\-|\\*|/|\\(|\\)|\\[|\\]|\\{|\\}",RegexOptions.Singleline);
string[] strList = reg.Split(this._inputString);
MatchCollection mc = reg.Matches(this._inputString);
if (mc.Count > 0)
{
for (int i = 0; i < strList.Length; i++)
{
if (!string.IsNullOrEmpty(strList[i]))
this._inputStringList.Add(strList[i]);
if (i < mc.Count)
{
Match mat = mc[i];
if (mat.Success)
{
string s = mat.Value;
if (!string.IsNullOrEmpty(s))
this._inputStringList.Add(s);
}
}
}
}
#endregion
this._token = this.GetNext(); /*载入第一个单元*/
result = this.Low(); /*进行计算*/
return result;
}
/*对当前的标志进行匹配*/
private void MatchMarker(string expectedToken)
{
if (this._token == expectedToken)
{
this._token = this.GetNext();/*匹配成功,获取下一个*/
}
else
{
throw new ArgumentException("运算符不匹配","token");
}
}
/*用于计算表达式中的+、-运算*/
private double Low()
{
double result = this.Mid();
while (this._token == "+" || this._token == "-")
if (this._token == "+")
{
this.MatchMarker("+");
result += this.Mid();
}
else if (this._token == "-")
{
this.MatchMarker("-");
result -= this.Mid();
}
return result;
}
/*用于计算表达式中的*、/运算*/
private double Mid()
{
double div;
double result = this.High();
while (this._token == "*" || this._token == "/")
if (this._token == "*")
{
this.MatchMarker("*");
div = this.High();
result *= div;
}
else if (this._token == "/")
{
this.MatchMarker("/");
div = this.High();
if (div == 0) div = 1;
result /= div;
}
return result;
}
/*用于计算表达式中带{}的运算*/
private double High()
{
double result = this.High2();
if (this._token == "{")
{
this.MatchMarker("{");
result = this.Low(); /*递归计算表达式*/
this.MatchMarker("}");
}
return result;
}
/*用于计算表达式中带[]的运算*/
private double High2()
{
double result = this.High3();
if (this._token == "[")
{
this.MatchMarker("[");
result = this.Low(); /*递归计算表达式*/
this.MatchMarker("]");
}
return result;
}
/*用于计算表达式中带()的运算*/
private double High3()
{
double result = this.MathInput();
if (this._token == "(")
{
this.MatchMarker("(");
result = this.Low(); /*递归计算表达式*/
this.MatchMarker(")");
}
return result;
}
/*获取计算数值*/
private double MathInput()
{
double result = 0;
if (double.TryParse(this._token, out result))
{
this._token = this.GetNext();
}
return result;
}
private string GetNext()
{
string ret = string.Empty;
if (this._index < this._inputStringList.Count)
{
ret = this._inputStringList[this._index];
this._index++;
}
return ret;
}
}
}
- 字符串数学公式计算类(C#)
- 计算字符串数学公式的结果
- 计算字符串中的简单数学公式
- 数学公式的计算
- 数学公式计算 结合 JavaBean
- java字符串数学公式运算
- Jep 解析字符串数学公式
- Jep解析字符串数学公式
- java字符串数学公式运算
- 计算物种平均寿命的数学公式
- c实现字符串长度计算
- C字符串相似度计算
- 一个简单的数学公式计算特殊日期
- 涉及的物理、数学公式及计算过程
- HDU-5858 Hard problem(数学公式、计算几何)
- 数学公式
- 数学公式
- 数学公式
- TCP资料汇编
- iphone 游戏 iThunderStrike 即将发布
- 新增v$session_wait_history视图
- 关于“计算机标识不能更改”问题的解决方法
- hadoop common not found
- 字符串数学公式计算类(C#)
- Expand Status bar
- 如何版本化SharePoint2010 工作流
- Using Android monkeyrunner from Eclipse in Windows
- Android的status bar分析
- java.net.UnknownHostException
- question-begin
- ActionContext与ServletActionContext的区别
- android中对服务的应用管理