我正在尝试编写一个程序,通过使用 Arraylist 将方程分解为单独的部分,以字符串的形式求解任何方程(从简单到复杂)。它首先检查括号,然后进入它可以找到的最深的括号集,并解决里面的操作集,从更强的运算符开始,然后继续。它继续检查直到没有更多的括号,然后它会做剩下的事情,从更强的运算符开始。数组列表中剩余的数字就是答案。但我遇到了麻烦。这是我的代码:
import java.util.ArrayList;
public class StringEquation
{
public static void main(String[] args)
{
String s = "80+5+3*(11%(3*2)-(5+1)+6)-(10+10)+(2*2)*5";
ArrayList<String> equation = new ArrayList<>();
String ns = "";
String b;
int nsi;
int si;
int res = 0;
boolean hasParen = false;
for(int c=0; c<s.length(); c++)
{
b = s.substring(c,c+1);
if("0".equals(b)||"1".equals(b)||"2".equals(b)||"3".equals(b)||"4".equals(b)||"5".equals(b)||"6".equals(b)||"7".equals(b)||"8".equals(b)||"9".equals(b))
{
ns += b;
if(c==s.length()-1)
{
nsi = Integer.parseInt(ns);
equation.add(Integer.toString(nsi));
}
}
else if(("+".equals(b)||"-".equals(b)||"*".equals(b)||"/".equals(b)||"%".equals(b))&&!"".equals(ns))
{
nsi = Integer.parseInt(ns);
equation.add(Integer.toString(nsi));
equation.add(b);
ns = "";
}
else if("(".equals(b))
{
equation.add(b);
}
else if (")".equals(b))
{
nsi = Integer.parseInt(ns);
equation.add(Integer.toString(nsi));
equation.add(b);
ns = "";
}
else if("+".equals(b)||"-".equals(b)||"*".equals(b)||"/".equals(b)||"%".equals(b))
{
equation.add(b);
}
}
while(true) //checks for parentheses
{
for(int d=0; d<equation.size(); d++)
{
if("(".equals(equation.get(d)))
{
hasParen = true;
}
if(hasParen==true)
{
while(!")".equals(equation.get(d)))
{
d++;
}
while(!"(".equals(equation.get(d)))
{
d--;
}
d++;
while(!")".equals(equation.get(d+1)))
{
if("-".equals(equation.get(d))) //checks to see if the String number on the Arraylist is negative
{
equation.remove(d);
si = Integer.parseInt(equation.get(d));
si *= -1;
}
else
{
si = Integer.parseInt(equation.get(d));
}
switch(equation.get(d+1))
{
case "*": si *= Integer.parseInt(equation.get(d+2));
equation.set(d, Integer.toString(si));
equation.remove(d+1);
equation.remove(d+2);
break;
case "/": si /= Integer.parseInt(equation.get(d+2));
equation.set(d, Integer.toString(si));
equation.remove(d+1);
equation.remove(d+2);
break;
case "%": si %= Integer.parseInt(equation.get(d+2));
equation.set(d, Integer.toString(si));
equation.remove(d+1);
equation.remove(d+2);
break;
default: d+=2;
}
}
while(!"(".equals(equation.get(d)))
{
d--;
}
d++;
while(!")".equals(equation.get(d+1)))
{
if("-".equals(equation.get(d))) //checks to see if the String number on the Arraylist is negative
{
equation.remove(d);
si = Integer.parseInt(equation.get(d));
si *= -1;
}
else
{
si = Integer.parseInt(equation.get(d));
}
switch(equation.get(d+1))
{
case "+": si += Integer.parseInt(equation.get(d+2));
equation.set(d, Integer.toString(si));
equation.remove(d+1);
equation.remove(d+2);
break;
case "-": si -= Integer.parseInt(equation.get(d+2));
equation.set(d, Integer.toString(si));
equation.remove(d+1);
equation.remove(d+2);
break;
}
}
if("(".equals(equation.get(d-1))&&")".equals(equation.get(d+1)))
{
equation.remove(d-1);
equation.remove(d+1);
}
hasParen = false;
d = 0;
}
}
break;
}
for(int e=0; e<equation.size(); e+=2) //does all multiplication, division and modulus first
{
if("-".equals(equation.get(e))) //checks to see if the String number on the Arraylist is negative
{
equation.remove(e);
si = Integer.parseInt(equation.get(e));
si *= -1;
}
else
{
si = Integer.parseInt(equation.get(e));
}
switch(equation.get(e+1))
{
case "*": si *= Integer.parseInt(equation.get(e+2));
equation.set(e, Integer.toString(si));
equation.remove(e+1);
equation.remove(e+2);
break;
case "/": si /= Integer.parseInt(equation.get(e+2));
equation.set(e, Integer.toString(si));
equation.remove(e+1);
equation.remove(e+2);
break;
case "%": si %= Integer.parseInt(equation.get(e+2));
equation.set(e, Integer.toString(si));
equation.remove(e+1);
equation.remove(e+2);
break;
default: e+=2;
}
}
for(int f=0; f<equation.size(); f+=2) //does the rest (addition and subtraction)
{
if("-".equals(equation.get(f))) //checks to see if the String number on the Arraylist is negative
{
equation.remove(f);
si = Integer.parseInt(equation.get(f));
si *= -1;
}
else
{
si = Integer.parseInt(equation.get(f));
}
switch(equation.get(f+1))
{
case "+": si += Integer.parseInt(equation.get(f+2));
equation.set(f, Integer.toString(si));
equation.remove(f+1);
equation.remove(f+2);
break;
case "-": si -= Integer.parseInt(equation.get(f+2));
equation.set(f, Integer.toString(si));
equation.remove(f+1);
equation.remove(f+2);
break;
}
}
System.out.print(equation.get(0)); //Arraylist should only have the answer of the string equation by this point.
//The part below was to see if lines 25-59 had obtained the whole equation in seperate parts within the arraylist.
// for(int i=0; i<equation.size(); i++)
// {
// System.out.print(equation.get(i));
// }
// System.out.println("\n");
}
}
当我运行它时,它给了我这个错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "("
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:527)
at stringequationfull.StringEquationFull.main(StringEquationFull.java:87)
Java Result: 1
有什么帮助吗?