GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* Demo program to run expression evaluation.12Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56The GNU MP Library is free software; you can redistribute it and/or modify7it under the terms of the GNU Lesser General Public License as published by8the Free Software Foundation; either version 2.1 of the License, or (at your9option) any later version.1011The GNU MP Library is distributed in the hope that it will be useful, but12WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY13or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public14License for more details.1516You should have received a copy of the GNU Lesser General Public License17along with the GNU MP Library; see the file COPYING.LIB. If not, write to18the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,19MA 02110-1301, USA. */202122/* Usage: ./run-expr [-z] [-q] [-f] [-p prec] [-b base] expression...2324Evaluate each argument as a simple expression. By default this is in mpz25integers, but -q selects mpq or -f selects mpf. For mpf the float26precision can be set with -p. In all cases the input base can be set27with -b, or the default is "0" meaning decimal with "0x" allowed.2829This is a pretty trivial program, it's just an easy way to experiment30with the evaluation functions. */313233#include <stdio.h>34#include <stdlib.h>3536#include "gmp.h"37#include "expr.h"383940void41run_expr (int type, int base, unsigned long prec, char *str)42{43int outbase = (base == 0 ? 10 : base);44int ret;4546switch (type) {47case 'z':48default:49{50mpz_t res, var_a, var_b;5152mpz_init (res);53mpz_init_set_ui (var_a, 55L);54mpz_init_set_ui (var_b, 99L);5556ret = mpz_expr (res, base, str, var_a, var_b, NULL);57printf ("\"%s\" base %d: ", str, base);58if (ret == MPEXPR_RESULT_OK)59{60printf ("result ");61mpz_out_str (stdout, outbase, res);62printf ("\n");63}64else65printf ("invalid (return code %d)\n", ret);6667mpz_clear (res);68mpz_clear (var_a);69mpz_clear (var_b);70}71break;7273case 'q':74{75mpq_t res, var_a, var_b;7677mpq_init (res);78mpq_init (var_a);79mpq_init (var_b);8081mpq_set_ui (var_a, 55L, 1);82mpq_set_ui (var_b, 99L, 1);8384ret = mpq_expr (res, base, str, var_a, var_b, NULL);85printf ("\"%s\" base %d: ", str, base);86if (ret == MPEXPR_RESULT_OK)87{88printf ("result ");89mpq_out_str (stdout, outbase, res);90printf ("\n");91}92else93printf ("invalid (return code %d)\n", ret);9495mpq_clear (res);96mpq_clear (var_a);97mpq_clear (var_b);98}99break;100101case 'f':102{103mpf_t res, var_a, var_b;104105mpf_init2 (res, prec);106mpf_init_set_ui (var_a, 55L);107mpf_init_set_ui (var_b, 99L);108109ret = mpf_expr (res, base, str, var_a, var_b, NULL);110printf ("\"%s\" base %d: ", str, base);111if (ret == MPEXPR_RESULT_OK)112{113printf ("result ");114mpf_out_str (stdout, outbase, (size_t) 0, res);115printf ("\n");116}117else118printf ("invalid (return code %d)\n", ret);119120mpf_clear (res);121mpf_clear (var_a);122mpf_clear (var_b);123}124break;125}126}127128int129main (int argc, char *argv[])130{131int type = 'z';132int base = 0;133unsigned long prec = 64;134int seen_expr = 0;135int opt;136char *arg;137138for (;;)139{140argv++;141arg = argv[0];142if (arg == NULL)143break;144145if (arg[0] == '-')146{147for (;;)148{149arg++;150opt = arg[0];151152switch (opt) {153case '\0':154goto end_opt;155156case 'f':157case 'q':158case 'z':159type = opt;160break;161162case 'b':163arg++;164if (arg[0] == '\0')165{166argv++;167arg = argv[0];168if (arg == NULL)169{170need_arg:171fprintf (stderr, "Need argument for -%c\n", opt);172exit (1);173}174}175base = atoi (arg);176goto end_opt;177178case 'p':179arg++;180if (arg[0] == '\0')181{182argv++;183arg = argv[0];184if (arg == NULL)185goto need_arg;186}187prec = atoi (arg);188goto end_opt;189190case '-':191arg++;192if (arg[0] != '\0')193{194/* no "--foo" options */195fprintf (stderr, "Unrecognised option --%s\n", arg);196exit (1);197}198/* stop option interpretation at "--" */199for (;;)200{201argv++;202arg = argv[0];203if (arg == NULL)204goto done;205run_expr (type, base, prec, arg);206seen_expr = 1;207}208209default:210fprintf (stderr, "Unrecognised option -%c\n", opt);211exit (1);212}213}214end_opt:215;216}217else218{219run_expr (type, base, prec, arg);220seen_expr = 1;221}222}223224done:225if (! seen_expr)226{227printf ("Usage: %s [-z] [-q] [-f] [-p prec] [-b base] expression...\n", argv[0]);228exit (1);229}230231return 0;232}233234235