本文介绍了表达式1 * 2 + 2 / 3-2 * 3 * 5 + 1/2/3的正确结果应该是 - 27.1666667,但我只能得到 - 27.1666666。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的计算器应用程序代码,我正在使用递归。
Here is my code for a calculator application, I'm using recursion.
public String execute(String screen_text)
{
String[] operand = {};
String res = screen_text;
double tmp=0;
if (screen_text.Contains('+'))
{
operand = screen_text.Split('+');
for (int a = 0; a < operand.Length; a++)
tmp += double.Parse(execute(operand[a]));
res = Convert.ToString(tmp);
return res;
}
else if (screen_text.Contains('-'))
{
operand = screen_text.Split('-');
tmp = double.Parse(execute(operand[0]));
for (int b = 1; b < operand.Length; b++)
tmp -= double.Parse(execute(operand[b]));
res = Convert.ToString(tmp);
return res;
}
else if (screen_text.Contains('x'))
{
operand = screen_text.Split('x');
tmp = double.Parse(execute(operand[0]));
for (int c = 1; c < operand.Length; c++)
tmp *= double.Parse(execute(operand[c]));
res = Convert.ToString(tmp);
return res;
}
else if (screen_text.Contains('/'))
{
operand = screen_text.Split('/');
tmp = double.Parse(execute(operand[0]));
for (int d = 1; d < operand.Length; d++)
tmp /= double.Parse(execute(operand[d]));
res = Convert.ToString(tmp);
return res;
}
else
{
return res;
}
}
我尝试过:
我试图使用Math.Round(res,6)将res小数点向上舍入到6,但它给出了相同的答案。我不知道还能做什么._。
What I have tried:
I've tried to round res decimals up only til 6 using Math.Round(res,6) but it gives the same answer. I don't know what else to do ._.
推荐答案
这篇关于表达式1 * 2 + 2 / 3-2 * 3 * 5 + 1/2/3的正确结果应该是 - 27.1666667,但我只能得到 - 27.1666666。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!