我正在编写中缀到后缀转换的代码。但是我无法找到一种方法来检查 (,),+,- 等运算符。我可以使用 ASCII,但有没有可以使用的方法(如 isLetter() 等)?另外我如何定义运算符优先级?
最佳答案
. I could use ASCII but is there any method (like isLetter() etc) which I could use?
好吧,我可以建议你Character.isDigit('1')
或Character.isLetter('A')
用于检查该字符是数字还是字母,但作为自定义,我将选择 ArrayList
.
ArrayList<Character> alist=new ArrayList<>();
alist.add('+');
alist.add('-');
alist.add('/');
alist.add('%');
char c='A';
if(alist.contains(c))
{
System.out.println("UNREACHABLE");
}
其次,您可以使用运算符优先级
HashMap<Character, Integer> hm=new HashMap<>();
hmap.put('+',1);
使用键和值对来确定其优先级
关于java - 检查 Java 中的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23932316/