formula-test.c
Go to the documentation of this file.
1 // Simple mathematical formula processing library
2 // Written by Atsushi Watanabe
3 // Intelligent Robot Laboratory, University of Tsukuba
4 //
5 // Copyright 2011 Atsushi Watanabe, All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 // * Redistributions of source code must retain the above copyright notice, this
11 // list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
17 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 // EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include <formula-calc.h>
31 
32 int main(int argc, char *argv[])
33 {
34  struct rpf_t *rpf;
35  struct rpf_t *rpf2;
36  double test = 0;
37  struct variables_t variable[8] =
38  {
39  { "TEST", &test },
40  { NULL, NULL }
41  };
42 
43  if (argc < 2)
44  {
45  printf("Usage: %s <formula>\n", argv[0]);
46  return 0;
47  }
48 
49  if (!formula(argv[1], &rpf, variable))
50  {
51  printf("Invalid formula\n");
52  }
53  else
54  {
55  float d;
56 
57  printf("Given formula: %s\n", argv[1]);
58 
59  printf("Reverse polish: ");
60  formula_print(stdout, rpf);
61  printf("\n");
62 
63  rpf2 = formula_optimize(rpf);
64  printf("Optimized reverse polish: ");
65  formula_print(stdout, rpf2);
66  printf("\n");
67 
68  d = formula_eval(rpf2);
69  printf("Result: %f %f\n", d, test);
70 
71  formula_free(rpf);
72  formula_free(rpf2);
73  }
74 
75  return 1;
76 }
struct rpf_t * formula_optimize(struct rpf_t *rpf)
double formula_eval(struct rpf_t *rpf)
int formula(const char *expr, struct rpf_t **rpf, const struct variables_t *variable)
void formula_print(FILE *stream, struct rpf_t *rpf)
void formula_free(struct rpf_t *rpf)
int main(int argc, char *argv[])
Definition: formula-test.c:32


yp-spur
Author(s):
autogenerated on Sat May 11 2019 02:08:24