Question: Infix to Postfix calculator in Java. Source code given: ESTester.java import java.util.Scanner; public class ESTester { public static void main(String[] args) { Scanner input =

Infix to Postfix calculator in Java.

Infix to Postfix calculator in Java. Source code given: ESTester.java import java.util.Scanner;

public class ESTester { public static void main(String[] args) { Scanner input

= new Scanner(System.in); ExpressionScanner es = new ExpressionScanner(input); while(es.hasNext()) { Token t

Source code given:

ESTester.java
import java.util.Scanner;
public class ESTester {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ExpressionScanner es = new ExpressionScanner(input);
while(es.hasNext())
{
Token t = es.next();
System.out.println(t);
}
}
}
ExpressionScanner.java
import java.util.Scanner;
import java.util.NoSuchElementException;
public class ExpressionScanner
{
private Scanner in;
private Scanner lineScanner;
private boolean doEnd=false;;
private boolean opAllowed=false;
/**
Create an ExpressionScanner with a preconfigured Scanner
@param in Scanner to use for expression
*/
public ExpressionScanner(Scanner in)
{
this.in = in;
}
/**
Create an ExpressionScanner for a single String
@param expStr String to scan for expression
*/
public ExpressionScanner(String expStr)
{
this.in = new Scanner(expStr);
}
/**
@return true if a token will be returned by next()
*/
public boolean hasNext()
{
if (lineScanner == null)
{
if (! in.hasNextLine())
{
if (doEnd == false)
{
doEnd=true;
return true;
}
return false;
}
String line = in.nextLine();
if (line.equals("."))
{
return false;
}
lineScanner = new Scanner(line);
opAllowed = false;
}
if (lineScanner.hasNext())
{
return true;
}
else
{
if (doEnd == false)
{
doEnd=true;
return true;
}
doEnd=false;
if (in.hasNextLine())
{
String line = in.nextLine();
if (line.equals("."))
{
return false;
}
lineScanner = new Scanner(line);
opAllowed = false;
return hasNext();
}
else
{
return false;
}
}
}
/**
@return the next Token
*/
public Token next()
{
String match = null;
int multiplier=1;
if (doEnd)
{
return new Token(Token.Type.END, "");
}
if (opAllowed) // special 'minus' handling
{
// see if there's a negative number (leading space before -, but
// none after, with a digit there... )
match = lineScanner.findInLine("\\G\\s+\\-(?=\\d)");
// skip whitespace
lineScanner.skip("\\s*");
if (match != null)
{
// it's a negative number -- read the rest of it down below
multiplier=-1;
}
else
{
// if a - is there, after the previous test, then it's an operator
match = lineScanner.findInLine("\\G\\-");
if (match != null)
{
switch (match.charAt(0))
{
case '-':
return new Token(Token.Type.OP, match);
default:
throw new RuntimeException("impossible state: " + match);
}
}
}
}
else
{
lineScanner.skip("\\s*");
}
// don't do special check for minuses next time...
opAllowed = true;
// check for delimiters
match = lineScanner.findInLine("\\G[({\\[\\]})]");
if (match != null)
{
switch (match.charAt(0))
{
case '(':
case '{':
case '[':
return new Token(Token.Type.DELIM_OPEN, match);
case ')':
case '}':
case ']':
return new Token(Token.Type.DELIM_CLOSE, match);
default:
throw new RuntimeException("impossible state: "+match);
}
}
// look for number
match = lineScanner.findInLine("\\G-?\\d+\\.?\\d*");
if (match != null)
{
// apply multiplier if necessary
return new Token(multiplier * Double.parseDouble(match));
}
// look for operator
match = lineScanner.findInLine("\\G[+*\\-/=]");
if (match != null)
{
opAllowed = false;
switch (match.charAt(0))
{
case '+':
case '-':
case '*':
case '/':
case '=':
return new Token(Token.Type.OP, match);
default:
throw new RuntimeException("impossible state: "+match);
}
}
// look for variable
match = lineScanner.findInLine("\\G[a-zA-Z]+");
if (match != null)
{
return new Token(Token.Type.VAR, match);
}
match = lineScanner.next();
throw new RuntimeException("expression parse error: '" + match + "'");
}
}
Token.java
/**
A single token from the parsed expression.
*/
public class Token
{
public static enum Type {
/** an operator */
OP,
/** a variable */
VAR,
/** an opening delimiter */
DELIM_OPEN,
/** a closing delimiter */
DELIM_CLOSE,
/** a number */
NUM,
/** expression end */
END
};
private Type type;
private String content;
private double value;
public Token(Type type, String content)
{
this.type = type;
this.content = content.trim();
}
public Token(double value)
{
this.type = Type.NUM;
this.value = value;
}
/**
@return token type
*/
public Type getType()
{
return type;
}
/**
@return raw String token contents for all types other than Type.NUM
*/
public String getText()
{
return content;
}
/**
@return numeric value of token with type Type.NUM
*/
public double getNum()
{
return value;
}
public String toString()
{
String rval="Token[";
if (type == Type.NUM)
{
rval += "NUM: " + value;
}
else if (type == Type.END)
{
rval +="END";
}
else
{
rval += type + ": '" + content + "'";
}
rval += "]";
return rval;
}
}

ExpressionScanner I'm providing a code for dealing with user input in a flexible way.The ExpressionScanner class can be constructed with either a String or a java.util.Scanner. Once instantiated, it tokenizes the input and provides a sequence of tokens containing their type and value. Please look at the documentation for ExpressionTester and Token, as well as the provided ESTester which shows it in action Here's what it looks like when I run ESTester: prompt$ java ESTester 35 (9.0 8.3) Token [NUM: 35.0] Token [OP:''] Token [DELIM_OPEN:('] Token [NUM:9.0] Token [OP:'] Token [NUM: 8.3] Token [DELIM-CLOSE: ,),] Token [END 13 8* Token [NUM: 13.0] Token [NUM: 8.0] Token [OP:'] Token [END ExpressionScanner I'm providing a code for dealing with user input in a flexible way.The ExpressionScanner class can be constructed with either a String or a java.util.Scanner. Once instantiated, it tokenizes the input and provides a sequence of tokens containing their type and value. Please look at the documentation for ExpressionTester and Token, as well as the provided ESTester which shows it in action Here's what it looks like when I run ESTester: prompt$ java ESTester 35 (9.0 8.3) Token [NUM: 35.0] Token [OP:''] Token [DELIM_OPEN:('] Token [NUM:9.0] Token [OP:'] Token [NUM: 8.3] Token [DELIM-CLOSE: ,),] Token [END 13 8* Token [NUM: 13.0] Token [NUM: 8.0] Token [OP:'] Token [END

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!