是的!感谢上一个问题的帮助!对于指定的字符串方程,它完美地工作!但是现在我遇到两个括号(无论是打开还是关闭)在一起时遇到麻烦
示例:4+(2+(1 + 1))
在我的代码的某处,它被困在一个循环中,但我不知道在哪里!怎么了?我的代码:
import java.util.ArrayList;
public class StringEquation
{
public static void main(String[] args)
{
String s = "2+(8*(7+1))";
ArrayList<String> equation = new ArrayList<>();
String ns = "";
String b;
int nsi = 0;
double n;
double res;
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))
{
if(!"".equals(ns))
{
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(equation.contains("(")||equation.contains(")"))
{
for(int d=0; d<equation.size(); d++)
{
if("*".equals(equation.get(d))||"/".equals(equation.get(d))||"%".equals(equation.get(d)))
{
if("(".equals(equation.get(d-1))||")".equals(equation.get(d-1))||"(".equals(equation.get(d+1))||")".equals(equation.get(d+1)))
{
switch(equation.get(d))
{
case "*": equation.set(d, "TIMES"); break;
case "/": equation.set(d, "DIVBY"); break;
default: equation.set(d, "MOD");
}
}
else
{
switch(equation.get(d))
{
case "*":
n = Double.parseDouble(equation.get(d-1));
n *= Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
break;
case "/":
n = Double.parseDouble(equation.get(d-1));
n /= Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
break;
default:
n = Double.parseDouble(equation.get(d-1));
n %= Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
}
}
}
}
for(int d=0; d<equation.size(); d++)
{
if("+".equals(equation.get(d))||"-".equals(equation.get(d)))
{
if("(".equals(equation.get(d-1))||")".equals(equation.get(d-1))||"(".equals(equation.get(d+1))||")".equals(equation.get(d+1)))
{
switch(equation.get(d))
{
case "+": equation.set(d, "PLUS"); break;
default: equation.set(d, "MINUS");
}
}
else
{
switch(equation.get(d))
{
case "+":
n = Double.parseDouble(equation.get(d-1));
n += Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
break;
default:
n = Double.parseDouble(equation.get(d-1));
n -= Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
}
}
}
}
for(int d=0; d<equation.size(); d++)
{
switch(equation.get(d))
{
case "PLUS": equation.set(d, "+"); break;
case "MINUS": equation.set(d, "-"); break;
case "TIMES": equation.set(d, "*"); break;
case "DIVBY": equation.set(d, "/"); break;
case "MOD": equation.set(d, "%"); break;
}
}
for(int d=0; d<equation.size(); d++)
{
if(d>0)
{
if("(".equals(equation.get(d-1))&&")".equals(equation.get(d+1)))
{
equation.remove(d-1);
equation.remove(d);
}
}
}
}
for(int d=0; d<equation.size(); d++)
{
if("*".equals(equation.get(d))||"/".equals(equation.get(d))||"%".equals(equation.get(d)))
{
switch(equation.get(d))
{
case "*":
n = Double.parseDouble(equation.get(d-1));
n *= Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
break;
case "/":
n = Double.parseDouble(equation.get(d-1));
n /= Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
break;
default:
n = Double.parseDouble(equation.get(d-1));
n %= Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
}
}
}
for(int d=0; d<equation.size(); d++)
{
if("+".equals(equation.get(d))||"-".equals(equation.get(d)))
{
switch(equation.get(d))
{
case "+":
n = Double.parseDouble(equation.get(d-1));
n += Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
break;
default:
n = Double.parseDouble(equation.get(d-1));
n -= Double.parseDouble(equation.get(d+1));
equation.set(d-1, Double.toString(n));
equation.remove(d);
equation.remove(d);
}
}
}
res = Double.parseDouble(equation.get(0));
System.out.println(res);
}
}
答案 0 :(得分:0)
从字符串中删除所有额外的元素说“(”“)”并将数字和符号放在单独的队列中,然后从队列中获取每个元素,同时继续检查天气它是否为数字,如果它是数字将下一个元素作为符号