可计算代小数的(+-*/数学公式)字符串
2007-05-24 17:20
549 查看
在VS2005.net C#中调试通过....
//只是调用码
//原文出处: http://blog.csdn.net/sunheartlee/archive/2007/02/11/1507384.aspx
//中序转换成后序表达式再计算
// 如:23+56/(102-100)*((36-24)/(8-6))
// 转换成:23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
//以便利用栈的方式都进行计算。
private string CalculateParenthesesExpression(string Expression)
{
ArrayList operatorList = new ArrayList();
string operator1;
string ExpressionString = "";
string operand3;
Expression = Expression.Replace(" ", "");
while (Expression.Length > 0)
{
operand3 = "";
//取数字处理
if (Char.IsNumber(Expression[0]))
{
while (Char.IsNumber(Expression[0]) || Expression[0].ToString() == ".")
{
operand3 += Expression[0].ToString();
Expression = Expression.Substring(1);
if (Expression == "") break;
}
//取小数点处理
ExpressionString += operand3 + "|";
}
//取“C”处理
if (Expression.Length > 0 && Expression[0].ToString() == "(")
{
operatorList.Add("(");
Expression = Expression.Substring(1);
}
//取“)”处理
operand3 = "";
if (Expression.Length > 0 && Expression[0].ToString() == ")")
{
do
{
if (operatorList[operatorList.Count - 1].ToString() != "(")
{
operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
operatorList.RemoveAt(operatorList.Count - 1);
}
else
{
operatorList.RemoveAt(operatorList.Count - 1);
break;
}
} while (true);
ExpressionString += operand3;
Expression = Expression.Substring(1);
}
//取运算符号处理
operand3 = "";
if (Expression.Length > 0 && (Expression[0].ToString() == "*" || Expression[0].ToString() == "/" || Expression[0].ToString() == "+" || Expression[0].ToString() == "-"))
{
operator1 = Expression[0].ToString();
if (operatorList.Count > 0)
{
if (operatorList[operatorList.Count - 1].ToString() == "(" || verifyOperatorPriority(operator1, operatorList[operatorList.Count - 1].ToString()))
{
operatorList.Add(operator1);
}
else
{
operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
operatorList.RemoveAt(operatorList.Count - 1);
operatorList.Add(operator1);
ExpressionString += operand3;
}
}
else
{
operatorList.Add(operator1);
}
Expression = Expression.Substring(1);
}
}
operand3 = "";
while (operatorList.Count != 0)
{
operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
operatorList.RemoveAt(operatorList.Count - 1);
}
ExpressionString += operand3.Substring(0, operand3.Length - 1); ;
return CalculateParenthesesExpressionEx(ExpressionString);
}
// 第二步:把转换成后序表达的式子计算
//23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
private string CalculateParenthesesExpressionEx(string Expression)
{
//定义两个栈
ArrayList operandList = new ArrayList();
float operand1;
float operand2;
string[] operand3;
Expression = Expression.Replace(" ", "");
operand3 = Expression.Split(Convert.ToChar("|"));
for (int i = 0; i < operand3.Length; i++)
{
if (Char.IsNumber(operand3[i], 0))
{
operandList.Add(operand3[i].ToString());
}
else
{
//两个操作数退栈和一个操作符退栈计算
operand2 = (float)Convert.ToDouble(operandList[operandList.Count - 1]);
operandList.RemoveAt(operandList.Count - 1);
operand1 = (float)Convert.ToDouble(operandList[operandList.Count - 1]);
operandList.RemoveAt(operandList.Count - 1);
operandList.Add(calculate(operand1, operand2, operand3[i]).ToString());
}
}
return operandList[0].ToString();
}
//判断两个运算符优先级别
private bool verifyOperatorPriority(string Operator1, string Operator2)
{
if (Operator1 == "*" && Operator2 == "+")
return true;
else if (Operator1 == "*" && Operator2 == "-")
return true;
else if (Operator1 == "/" && Operator2 == "+")
return true;
else if (Operator1 == "/" && Operator2 == "-")
return true;
else
return false;
}
//计算
private float calculate(float operand1, float operand2, string operator2)
{
switch (operator2)
{
case "*":
operand1 *= operand2;
break;
case "/":
operand1 /= operand2;
break;
case "+":
operand1 += operand2;
break;
case "-":
operand1 -= operand2;
break;
default:
break;
}
return operand1;
}
//只是调用码
//原文出处: http://blog.csdn.net/sunheartlee/archive/2007/02/11/1507384.aspx
//中序转换成后序表达式再计算
// 如:23+56/(102-100)*((36-24)/(8-6))
// 转换成:23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
//以便利用栈的方式都进行计算。
private string CalculateParenthesesExpression(string Expression)
{
ArrayList operatorList = new ArrayList();
string operator1;
string ExpressionString = "";
string operand3;
Expression = Expression.Replace(" ", "");
while (Expression.Length > 0)
{
operand3 = "";
//取数字处理
if (Char.IsNumber(Expression[0]))
{
while (Char.IsNumber(Expression[0]) || Expression[0].ToString() == ".")
{
operand3 += Expression[0].ToString();
Expression = Expression.Substring(1);
if (Expression == "") break;
}
//取小数点处理
ExpressionString += operand3 + "|";
}
//取“C”处理
if (Expression.Length > 0 && Expression[0].ToString() == "(")
{
operatorList.Add("(");
Expression = Expression.Substring(1);
}
//取“)”处理
operand3 = "";
if (Expression.Length > 0 && Expression[0].ToString() == ")")
{
do
{
if (operatorList[operatorList.Count - 1].ToString() != "(")
{
operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
operatorList.RemoveAt(operatorList.Count - 1);
}
else
{
operatorList.RemoveAt(operatorList.Count - 1);
break;
}
} while (true);
ExpressionString += operand3;
Expression = Expression.Substring(1);
}
//取运算符号处理
operand3 = "";
if (Expression.Length > 0 && (Expression[0].ToString() == "*" || Expression[0].ToString() == "/" || Expression[0].ToString() == "+" || Expression[0].ToString() == "-"))
{
operator1 = Expression[0].ToString();
if (operatorList.Count > 0)
{
if (operatorList[operatorList.Count - 1].ToString() == "(" || verifyOperatorPriority(operator1, operatorList[operatorList.Count - 1].ToString()))
{
operatorList.Add(operator1);
}
else
{
operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
operatorList.RemoveAt(operatorList.Count - 1);
operatorList.Add(operator1);
ExpressionString += operand3;
}
}
else
{
operatorList.Add(operator1);
}
Expression = Expression.Substring(1);
}
}
operand3 = "";
while (operatorList.Count != 0)
{
operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
operatorList.RemoveAt(operatorList.Count - 1);
}
ExpressionString += operand3.Substring(0, operand3.Length - 1); ;
return CalculateParenthesesExpressionEx(ExpressionString);
}
// 第二步:把转换成后序表达的式子计算
//23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
private string CalculateParenthesesExpressionEx(string Expression)
{
//定义两个栈
ArrayList operandList = new ArrayList();
float operand1;
float operand2;
string[] operand3;
Expression = Expression.Replace(" ", "");
operand3 = Expression.Split(Convert.ToChar("|"));
for (int i = 0; i < operand3.Length; i++)
{
if (Char.IsNumber(operand3[i], 0))
{
operandList.Add(operand3[i].ToString());
}
else
{
//两个操作数退栈和一个操作符退栈计算
operand2 = (float)Convert.ToDouble(operandList[operandList.Count - 1]);
operandList.RemoveAt(operandList.Count - 1);
operand1 = (float)Convert.ToDouble(operandList[operandList.Count - 1]);
operandList.RemoveAt(operandList.Count - 1);
operandList.Add(calculate(operand1, operand2, operand3[i]).ToString());
}
}
return operandList[0].ToString();
}
//判断两个运算符优先级别
private bool verifyOperatorPriority(string Operator1, string Operator2)
{
if (Operator1 == "*" && Operator2 == "+")
return true;
else if (Operator1 == "*" && Operator2 == "-")
return true;
else if (Operator1 == "/" && Operator2 == "+")
return true;
else if (Operator1 == "/" && Operator2 == "-")
return true;
else
return false;
}
//计算
private float calculate(float operand1, float operand2, string operator2)
{
switch (operator2)
{
case "*":
operand1 *= operand2;
break;
case "/":
operand1 /= operand2;
break;
case "+":
operand1 += operand2;
break;
case "-":
operand1 -= operand2;
break;
default:
break;
}
return operand1;
}
相关文章推荐
- [转]用Objective-C实现简单的数学字符串公式的计算
- 关于数学字符串公式的计算
- 计算字符串数学公式的结果
- 字符串解析成数学计算公式并计算
- 计算字符串中的简单数学公式
- 字符串数学公式计算类(C#)
- c#字符串中包含的运算符按正常计算 例如按四则运算等,类似公式计算,很好很强大
- [ios]object-c math.h里的数学计算公式介绍
- 转 C# 将字符串转换为计算公式,并计算结果
- Unity常用数学计算公式
- C#后台讲字符串转化为计算公式
- 字符串公式的计算(有兴趣可以研究…
- 数学公式计算 结合 JavaBean
- c#字符串中包含的运算符按正常计算 例如按四则运算等,类似公式计算,很好很强大
- .Net实现表达式计算(公式) 表达式字符串
- C#中字符串转换为计算公式,并进行计算的方法(自定义公式的计算)
- 高中数学排列组合公式/排列组合计算公式
- php中通过eval实现字符串格式的计算公式
- c#字符串中包含的运算符按正常计算 例如按四则运算等,类似公式计算,很好很强大
- math.h里的数学计算公式介绍