日期:2014-05-17 浏览次数:20732 次
private void button2_Click(object sender, EventArgs e)
{
string expression = CleanString(tbInput.Text);//MessageBox.Show(expression);
if(expression .IndexOf ('(')>=0)//找到括号
{
int i = expression .IndexOf ('(');
int j = expression.IndexOf(')');
string strsub = expression.Substring(i, j - i-1);//取出括号中的内容
double m = CalculateSimple(strsub);//计算括号的内容
expression = expression.Substring(0, i) + m.ToString() + expression.Substring(j + 1);//把计算结果加入剩余解析式字符串
}
double re = CalculateSimple(expression);//对表达式重新计算
tbOutcome.Text = re.ToString();
}
public double CalculateSimple(string ex) //取出括号内容进行计算,包含运算符和数值
{
double end;
string a="",b="",t="";
char[] chEx = ex.ToCharArray();//把字符串转化为字符数组
char f, s='0';
int n = 0;//计数
int w =0;//记录位置
for (int i = 0; i < chEx.GetLength(0); i++)//循环取出字符,判断是否为运算符
{
char c = chEx[i];
if (c == '.' || (c >= 0 && c <= 9)) continue;//如果是数值就重新查找
&