Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

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