00001 // Simple mathematical formula processing library 00002 // Written by Atsushi Watanabe 00003 // Intelligent Robot Laboratory, University of Tsukuba 00004 // 00005 // Copyright 2011 Atsushi Watanabe, All rights reserved. 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // * Redistributions of source code must retain the above copyright notice, this 00011 // list of conditions and the following disclaimer. 00012 // * Redistributions in binary form must reproduce the above copyright notice, 00013 // this list of conditions and the following disclaimer in the documentation 00014 // and/or other materials provided with the distribution. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR 00017 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00018 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 00019 // EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00020 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 00027 #include <stdio.h> 00028 #include <stdlib.h> 00029 00030 #include <formula-calc.h> 00031 00032 int main(int argc, char *argv[]) 00033 { 00034 struct rpf_t *rpf; 00035 struct rpf_t *rpf2; 00036 double test = 0; 00037 struct variables_t variable[8] = 00038 { 00039 { "TEST", &test }, 00040 { NULL, NULL } 00041 }; 00042 00043 if (argc < 2) 00044 { 00045 printf("Usage: %s <formula>\n", argv[0]); 00046 return 0; 00047 } 00048 00049 if (!formula(argv[1], &rpf, variable)) 00050 { 00051 printf("Invalid formula\n"); 00052 } 00053 else 00054 { 00055 float d; 00056 00057 printf("Given formula: %s\n", argv[1]); 00058 00059 printf("Reverse polish: "); 00060 formula_print(stdout, rpf); 00061 printf("\n"); 00062 00063 rpf2 = formula_optimize(rpf); 00064 printf("Optimized reverse polish: "); 00065 formula_print(stdout, rpf2); 00066 printf("\n"); 00067 00068 d = formula_eval(rpf2); 00069 printf("Result: %f %f\n", d, test); 00070 00071 formula_free(rpf); 00072 formula_free(rpf2); 00073 } 00074 00075 return 1; 00076 }