Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2008, Manuel Mausz <manuel at mausz.at>
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  * * Redistributions of source code must retain the above copyright
  8.  * notice, this list of conditions and the following disclaimer.
  9.  * * Redistributions in binary form must reproduce the above copyright
  10.  * notice, this list of conditions and the following disclaimer in the
  11.  * documentation and/or other materials provided with the distribution.
  12.  * * Neither the name of the copyright holders nor the
  13.  * names of its contributors may be used to endorse or promote products
  14.  * derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20.  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  23.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  25.  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  27.  * DAMAGE.
  28.  */
  29.  
  30. import java.io.InputStream;
  31. import java.util.HashMap;
  32. import java.util.Scanner;
  33. import java.lang.Class;
  34. import java.lang.Boolean;
  35. import java.lang.reflect.*;
  36. import java.util.InputMismatchException;
  37. import java.lang.NoSuchMethodException;
  38.  
  39.  
  40. /**
  41.  * CalcOperator encapsulate an Operator
  42.  * NOTE: This class isn't public due to EPROG limitations.
  43.  * EPROG doesn't allow shipping files not specified
  44.  *
  45.  * @version 1.0
  46.  * @author Manuel Mausz (manuel at mausz.at)
  47.  * @author http://manuel.mausz.at/
  48.  */
  49. class CalcOperator
  50. {
  51. private String op;
  52.  
  53. /**
  54.   * Constructor
  55.   *
  56.   * @param value init value
  57.   */
  58. CalcOperator(String op)
  59. {
  60. this.op = op;
  61. }
  62.  
  63. /**
  64.   * @return encapsulated operator
  65.   */
  66. public String get()
  67. {
  68. return op;
  69. }
  70.  
  71. /**
  72.   * Indicates whether some other object is "equal to" this one
  73.   *
  74.   * @param obj the reference object with which to compare
  75.   * @return true if this object is the same as the obj argument; false otherwise
  76.   */
  77. public boolean equals(Object op2)
  78. {
  79. if (op2 == null)
  80. return false;
  81. else if (op2 instanceof CalcOperator)
  82. return this.op.equals(((CalcOperator) op2).get());
  83. else
  84. return false;
  85. }
  86.  
  87. /**
  88.   * Returns a hash code value for the object
  89.   *
  90.   * @return string
  91.   */
  92. public int hashCode()
  93. {
  94. return op.hashCode();
  95. }
  96.  
  97. /**
  98.   * Converts instance to string
  99.   *
  100.   * @return string
  101.   */
  102. public String toString()
  103. {
  104. return op;
  105. }
  106. }
  107.  
  108.  
  109. /**
  110.  * Abstract for CalcValue
  111.  *
  112.  * @version 1.0
  113.  * @author Manuel Mausz (manuel at mausz.at)
  114.  * @author http://manuel.mausz.at/
  115.  */
  116. abstract class CalcValue
  117. {
  118. protected static HashMap<CalcOperator, String> operators = new HashMap<CalcOperator, String>();
  119.  
  120. /**
  121.   * Assigns an operator to a method
  122.   *
  123.   * @param op operator
  124.   * @param func method name
  125.   */
  126. protected void addOp(CalcOperator op, String func)
  127. {
  128. operators.put(op, func);
  129. }
  130.  
  131. /**
  132.   * Checks if instance has an method assigned to operater
  133.   *
  134.   * @param op operator
  135.   * @return boolean
  136.   */
  137. public boolean isSupported(CalcOperator op)
  138. {
  139. return operators.containsKey(op);
  140. }
  141.  
  142. /**
  143.   * Invokes the method assigned to operator
  144.   * passes value as argument
  145.   *
  146.   * @param value argument passed to the invoked method
  147.   * @param op operator
  148.   * @return object return by invoked method
  149.   * @throws NoSuchMethodException
  150.   * @throws InvocationTargetException
  151.   */
  152. public Object calc(CalcValue value, CalcOperator op)
  153. throws NoSuchMethodException, InvocationTargetException
  154. {
  155. Object ret = null;
  156.  
  157. String methodname = operators.get(op);
  158. if (methodname == null)
  159. throw new NoSuchMethodException("Method for operator '" + op.get() + "' not found");
  160.  
  161. try
  162. {
  163. Method method = this.getClass().getMethod(methodname, new Class[] { value.getClass() });
  164. ret = method.invoke(this, new Object[] { value } );
  165. }
  166. catch(IllegalAccessException e)
  167. {
  168. throw new NoSuchMethodException(e.getMessage());
  169. }
  170.  
  171. return ret;
  172. }
  173. }
  174.  
  175.  
  176. /**
  177.  * Calculator
  178.  *
  179.  * @version 1.0
  180.  * @author Manuel Mausz (manuel at mausz.at)
  181.  * @author http://manuel.mausz.at/
  182.  */
  183. public class Calculator
  184. {
  185. private static Complex val1;
  186. private static Complex val2;
  187. private static CalcOperator op;
  188.  
  189. /**
  190.   * read values and operator from src
  191.   * only allow syntax: "op real1 img1 real2 img2 ..."
  192.   *
  193.   * @param src input stream
  194.   * @return boolean true on success, false otherwise
  195.   * @throws InputMismatchException
  196.   * @throws NoSuchMethodException
  197.   * @throws InvocationTargetException
  198.   */
  199. public static boolean parse(InputStream src)
  200. throws InputMismatchException, NoSuchMethodException, InvocationTargetException
  201. {
  202. int real, img;
  203.  
  204. /* get first line only */
  205. Scanner scanner = new Scanner(System.in);
  206. if (!scanner.hasNextLine())
  207. return false;
  208. String line1 = scanner.nextLine();
  209. scanner.close();
  210.  
  211. /* parse first line */
  212. scanner = new Scanner(line1);
  213.  
  214. /* scan operator */
  215. if (!scanner.hasNext())
  216. throw new InputMismatchException("Invalid operator");
  217. op = new CalcOperator(scanner.next());
  218.  
  219. /* scan first complex value */
  220. if (!scanner.hasNextInt())
  221. throw new InputMismatchException("Invalid real part. No integer");
  222. real = scanner.nextInt();
  223.  
  224. if (!scanner.hasNextInt())
  225. throw new InputMismatchException("Invalid imaginary param. No integer");
  226. img = scanner.nextInt();
  227.  
  228. val1 = new Complex(real, img);
  229.  
  230. /* check operator. this will also be done implicitly in calc() */
  231. if (!val1.isSupported(op))
  232. throw new NoSuchMethodException("Method for operator '" + op.get() + "' not found");
  233.  
  234. /* scan other complex values */
  235. while(scanner.hasNext())
  236. {
  237. if (!scanner.hasNextInt())
  238. throw new InputMismatchException("Invalid real param. No integer");
  239. real = scanner.nextInt();
  240.  
  241. if (!scanner.hasNextInt())
  242. throw new InputMismatchException("Invalid imaginary param. No integer");
  243. img = scanner.nextInt();
  244.  
  245. val2 = new Complex(real, img);
  246.  
  247. /* do calculation */
  248. val1.calc(val2, op);
  249. }
  250.  
  251. scanner.close();
  252.  
  253. return true;
  254. }
  255.  
  256. /**
  257.   * main method
  258.   *
  259.   * @param args not used
  260.   */
  261. public static void main(String[] args)
  262. {
  263. val1 = new Complex(0, 0);
  264.  
  265. try
  266. {
  267. /* parse stdin */
  268. parse(System.in);
  269.  
  270. /* print result to stdout */
  271. System.out.println(((Complex) val1).toString());
  272. }
  273. catch(Throwable e)
  274. {
  275. System.out.println("FALSCHE EINGABE");
  276. }
  277. }
  278. }
  279.