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.util.Scanner;
  31.  
  32. /**
  33.  * Implements a simple RPN (UPN) Calculator
  34.  * - Parses the given input syntax
  35.  * - passes them to the RPN Calculator
  36.  * - and prints the specified (tho completely non-informative) error messages
  37.  *
  38.  * @version 1.0
  39.  * @author Manuel Mausz (manuel at mausz.at)
  40.  * @author http://manuel.mausz.at/
  41.  */
  42. class Main
  43. {
  44. private static Upn calc = new Upn();
  45.  
  46. /**
  47.   * read operands and operator from first line of src
  48.   * parses RPN syntax; only allows integers as operands
  49.   * prints "?" to stdout if calculations fails
  50.   *
  51.   * @param src input stream
  52.   * @return boolean true on success, false otherwise
  53.   */
  54. private static boolean parse(java.io.InputStream src)
  55. throws Exception
  56. {
  57. Scanner scan;
  58.  
  59. /* get first line only */
  60. scan = new Scanner(src);
  61. if (!scan.hasNextLine())
  62. {
  63. scan.close();
  64. return false;
  65. }
  66. String line1 = scan.nextLine();
  67. scan.close();
  68.  
  69. /* parse first line */
  70. scan = new Scanner(line1);
  71. boolean err = false;
  72. while(!err && scan.hasNext())
  73. {
  74. /* pass operand or operator to UpnCalculator */
  75. if (scan.hasNextInt())
  76. calc.addOperand(scan.nextInt());
  77. else if (scan.hasNext())
  78. {
  79. String op = scan.next();
  80. if (!calc.supportsOperator(op))
  81. err = true;
  82. else
  83. {
  84. /* catch exceptions */
  85. try
  86. {
  87. calc.addOperator(op);
  88. }
  89. catch(Exception e)
  90. {
  91. System.out.println("?");
  92. }
  93. }
  94. }
  95. else
  96. err = true;
  97. }
  98. scan.close();
  99.  
  100. return !err;
  101. }
  102.  
  103. /**
  104.   * main method
  105.   *
  106.   * @param args not used
  107.   */
  108. public static void main(String args[])
  109. {
  110. /* try-catch for the sake of safety */
  111. try
  112. {
  113. /* dump RPN stack of everything was ok */
  114. if (parse(System.in))
  115. System.out.print(calc);
  116. else
  117. System.out.println("FALSCHE EINGABE");
  118. }
  119. catch(Throwable e)
  120. {
  121. System.out.println("FALSCHE EINGABE");
  122. }
  123. }
  124. }
  125.