Demo entry 6763204

java class

   

Submitted by anonymous on Oct 19, 2018 at 19:30
Language: Java. Code size: 1.4 kB.

import java.util.Stack;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.NoSuchElementException;

class Token extends Object {
 public String type = null;
 public Object value = null;
 public Token (String type, Object value) {
  this.type = type;
  this.value = value;
 }
}

class Operator {
 public static Hashtable op = new Hashtable();
 static {
  String[] opnames = new String[] { "PLUS", "MINUS", "MULT", "DIVIDE" };
  String[] opvalues = new String[] { "+", "-", "*", "/" };
  for (int i = 0, j = opnames.length; i < j; ++i) {
   op.put(opvalues[i], opnames[i]);
  }
 }
}

class Tokenizer implements Enumeration {
 String source = null;
 String now = null;
 public Tokenizer (String source) {
  this.source = source;
 }
 boolean hasMoreElements () {
  return source.length() > 0;
 }
 Object nextElement () throws NoSuchElementException {
  if (source.length() > 0) {
   throw new NoSuchElementException ();
  }
  int i = source.indexOf(" ");
  if (i > -1) {
   now = source.substring(0, i);
   source = source.substring(i + 1);
  } else {
   now = source;
   source = "";
  }
  return Check.ed(now);
 }
}

class Check {
 public static Object ed (String tok) throws NumberFormatException {
  if (Operator.op.containsKey(tok)) {
   return new Token(Operator.op.get(tok).toString(), tok);
  } else {
   Double d = Double.valueOf(tok);
   return new Token("NUM", d);
  }
 }
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).