GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* Demo program to run expression evaluation.12Copyright 2000-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 either:89* the GNU Lesser General Public License as published by the Free10Software Foundation; either version 3 of the License, or (at your11option) any later version.1213or1415* the GNU General Public License as published by the Free Software16Foundation; either version 2 of the License, or (at your option) any17later version.1819or both in parallel, as here.2021The GNU MP Library is distributed in the hope that it will be useful, but22WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY23or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License24for more details.2526You should have received copies of the GNU General Public License and the27GNU Lesser General Public License along with the GNU MP Library. If not,28see https://www.gnu.org/licenses/. */293031/* Usage: ./run-expr [-z] [-q] [-f] [-p prec] [-b base] expression...3233Evaluate each argument as a simple expression. By default this is in mpz34integers, but -q selects mpq or -f selects mpf. For mpf the float35precision can be set with -p. In all cases the input base can be set36with -b, or the default is "0" meaning decimal with "0x" allowed.3738This is a pretty trivial program, it's just an easy way to experiment39with the evaluation functions. */404142#include <stdio.h>43#include <stdlib.h>4445#include "gmp.h"46#include "expr.h"474849void50run_expr (int type, int base, unsigned long prec, char *str)51{52int outbase = (base == 0 ? 10 : base);53int ret;5455switch (type) {56case 'z':57default:58{59mpz_t res, var_a, var_b;6061mpz_init (res);62mpz_init_set_ui (var_a, 55L);63mpz_init_set_ui (var_b, 99L);6465ret = mpz_expr (res, base, str, var_a, var_b, NULL);66printf ("\"%s\" base %d: ", str, base);67if (ret == MPEXPR_RESULT_OK)68{69printf ("result ");70mpz_out_str (stdout, outbase, res);71printf ("\n");72}73else74printf ("invalid (return code %d)\n", ret);7576mpz_clear (res);77mpz_clear (var_a);78mpz_clear (var_b);79}80break;8182case 'q':83{84mpq_t res, var_a, var_b;8586mpq_init (res);87mpq_init (var_a);88mpq_init (var_b);8990mpq_set_ui (var_a, 55L, 1);91mpq_set_ui (var_b, 99L, 1);9293ret = mpq_expr (res, base, str, var_a, var_b, NULL);94printf ("\"%s\" base %d: ", str, base);95if (ret == MPEXPR_RESULT_OK)96{97printf ("result ");98mpq_out_str (stdout, outbase, res);99printf ("\n");100}101else102printf ("invalid (return code %d)\n", ret);103104mpq_clear (res);105mpq_clear (var_a);106mpq_clear (var_b);107}108break;109110case 'f':111{112mpf_t res, var_a, var_b;113114mpf_init2 (res, prec);115mpf_init_set_ui (var_a, 55L);116mpf_init_set_ui (var_b, 99L);117118ret = mpf_expr (res, base, str, var_a, var_b, NULL);119printf ("\"%s\" base %d: ", str, base);120if (ret == MPEXPR_RESULT_OK)121{122printf ("result ");123mpf_out_str (stdout, outbase, (size_t) 0, res);124printf ("\n");125}126else127printf ("invalid (return code %d)\n", ret);128129mpf_clear (res);130mpf_clear (var_a);131mpf_clear (var_b);132}133break;134}135}136137int138main (int argc, char *argv[])139{140int type = 'z';141int base = 0;142unsigned long prec = 64;143int seen_expr = 0;144int opt;145char *arg;146147for (;;)148{149argv++;150arg = argv[0];151if (arg == NULL)152break;153154if (arg[0] == '-')155{156for (;;)157{158arg++;159opt = arg[0];160161switch (opt) {162case '\0':163goto end_opt;164165case 'f':166case 'q':167case 'z':168type = opt;169break;170171case 'b':172arg++;173if (arg[0] == '\0')174{175argv++;176arg = argv[0];177if (arg == NULL)178{179need_arg:180fprintf (stderr, "Need argument for -%c\n", opt);181exit (1);182}183}184base = atoi (arg);185goto end_opt;186187case 'p':188arg++;189if (arg[0] == '\0')190{191argv++;192arg = argv[0];193if (arg == NULL)194goto need_arg;195}196prec = atoi (arg);197goto end_opt;198199case '-':200arg++;201if (arg[0] != '\0')202{203/* no "--foo" options */204fprintf (stderr, "Unrecognised option --%s\n", arg);205exit (1);206}207/* stop option interpretation at "--" */208for (;;)209{210argv++;211arg = argv[0];212if (arg == NULL)213goto done;214run_expr (type, base, prec, arg);215seen_expr = 1;216}217218default:219fprintf (stderr, "Unrecognised option -%c\n", opt);220exit (1);221}222}223end_opt:224;225}226else227{228run_expr (type, base, prec, arg);229seen_expr = 1;230}231}232233done:234if (! seen_expr)235{236printf ("Usage: %s [-z] [-q] [-f] [-p prec] [-b base] expression...\n", argv[0]);237exit (1);238}239240return 0;241}242243244