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 RPN Calculator
  34.  * - Fills calculator with operations
  35.  * - Parses the given input syntax
  36.  * - passes them to the RPN Calculator
  37.  * - and prints the specified (tho completely non-informative) error messages
  38.  *
  39.  * @version 1.0
  40.  * @author Manuel Mausz (manuel at mausz.at)
  41.  * @author http://manuel.mausz.at/
  42.  */
  43. class Main
  44. {
  45. public static void main(String args[])
  46. {
  47. Rechner calc = new Rechner();
  48. calc.addOperation(new AddOperation());
  49. calc.addOperation(new SubOperation());
  50. calc.addOperation(new MulOperation());
  51. calc.addOperation(new DivOperation());
  52. calc.addOperation(new PowOperation());
  53. calc.addOperation(new InvOperation());
  54. calc.addOperation(new SinOperation());
  55. calc.addOperation(new CosOperation());
  56. // missing in spec?
  57. //calc.addOperation(new FacOperation());
  58.  
  59. /* read syntax */
  60. Scanner scan = new Scanner(System.in);
  61. try
  62. {
  63. while(scan.hasNext())
  64. {
  65. /* pass operand or operator to RPNCalculator */
  66. if (scan.hasNextDouble())
  67. calc.enterNumber(scan.nextDouble());
  68. else if (scan.hasNext())
  69. calc.enterOperation(scan.next());
  70. }
  71.  
  72. /* dump RPN stack */
  73. System.out.println(calc);
  74. }
  75. catch(InvalidParameterException e)
  76. {
  77. System.out.println("?");
  78. }
  79. catch(InvalidOperationException e)
  80. {
  81. System.out.println("FALSCHE EINGABE");
  82. }
  83. scan.close();
  84. }
  85. }
  86.