compare_recursion.java
来自「Java编写后缀表达式计算器, 用于输出生成后缀表达式, 程序包含完整的Docu」· Java 代码 · 共 100 行
JAVA
100 行
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package comparerecursion;import java.io.*;import exception.SyntaxException;import exception.LexException;/** * * @author zouhao */public class Compare_Recursion { /* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * 带递归的版本 * @author zouhao */ int lookahead; int index; String input; boolean end; //BufferedReader keyboard; public Compare_Recursion() throws IOException { //keyboard = new BufferedReader(new InputStreamReader(System.in)); input = "1+2+3+4+5+6+7+8+9e"; index = 0; lookahead = input.charAt(index); end = false; } void expr() throws IOException { term(); rest(); } void rest() throws IOException { if(!end){ try{ if(lookahead == 'e')end = true; else if (lookahead == '+') { match('+'); term(); //System.out.write('+'); rest(); } else if (lookahead == '-') { match('-'); term(); term(); //System.out.write('-'); rest(); } else if(Character.isDigit((char)lookahead)){ throw new SyntaxException(index,"miss operator"); } else throw new LexException(index); } catch (SyntaxException e){} catch (LexException e){} } } void term() throws IOException { try {if (lookahead == '+'||lookahead == '-') { throw new SyntaxException(index,"miss operand"); }else if(Character.isDigit((char)lookahead)) { //System.out.write((char)lookahead); match(lookahead); } else throw new LexException(index); } catch (SyntaxException e){} catch (LexException e){} } void match(int t) throws IOException { if (lookahead == t) { if(index<input.length()-1) { index++; lookahead = input.charAt(index); } else lookahead = '\n'; } else{ } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?