我正在尝试实现一个像普通计算器一样工作的计算器,我设法阻止键盘上的许多输入以防止不必要的字符,然后我设法分析文本字段内容并将内容放在arraylist中。数组列表包含运算符,包括括号,也放在数组列表中。现在我希望能够获取括号并为它们执行乘法或执行前面的运算符。我的支架分析器代码如下
private void BracketSolver(int opnBracketPosition, ArrayList Analyzed)
{Boolean _closed=true;
for (int i=opnBracketPosition;i<Analyzed.size();i++)//check all array element
{
if (Analyzed.get(i).equals("("))//if its an open bracket
{
_closed = false;//a bracket opened
if(Analyzed.get(i+1).equals("-")){//if a minus after the bracket, then turn the number after the minus to a negative and delete the minus
Analyzed.set(i+2,String.valueOf((-1)*Double.parseDouble(Analyzed.get(i+2).toString())));
Analyzed.remove(i+1);}
for (int j=i+1;j<Analyzed.size();j++){//check all element after the open bracket
if(Analyzed.get(j).equals("("))
{
BracketSolver(j, Analyzed);i=0;
}//if we meet another open bracket, then restart the function from there
else if(Analyzed.get(j).equals(")"))//but if its a closing
{
_closed = true;//it has been closed
if(i!=0){//make sure is not zero to prevent error of i-1=-1
if((Analyzed.get(i-1).equals("+"))||(Analyzed.get(i-1).equals("-"))
||(Analyzed.get(i-1).equals("x"))||(Analyzed.get(i-1).equals("^"))
||(Analyzed.get(i-1).equals("÷"))||(Analyzed.get(i-1).equals("√"))) {
//if an operator is before the bracket then just delete the brackets and calculate
Analyzed.remove(j);Analyzed.remove(i);//remove the brackets
Calculator_Basic(Analyzed, i, j-1);//solve for all after the open bracket n before the close
}
else{Analyzed.remove(j);Analyzed.set(i,String.valueOf('x'));
//remove the close bracket but change the open bracket to multiply
Calculator_Basic(Analyzed, i+1, j-1);//solve for all after the open bracket n before the close
}
}else{ Analyzed.remove(j);Analyzed.remove(i);//remove the brackets
Calculator_Basic(Analyzed, i, j-1);//solve for all after the open bracket n before the close
}//i=0;//restart
}
}
}
} if(!_closed)//if it has ended and still no close
{throw new IllegalArgumentException("Syntax Error: Bracket Not Closed");
}
}
函数calculator_Basic是另一个函数,它接受一个arraylist和一个开始和结束整数,并计算其间的所有内容。它已经工作正常,但如果你需要它来帮助我,我也可以粘贴它,虽然它非常丰富和粗糙。现在我的问题是这个代码不能像(5)(8)那样执行。我不知道为什么。我知道我的代码很多,可能不太容易理解。 下一组代码是我在将文本字段存储到数组列表中时使用的代码
private static ArrayList<String> AnalyzedContent;
private void Analyzer()//function that analyzes the user input char by char and takes the numbers
{
AnalyzedContent = new ArrayList();//to store everything
char[] operators = {'+','-','x','÷','^','√','(',')'};//array holding all operators
String set = "";//string to hold the numbers
for (int i=0;i<Display.getText().length();i++)//for all chars in display
{//if its an operator
if((Display.getText().charAt(i)==operators[0])||(Display.getText().charAt(i)==operators[1])||(Display.getText().charAt(i)==operators[2])
||(Display.getText().charAt(i)==operators[3])||(Display.getText().charAt(i)==operators[4])||(Display.getText().charAt(i)==operators[5])
||(Display.getText().charAt(i)==operators[6])||(Display.getText().charAt(i)==operators[7]))
{
if (!set.equals(""))AnalyzedContent.add(set);//check if set is not empty then store its content
set = "";//reset the set
for (int j = 0; j<operators.length;j++){//check which operator it is and store
if (Display.getText().charAt(i)==operators[j])
{
if((i==0)&&(j!=6))throw new ArithmeticException("Syntax Error::Cannot Begin with Operator");
//if it is start of the display and operator is not open bracket then throw error
AnalyzedContent.add(operators[j] + "");
}}
//j=operators.length;//leave the loop when something is found
}
else//else if its a number append to set which might already contain numbers not separated by an operator
{
set = set + Display.getText().charAt(i);//append the number to previous
}
}if (!set.equals(""))AnalyzedContent.add(set);//check if set is not empty then store its content
先谢谢任何人 再次,使用||的运算符的我的if语句是非常粗糙,我猜有一种更简洁的方法来做,如果有人也可以帮助我。