changing a scanner to find user input variables java

Im making a basic calculator that receives user input mathematical expressions like ADD(DIV(5,b3) , POW(2,4) ) and then tells the user what are the variables that dont have a value (like b3) and then asks for user to type the value .
after this the program will print out the result of the expression
Ive written the shunting yard code but I cant make it to identify variables . what I should do is to follow the usual algorithm but add a part that will consider everything other than function name and numbers as unknown variables .but I dont know how can I do this

this the code for shunting yard and the link is the whole parser class

https://www.dropbox.com/s/6bjlmo3uqafxqyp/parser.rtf?dl=0

private List<String> shuntingYard(String expression) {
    List<String> outputQueue = new ArrayList<String>();
    Stack<String> stack = new Stack<String>();

    Tokenizer tokenizer = new Tokenizer(expression);

    String lastFunction = null;
    String previousToken = null;
    while (tokenizer.hasNext()) {
        String token = tokenizer.next();
        if (isNumber(token)) {
            outputQueue.add(token);
        } else if (variables.containsKey(token)) {
            outputQueue.add(token);
        } else if (functions.containsKey(token.toUpperCase())) {
            stack.push(token);
            lastFunction = token;
        } else if (Character.isLetter(token.charAt(0))) {
            stack.push(token);
        } else if (",".equals(token)) {
            while (!stack.isEmpty() && !"(".equals(stack.peek())) {
                outputQueue.add(stack.pop());
            }
            if (stack.isEmpty()) {
                throw new ExpressionException("Parse error for function "
                        + lastFunction + "");
            }
        } else if (operators.containsKey(token)) {
            Operator o1 = operators.get(token);
            String token2 = stack.isEmpty() ? null : stack.peek();
            while (operators.containsKey(token2)
                    && ((o1.isLeftAssoc() && o1.getPrecedence() <= operators
                            .get(token2).getPrecedence()) || (o1
                            .getPrecedence() < operators.get(token2)
                            .getPrecedence()))) {
                outputQueue.add(stack.pop());
                token2 = stack.isEmpty() ? null : stack.peek();
            }
            stack.push(token);
        } else if ("(".equals(token)) {
            if (previousToken != null) {
                if (isNumber(previousToken)) {
                    throw new ExpressionException("Missing operator at character position " + tokenizer.getPos());
                }
            }
            stack.push(token);
        } else if (")".equals(token)) {
            while (!stack.isEmpty() && !"(".equals(stack.peek())) {
                outputQueue.add(stack.pop());
            }
            if (stack.isEmpty()) {
                throw new RuntimeException("Mismatched parentheses");
            }
            stack.pop();
            if (!stack.isEmpty()
                    && functions.containsKey(stack.peek().toUpperCase())) {
                outputQueue.add(stack.pop());
            }
        }
        previousToken = token;
    }
    while (!stack.isEmpty()) {
        String element = stack.pop();
        if ("(".equals(element) || ")".equals(element)) {
            throw new RuntimeException("Mismatched parentheses");
        }
        if (!operators.containsKey(element)) {
            throw new RuntimeException("Unknown operator or function: "
                    + element);
        }
        outputQueue.add(element);
    }
    return outputQueue;
}


Download changing.a.scanner.to.find.user.input.variables.java.zip
Direct Link


Download


Download changing.a.scanner.to.find.user.input.variables.java.zip
Mediafire


Download


Download changing.a.scanner.to.find.user.input.variables.java.zip
FILESERVE


Download

Both comments and pings are currently closed.

Comments are closed.