Question: can someone make a code for a calculator based on this skeleton please. import java.util.*; public class SimpleCalc { //--------------------------------------- // enum TokenType // //
can someone make a code for a calculator based on this skeleton please.
import java.util.*;
public class SimpleCalc {
//--------------------------------------- // enum TokenType // // to represent possible token types // // WS: white space // LP: left paren // RP: right paren // NUM: number // OP: operation //---------------------------------------
private enum TokenType {WS,LP,RP,NUM,OP};
//-------------- // class Token // // nested class to represent a single token // value: the text assigned to this token, originally one char, but converted to string here // type: the type of this token, must be one of the enum values listed above //--------------
private static class Token {
// instance variables private String value; private TokenType type;
// constructor public Token(String v, TokenType t) { }
// toString() is important for diagnostic output public String toString() { }
// getter or accessor methods for the instance vars or properties // setter methods are not needed public String getValue() { } public TokenType getType() { } }
//------------------------------------------- // private static methods of class SimpleCalc //-------------------------------------------
//---------------------------------------- // method: TokenType getTokenType(char c) // // converts a char from the input string to its token type //----------------------------------------
private static TokenType getTokenType(char c) { }
//---------------------------------------- // method: int getAssoc(Token token) // // determines the mathematical associativity (or precedence) of a token // this method is only used for the arithmetic ops + - * / // the return value is arbitrarily chosen to be 1 for + - and 2 for * / // any value is acceptible so long as relative precedence is implemented // its meaning is undefined for other tokens and returns 0 //----------------------------------------
private static int getAssoc(Token token) { }
//---------------------------------------- // method: ArrayList
private static ArrayList
//---------------------------------------- // method: TokenType getTokenType(char c) // // implements Dijkstra's shunting yard algorithm to convert a sequence of // tokens in infix notation into a sequence of tokens in postfix notation // uses a stack to rearrange operations based on precedence // also eliminates parentheses // uses the getAssoc() method to compare operation's associativity or precedence // // summary of algorithm // // 1. NUM tokens: moved immediately to the output sequence (outtokens); note that NUM // tokens are never placed on the stack // 2. LP tokens: pushed onto stack as a marker // 3. RP tokens: triggers all tokens on the stack to be popped and moved to the output // sequence, down to the LP marker; when finished, LP and RP tokens are discarded // 4. OP tokens: depends on what's currently on top of the stack; if the top of the stack // is an op and it has higher precedence than the current op, then the higher precedence // op is popped from the stack and moved to the output sequence; there may be multiple // such ops at the top of the stack, they are all moved off the stack onto the output // sequence, until the top of the stack is not an op, or an op with lower precedence // than the current op; finally, the current op is pushed onto the stack // 5. When all input tokens are processed, there may be some tokens remaining on the stack, // they are now popped and moved to the output sequence //----------------------------------------
private static ArrayList
//---------------------------------------- // method: Token evalOp(Token Op, Token t1, Token T2) // // used during evaluation to evaluate two numerical tokens and an operation // be careful on the order of operands for non-commutative operations like - // example: a - b (infix) converts to (a b -) post fix // a is pushed onto the stack first, then b is pushed onto the stack next // when the evalOp() method is reached, the top of stack is the 2nd operand // and the next top of stack is the first operand //----------------------------------------
private static Token evalOp(Token op, Token t1, Token t2) { }
//---------------------------------------- // method: String evalPostfix(ArrayList private static String evalPostfix(ArrayList //===================== // public methods // // there are two public static method that can be used to invoke the calculator from main // String evalExpression(String s) and void commandLine() //===================== //------------------- // method: String evalExpression(String s) // // this is the simplest method to use for simple tests // call this method from main, input is an infix notation string // result is the value after it has been parsed, converted to postfix, and evaluated // for diagnostic purposes, print out intermediate values //---------------------- public static String evalExpression(String s) { ArrayList ArrayList return evalPostfix(postfixtokens); } //-------------------- // method: void commandLine() // // instead of calling evalExpression() from main, call commandLine() // this method prints a prompt ">" on the command line // user can then type an infix expression onto the command line and get the result // back to the display // this method also recognizes the input "quit" to quit //--------------------- public static void commandLine() { Scanner in = new Scanner(System.in); while (true) { System.out.print("> "); String line = in.nextLine(); if (line.equals("quit")) { System.out.println("Bye!"); break; } else { System.out.println(evalExpression(line)); } } } } //--------------------- // class SimpleCalcTest // // this class provides the main method to drive the SimpleCalc class // from the main method, your choices are // 1. one call to SimpleCalc.commandLine() // 2. multiple calls to SimpleCalc.evalExpression() // 3. one call to test() and place expressions to test into the inputs[] array //--------------------- class SimpleCalcTest { public static void test() { String[] inputs = { "3*4 + 5", "3 + 4*5", "(1+1)*(5-2)", "(3*4+5)*(3*3 + 4*4)" }; for (int i=0; i public static void main(String[] args) { SimpleCalc.commandLine(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
