compare_improve.java
来自「Java编写后缀表达式计算器, 用于输出生成后缀表达式, 程序包含完整的Docu」· Java 代码 · 共 93 行
JAVA
93 行
/* * 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_Improve { /* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * 无递归的版本 * @author zouhao */ int lookahead; int index; String input; //BufferedReader keyboard; public Compare_Improve() throws IOException { //keyboard = new BufferedReader(new InputStreamReader(System.in)); input = "1+2+3+4+5+6+7+8+9\n"; index = 0; lookahead = input.charAt(index); } void expr() throws IOException { term(); rest(); } void rest() throws IOException { try{while(lookahead != '\n'){ if (lookahead == '+') { match('+'); term(); //System.out.write('+'); if(lookahead != '\n'){continue;} else {break;} } else if (lookahead == '-') { match('-'); term(); //System.out.write('-'); if(lookahead != '\n'){continue;} else {break;} } 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 + -
显示快捷键?