run-expr.c revision 1.1.1.2
1/* Demo program to run expression evaluation.
2
3Copyright 2000-2002, 2004 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of either:
9
10  * the GNU Lesser General Public License as published by the Free
11    Software Foundation; either version 3 of the License, or (at your
12    option) any later version.
13
14or
15
16  * the GNU General Public License as published by the Free Software
17    Foundation; either version 2 of the License, or (at your option) any
18    later version.
19
20or both in parallel, as here.
21
22The GNU MP Library is distributed in the hope that it will be useful, but
23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25for more details.
26
27You should have received copies of the GNU General Public License and the
28GNU Lesser General Public License along with the GNU MP Library.  If not,
29see https://www.gnu.org/licenses/.  */
30
31
32/* Usage: ./run-expr [-z] [-q] [-f] [-p prec] [-b base] expression...
33
34   Evaluate each argument as a simple expression.  By default this is in mpz
35   integers, but -q selects mpq or -f selects mpf.  For mpf the float
36   precision can be set with -p.  In all cases the input base can be set
37   with -b, or the default is "0" meaning decimal with "0x" allowed.
38
39   This is a pretty trivial program, it's just an easy way to experiment
40   with the evaluation functions.  */
41
42
43#include <stdio.h>
44#include <stdlib.h>
45
46#include "gmp.h"
47#include "expr.h"
48
49
50void
51run_expr (int type, int base, unsigned long prec, char *str)
52{
53  int  outbase = (base == 0 ? 10 : base);
54  int  ret;
55
56  switch (type) {
57  case 'z':
58  default:
59    {
60      mpz_t  res, var_a, var_b;
61
62      mpz_init (res);
63      mpz_init_set_ui (var_a, 55L);
64      mpz_init_set_ui (var_b, 99L);
65
66      ret = mpz_expr (res, base, str, var_a, var_b, NULL);
67      printf ("\"%s\" base %d: ", str, base);
68      if (ret == MPEXPR_RESULT_OK)
69        {
70          printf ("result ");
71          mpz_out_str (stdout, outbase, res);
72          printf ("\n");
73        }
74      else
75        printf ("invalid (return code %d)\n", ret);
76
77      mpz_clear (res);
78      mpz_clear (var_a);
79      mpz_clear (var_b);
80    }
81    break;
82
83  case 'q':
84    {
85      mpq_t  res, var_a, var_b;
86
87      mpq_init (res);
88      mpq_init (var_a);
89      mpq_init (var_b);
90
91      mpq_set_ui (var_a, 55L, 1);
92      mpq_set_ui (var_b, 99L, 1);
93
94      ret = mpq_expr (res, base, str, var_a, var_b, NULL);
95      printf ("\"%s\" base %d: ", str, base);
96      if (ret == MPEXPR_RESULT_OK)
97        {
98          printf ("result ");
99          mpq_out_str (stdout, outbase, res);
100          printf ("\n");
101        }
102      else
103        printf ("invalid (return code %d)\n", ret);
104
105      mpq_clear (res);
106      mpq_clear (var_a);
107      mpq_clear (var_b);
108    }
109    break;
110
111  case 'f':
112    {
113      mpf_t  res, var_a, var_b;
114
115      mpf_init2 (res, prec);
116      mpf_init_set_ui (var_a, 55L);
117      mpf_init_set_ui (var_b, 99L);
118
119      ret = mpf_expr (res, base, str, var_a, var_b, NULL);
120      printf ("\"%s\" base %d: ", str, base);
121      if (ret == MPEXPR_RESULT_OK)
122        {
123          printf ("result ");
124          mpf_out_str (stdout, outbase, (size_t) 0, res);
125          printf ("\n");
126        }
127      else
128        printf ("invalid (return code %d)\n", ret);
129
130      mpf_clear (res);
131      mpf_clear (var_a);
132      mpf_clear (var_b);
133    }
134    break;
135  }
136}
137
138int
139main (int argc, char *argv[])
140{
141  int            type = 'z';
142  int            base = 0;
143  unsigned long  prec = 64;
144  int            seen_expr = 0;
145  int            opt;
146  char           *arg;
147
148  for (;;)
149    {
150      argv++;
151      arg = argv[0];
152      if (arg == NULL)
153        break;
154
155      if (arg[0] == '-')
156        {
157          for (;;)
158            {
159              arg++;
160              opt = arg[0];
161
162              switch (opt) {
163              case '\0':
164                goto end_opt;
165
166              case 'f':
167              case 'q':
168              case 'z':
169                type = opt;
170                break;
171
172              case 'b':
173                arg++;
174                if (arg[0] == '\0')
175                  {
176                    argv++;
177                    arg = argv[0];
178                    if (arg == NULL)
179                      {
180                      need_arg:
181                        fprintf (stderr, "Need argument for -%c\n", opt);
182                        exit (1);
183                      }
184                  }
185                base = atoi (arg);
186                goto end_opt;
187
188              case 'p':
189                arg++;
190                if (arg[0] == '\0')
191                  {
192                    argv++;
193                    arg = argv[0];
194                    if (arg == NULL)
195                      goto need_arg;
196                  }
197                prec = atoi (arg);
198                goto end_opt;
199
200              case '-':
201                arg++;
202                if (arg[0] != '\0')
203                  {
204                    /* no "--foo" options */
205                    fprintf (stderr, "Unrecognised option --%s\n", arg);
206                    exit (1);
207                  }
208                /* stop option interpretation at "--" */
209                for (;;)
210                  {
211                    argv++;
212                    arg = argv[0];
213                    if (arg == NULL)
214                      goto done;
215                    run_expr (type, base, prec, arg);
216                    seen_expr = 1;
217                  }
218
219              default:
220                fprintf (stderr, "Unrecognised option -%c\n", opt);
221                exit (1);
222              }
223            }
224        end_opt:
225          ;
226        }
227      else
228        {
229          run_expr (type, base, prec, arg);
230          seen_expr = 1;
231        }
232    }
233
234 done:
235  if (! seen_expr)
236    {
237      printf ("Usage: %s [-z] [-q] [-f] [-p prec] [-b base] expression...\n", argv[0]);
238      exit (1);
239    }
240
241  return 0;
242}
243