⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scanner.java

📁 Java编写的表达式计算器, 即可以像我们书写表达式那样直接输入计算表达式, 程序自动进行运算, 支持加减乘除幂运算以及判断表达式如A?B C, 程序包含完整的Document和测试运行环境
💻 JAVA
字号:
package parser;

import exceptions.*;
/**
 * 整个程序的scanner,用于扫描输入流并将其作为token流返回
 * 期间是由parser调用getNextToken方法前进,并实现了空格的过滤
 * @author zouhao
 */
public class Scanner {
	String input;
	int index;
        /**
         * 构造函数
         * @param temp 初始化对象的输入流
         */
	public Scanner(String temp)
	{
		input = temp;
		index = 0;
		input.toLowerCase();
	}
        /**
         * 判断输入是否已经扫描完毕
         * @return 结果
         */
	public boolean isEnd()
	{
		if(index == input.length())return true;
		else 
			return false;
	}
        /**
         * 由于做reduce操作的时候已经进行了getNextToken而实际上没有shift返回的token
         * 所以需要回退一个token
         */
        public void forward()
        {
            index--;
        }
        /**
         * 整个scanner的核心方法,用于在输入流扫描token,其中包含语法的异常返回
         * @return 获得的token
         * @throws exceptions.IllegalIdentifierException 未定义符号异常
         * @throws exceptions.IllegalDecimalException 数值类型异常
         * @throws exceptions.IllegalSymbolException 非法字符异常
         * @throws exceptions.EmptyExpressionException 空输入串异常
         */
	public Token getNextToken() throws
                IllegalIdentifierException,
                IllegalDecimalException,
                IllegalSymbolException,
                EmptyExpressionException
	{
            if(!isEnd()){
		Fraction fraction = null;
		Exponent exponent = null;
		Integral integral = null;
		String o, tin = "";
		o = input.substring(index, index+1);
		while(o.equals(" ")){
			index++;
                        if(!isEnd())
                            o = input.substring(index, index+1);
                        else throw new EmptyExpressionException();
			}
		
                    if(Character.isDigit(o.charAt(0)))
                    {
                            tin = input.substring(index);
                            integral = new Integral(tin,o);
                            index += integral.getL();
                            if(!isEnd())
                                o = input.substring(index, index+1);
                            if(o.equals("."))
                            {
                                    index++;
                                    if(!isEnd())
                                        o = input.substring(index, index+1);
                                    if(!Character.isDigit(o.charAt(0)))throw new IllegalDecimalException();
                                    tin = input.substring(index);
                                    Integral frac = new Integral(tin,o);
                                    index += frac.getL();
                                    fraction = new Fraction(frac);
                                    if(!isEnd())
                                        o = input.substring(index, index+1);
                            }
                            if(o.equalsIgnoreCase("e"))
                            {
                                    String op = "+";
                                    index++;
                                    if(!isEnd())
                                        o = input.substring(index, index+1);
                                    if(!Character.isDigit(o.charAt(0)))
                                    {
                                            if(o.equals("-"))
                                            {
                                                    op = "-";
                                            }
                                            else if(!o.equals("+"))
                                            {
                                                throw new IllegalDecimalException();
                                            }
                                            index++;
                                            if(!isEnd())
                                                o = input.substring(index, index+1);
                                            if(!Character.isDigit(o.charAt(0))){throw new IllegalDecimalException();}
                                    }
                                    tin = input.substring(index);
                                    Integral expo = new Integral(tin,o);
                                    index += expo.getL();
                                    exponent = new Exponent(expo,op);
                            }
                            if(fraction != null && exponent != null)
                            {
                                    return new DecimalToken(integral,fraction,exponent);
                            }
                            else if(fraction != null && exponent == null)
                            {
                                    return new DecimalToken(integral,fraction);
                            }
                            else if(fraction == null && exponent != null)
                            {
                                return new DecimalToken(integral,exponent);
                            }
                            return new DecimalToken(integral);
                    }
                    if(o.equals("-"))
                    {
                        int tindex = index-1;
                        if(tindex == -1)
                        {
                            index++;
                            return new OperatorToken(o);
                        }
                        while(input.charAt(tindex) == ' '){
			tindex--;
                        if(tindex == -1)
                            return new OperatorToken(o);
			}
                        if(!Character.isDigit(input.charAt(tindex)) && input.charAt(tindex) != ')')
                        {
                            index++;
                            return new OperatorToken(o);
                        }
                    }
                    if(o.equals("m")||o.equals("c")||o.equals("s")||o.equals("t")||o.equals("f")||o.equals("T")||
                            o.equals("F")||o.equals("<")||o.equals(">"))
                    {
                        tin = input.substring(index);
                        String temp = tin.substring(0, 3);
                        if(tin.length() >= 3){
                            if(temp.equals("min"))
                                {index+=3; return new FunctionToken("min");}
                            else if(temp.equals("max"))
                                {index+=3; return new FunctionToken("max");}
                            else if(temp.equals("cos"))
                                {index+=3; return new FunctionToken("cos");}
                            else if(temp.equals("sin"))
                                {index+=3; return new FunctionToken("sin");}
                            else if(temp.equals("<="))
                                {index+=3;
                                 OperatorToken temp1 = new OperatorToken("<=");
                                 temp1.setForS("cmp");
                                 return temp1;}
                            else if(temp.equals("<>"))
                                {index+=3;
                                 OperatorToken temp1 = new OperatorToken("<>");
                                 temp1.setForS("cmp");
                                 return temp1;}
                            else if(temp.equals(">="))
                                {index+=3;
                                 OperatorToken temp1 = new OperatorToken(">=");
                                 temp1.setForS("cmp");
                                 return temp1;}
                        }
                        if(tin.length() >= 4)
                        {
                            temp = tin.substring(0, 4);
                            if(temp.equalsIgnoreCase("true"))
                                {index+=4; return new BooleanToken("true");}
                        }
                        if(tin.length() >= 5)
                        {
                            temp = tin.substring(0, 5);
                            if(temp.equalsIgnoreCase("false"))
                                {index+=5; return new BooleanToken("false");}
                        }
                        if(!o.equals("<")&&!o.equals(">"))throw new IllegalIdentifierException();
                    }
                    if(o.equals("+")||o.equals("-")||o.equals("*")||o.equals("/")||o.equals("!")||o.equals(">")||
                            o.equals("<")||o.equals("=")||o.equals("^")||o.equals("&")||o.equals("|")||o.equals("?")||
                            o.equals(":")||o.equals(",")||o.equals("(")||o.equals(")"))
                    {
                        OperatorToken temp = new OperatorToken(o);
                        if(o.equals("+")||o.equals("-"))temp.setForS("+/-");
                        if(o.equals("*")||o.equals("/"))temp.setForS("m/d");
                        if(o.equals(">")||o.equals("<")||o.equals("="))temp.setForS("cmp");
                        index++; return temp;}
                    if(Character.isLetter(o.charAt(0)))
                        throw new IllegalIdentifierException();
                    else throw new IllegalSymbolException();
                }
		return new DollarToken();
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -