/* ACM North Central Region, 1993-94 Problem B, Identifying Legal Pascal Real constants Ed Karrels, April 1998 */ import ContestSolution; import java.io.*; public class acmreg93b implements ContestSolution { public static void main(String[] args) { acmreg93b soln = new acmreg93b(); soln.do_it(System.in, System.out); } public void do_it(InputStream input, PrintStream output) { DataInputStream data_input = new DataInputStream(input); String line; String value; while (true) { try { line = data_input.readLine(); } catch (IOException e) { output.println("Input read error"); return; } if (line == null || line.equals("*")) return; value = line.trim(); if (is_valid(value)) output.println(value.toString() + " is legal."); else output.println(value.toString() + " is illegal."); } } boolean is_valid(String str) { boolean has_decimal=false, has_exponent=false; Position pos = new Position(); int n; pos.pos = 0; pos.str = str; // start with + or -, but only one, not both if (read_through("-+", pos) > 1) return false; // must have at least one digit in integer part if (read_through("0123456789", pos) == 0) return false; // fractional part n = read_through(".", pos); // one or zero periods if (n > 1) return false; if (n == 1) { has_decimal = true; // must have at least one digit in fractional part, if there // is a fractional part if (read_through("0123456789", pos) == 0) return false; } // exponent -- preceded by exactly one 'e' or 'E' n = read_through("eE", pos); if (n>1) return false; if (n == 1) { has_exponent = true; // allow + or - before exponent if (read_through("+-", pos) > 1) return false; // must have at least one digit in exponent if (read_through("0123456789", pos) == 0) return false; } // check for extra garbage if (pos.pos != str.length()) return false; if (!has_exponent && !has_decimal) return false; return true; } int read_through(String search_set, Position p) { int count = 0; if (p.pos == p.str.length()) return count; while (search_set.indexOf(p.str.charAt(p.pos)) > -1) { count++; p.pos++; if (p.pos == p.str.length()) return count; } return count; } } class Position { String str; int pos; };