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