不完善,以后修改吧。
public class Calculator
{
private string _expression;
private Stack s;
//构造函数
public Calculator(string expression)
{
this._expression = expression;
s = new Stack();
}
//计算函数
public double Run()
{
string[] functionString = { "max", "min","sum" };
string expression = PostFix();
//"10|20|max|3|5|min|-";
bool flag = true; ;
string[] aryString = expression.Split('|');
foreach (string str in aryString)
{
for (int i = 0; i < functionString.Length; i++)
{
if (str.Equals(functionString[i]))
{
DoOperator(str);
flag = false;
break;
}
else flag = true;
}
if (flag == true)
{
if (IsNumber(str))
{
double d = Convert.ToDouble(str.ToString());
AddOperands(d);
}
else
{
DoOperator(str);
}
}
}
return (double)s.Pop();
}
private bool IsNumber(string str)
{
if (str.Length > 1)
{
return true;
}
else
{
return Char.IsDigit(str[0]);
}
}
private void AddOperands(double val)
{
s.Push(val);
}
private bool Get2Operands(out double left, out double right)
{
try
{
right = (double)s.Pop();
left = (double)s.Pop();
}
catch (InvalidOperationException)
{
right = 0;
left = 0;
return false;
}
return true;
}
private void DoOperator(string op)
{
double left, right;
bool result = Get2Operands(out left, out right);
if (result)
switch (op)
{
case "+": s.Push(left + right); break;
case "-": s.Push(left - right); break;
case "*": s.Push(left * right); break;
case "max": s.Push(max(left, right)); break;
case "min": s.Push(min(left, right)); break;
case "/":
if (right == 0.0)
{
s.Clear();
//Divide by 0!
throw new Expressception("除数不能为零");
}
else
s.Push(left / right);
break;
case "^":
s.Push(Math.Pow(left, right));
break;
}
else
s.Clear();
}
public double max(double left, double right)
{
if (left >= right)
return left;
else
return right;
}
public double min(double left, double right)
{
if (left >= right)
return right;
else
return left;
}
public string PostFix()
{
string str = this._expression + "#";
string tempc;
char[] chars = str.ToCharArray();
Stack ts = new Stack();
ts.Push('#');
string str1 = "";
string tmpStr = "";
bool isNum = false;
string[] tmpfun = {"",""};
string[] functionString = { "max", "min","sum" };
int k = 0;
for(int i = 0;i<chars.Length;i++)
{
string c = chars[i].ToString();
if (Char.IsDigit(c,0))
{
tmpStr += c.ToString();
isNum = true;
}
else if (char.IsLetter(c,0))
{
tmpStr += c.ToString();
if (tmpStr.Equals(functionString[0]) || tmpStr.Equals(functionString[1]) || tmpStr.Equals(functionString[2]))
{
tmpfun[k]= tmpStr;
k++;
tmpStr = "";
}
}
else
{
if (isNum)
{
str1 += tmpStr + "|";
tmpStr = "";
}
isNum = false;
if (c == ")")
{
for (tempc = Convert.ToString(ts.Pop()); tempc != "("; tempc = Convert.ToString(ts.Pop()))
str1 += tempc.ToString() + "|";
}
else
{
for (tempc = Convert.ToString(ts.Pop()); Isp(Convert.ToChar(tempc)) > Icp(Convert.ToChar(c)); tempc = Convert.ToString(ts.Pop()))
str1 += tempc.ToString() + "|";
ts.Push(tempc);
if (c == ",")
{
ts.Push(tmpfun[k-1]);k-- ;
}
else
{ ts.Push(c); }
}
}
}
return str1.Substring(0, str1.Length - 1);
}
private int Isp(char c)
{
int k;
switch (c)
{
case '#': k = -3; break;
case ',': k = 2; break;
case '(': k = 0; break;
case '^': k = 8; break;
case '*':
case '/':
case '%': k = 6; break;
case '+':
case '-': k = 4; break;
case ')': k = 10; break;
default:
//Unknown operator!
throw new Expressception("不知道的操作符!");
}
return k;
}
private int Icp(char c)
{
int k;
switch (c)
{
case '#': k = -3; break;
case ',': k = 1; break;
case '(': k = 10; break;
case '^': k = 7; break;
case '*':
case '/':
case '%': k = 5; break;
case '+':
case '-': k = 3; break;
case ')': k = 0; break;
default:
//Unknown operator!
throw new Expressception("不知道的操作符!");
}
return k;
}
}