create changelog entry
[debian/openrocket] / core / src / de / congrace / exp4j / CommandlineInterpreter.java
1 /*
2    Copyright 2011 frank asseg
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15
16  */
17 package de.congrace.exp4j;
18
19 /**
20  * Simple commandline interpreter for mathematical expressions the interpreter
21  * takes a mathematical expressions as a {@link String} argument, evaluates it
22  * and prints out the result.
23  * 
24  * 
25  * <pre>
26  * java de.congrace.exp4j.CommandlineInterpreter "2 * log(2.2223) - ((2-3.221) * 14.232^2)"
27  * > 248.91042049521056
28  * </pre>
29  * 
30  * @author fas@congrace.de
31  * 
32  */
33 public class CommandlineInterpreter {
34         private static void calculateExpression(String string) {
35                 try {
36                         final PostfixExpression pe = PostfixExpression.fromInfix(string);
37                         System.out.println(pe.calculate());
38                 } catch (UnparsableExpressionException e) {
39                         e.printStackTrace();
40                 } catch (UnknownFunctionException e) {
41                         e.printStackTrace();
42                 }
43         }
44
45         public static void main(String[] args) {
46                 if (args.length != 1) {
47                         printUsage();
48                 } else {
49                         calculateExpression(args[0]);
50                 }
51         }
52
53         private static void printUsage() {
54                 final StringBuilder usage = new StringBuilder();
55                 usage.append("Commandline Expression Parser\n\n").append("Example: ").append("\n").append("java -jar exp4j.jar \"2.12 * log(23) * (12 - 4)\"\n\n")
56                                 .append("written by fas@congrace.de");
57                 System.err.println(usage.toString());
58         }
59 }