如何计算算术表达式(String)并返回答案?
您可能想看看java的脚本功能。看来你可以使用这个脚本引擎从Java执行Javascript(或其他脚本语言)。
你会在https://today.java.net/pub/a/today/2006/04/11/scripting-for-java-platform.html找到一些例子。
你在问什么,是让程序转换(并可能解决)数学方程。这当然是可能的,并且有围绕它的工具和肯定的API,但它绝对超越了你自己的Java程序的黑客攻击。
如果你只是想计算给定公式的结果,那么这是可行的。有趣的是,你可以在Google上投一个等式(例如(5 + 3)*(8-4)),它会给你结果。那么,为什么不使用它呢? ;-)
当我们去面试时,他们问这个逻辑类型的问题。我遇到了同样的问题。但由于这类问题,我无法成功面试。后来我在互联网上搜索后发现了我自己的结果。朋友们都希望这样做。
请按照此操作。我会给这充满*。
我将输入方程作为字符串像。
a * b * c + d/m-x。
得到这个方程作为字符串对象。并使用以下功能:#
public void calc() throws IOException
{
String equation,store,get;
StringBuilder sb= new StringBuilder();
DataInputStream dis= new DataInputStream(System.in);
System.out.println("Enter the equation");
equation= dis.readLine();
equation="%"+equation+"%";
byte[] buf= equation.getBytes();
for(int i=0;i<equation.length();i++)
{
if(buf[i]>=97&&buf[i]<=122)
{
System.out.println("Enter the value for "+(char)buf[i]);
get=dis.readLine();
sb.append(get);
}
else
sb.append((char)buf[i]);
}
store= sb.toString();
char[] buf1= new char[25];
for(int i=0;i<store.length();i++)
{
buf1[i]=store.charAt(i);
}
for(int i=0;i<buf1.length;i++)
{
no.append(buf1[i]);
}
System.out.println(no.toString());
int m,n=0;
for(int i=0;i<no.length()-1;i++)
{
if('/'==no.charAt(i))
{
leftCount=rightCount=0;
m=findLeftValue(i-1)/findRightValue(i+1);
no.replace(i-leftCount, i+rightCount+1, String.valueOf(m));
i=0;
}
}
for(int i=0;i<no.length()-1;i++)
{
if('*'==no.charAt(i))
{
leftCount=rightCount=0;
m=findLeftValue(i-1) * findRightValue(i+1);
no.replace(i-leftCount, i+rightCount+1, String.valueOf(m));
i=0;
}
}
for(int i=0;i<no.length()-1;i++)
{
if('+'==no.charAt(i))
{
leftCount=rightCount=0;
m=findLeftValue(i-1) + findRightValue(i+1);
no.replace(i-leftCount, i+rightCount+1, String.valueOf(m));
i=0;
}
}
for(int i=0;i<no.length()-1;i++)
{
if('-'==no.charAt(i))
{
leftCount=rightCount=0;
m=findLeftValue(i-1) - findRightValue(i+1);
no.replace(i-leftCount, i+rightCount+1, String.valueOf(m));
i=0;
}
}
for(int i=0;i<no.length();i++)
{
if('%'==no.charAt(i))
{
no.deleteCharAt(i);
i=0;
}
}
System.out.println(no.toString());
}
public int findLeftValue(int i)
{
StringBuilder sb= new StringBuilder();
int x=0;
while(no.charAt(i)!='*'&&no.charAt(i)!='+'&&no.charAt(i)!='-'&&no.charAt(i)!='/' &&no.charAt(i)!='%')
{
leftCount++;
sb.insert(0, no.charAt(i));
i--;
}
x=Integer.parseInt(sb.toString());
return x;
}
public int findRightValue(int i)
{
StringBuilder sb= new StringBuilder();
int x;
while(no.charAt(i)!='*'&&no.charAt(i)!='+'&&no.charAt(i)!='-'&&no.charAt(i)!='/' &&no.charAt(i)!='%')
{
rightCount++;
sb.append(no.charAt(i));
i++;
}
x=Integer.parseInt(sb.toString());
return x;
}
这里可能是未使用的变量可能是there.please找到并删除它。 我没有对计算设置任何偏好。只是我补充了流量。如果你改变流程,答案将会不同。所以,请记住。你也可以为不同的操作员做同样的事情。 还有一件事是,请在继续功能之前验证方程式。因为它不会检查方程是正确还是错误。(a + * d ++ c)。这是不正确的方程式。 该公式应该是正确的格式。像a + b * c-d/x。
享受..Break采访中,让你的正确的工作..
使用它friends..congralations .. – Noorul 2014-07-26 09:11:52
是possible.I想是不是possiblem – PSR 2013-04-25 11:52:02
我会说这是可能的,但它会涉及编码 – Apurv 2013-04-25 11:54:40
您颇多”需要将该字符串编译为代码。看[这] [1]。 [1]:http://*.com/questions/935175/convert-string-to-code – 2013-04-25 11:55:17