2 * sbltool.c - Executable tool of the slib, preceded by the admin project
4 * Copyright (C) 2016-2020 Zhang Maiyun
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 #include <slib/getopt.h>
27 #include <slib/math.h>
28 #include <slib/stack.h>
31 #define chk_fgets(s, l, f) \
32 if (!fgets((s), (l), (f))) \
35 const char *ver
= "4.1.0";
45 printf("sbltool Copyright (C) 2016-2020 Zhang Maiyun\n"
46 "This program comes with ABSOLUTELY NO WARRANTY.\n"
47 "This is free software, and you are welcome to redistribute it\n"
48 "under certain conditions; visit <https://www.gnu.org/licenses/> "
51 printf("1: Print program version\n"
52 "2: Prime numbers in a range\n"
55 "5: Greatest common factor\n"
56 "6: Lowest common multiple\n");
61 chk_fgets(buffer
, INT_LEN
, stdin
);
62 selection
= strtod(buffer
, NULL
);
64 /* It is a character */
65 selection
= buffer
[0];
72 printf("sbl admin v%s with slib v%d.%d.%d\n", ver
,
73 SBLLIB_VERSION
, SBLLIB_MINOR
, SBLLIB_PATCHLEVEL
);
74 printf("Build %s, %s\n", __DATE__
, __TIME__
);
81 chk_fgets(buffer
, INT_LEN
, stdin
);
82 num1
= strtoslib(buffer
, NULL
, 0);
85 chk_fgets(buffer
, INT_LEN
, stdin
);
86 num2
= strtoslib(buffer
, NULL
, 0);
87 slib_prtpn(num1
, num2
);
93 printf("Which number to test: ");
95 chk_fgets(buffer
, INT_LEN
, stdin
);
96 num
= strtoslib(buffer
, NULL
, 0);
97 if (slib_ispn(num
) == 1)
98 printf("Is a prime number!\n");
100 printf("Not a prime number!\n");
105 slib_uint num1
, num2
;
106 printf("First number: ");
108 chk_fgets(buffer
, INT_LEN
, stdin
);
109 num1
= strtoslib(buffer
, NULL
, 0);
110 printf("Second number: ");
112 chk_fgets(buffer
, INT_LEN
, stdin
);
113 num2
= strtoslib(buffer
, NULL
, 0);
114 if (slib_isrp(num1
, num2
) == 1)
115 printf("They are coprime!\n");
117 printf("They are not coprime!\n");
122 slib_uint num1
, num2
;
123 printf("First number: ");
125 chk_fgets(buffer
, INT_LEN
, stdin
);
126 num1
= strtoslib(buffer
, NULL
, 0);
127 printf("Second number: ");
129 chk_fgets(buffer
, INT_LEN
, stdin
);
130 num2
= strtoslib(buffer
, NULL
, 0);
131 printf("gcf(%" PRIslib
", %" PRIslib
") = %" PRIslib
"\n", num1
,
132 num2
, slib_gcf(num1
, num2
));
137 slib_uint num1
, num2
;
138 printf("First number: ");
140 chk_fgets(buffer
, INT_LEN
, stdin
);
141 num1
= strtoslib(buffer
, NULL
, 0);
142 printf("Second number: ");
144 chk_fgets(buffer
, INT_LEN
, stdin
);
145 num2
= strtoslib(buffer
, NULL
, 0);
146 printf("lcm(%" PRIslib
", %" PRIslib
") = %" PRIslib
"\n", num1
,
147 num2
, slib_lcm(num1
, num2
));
155 printf("%s: Unknown option\n", buffer
);
160 int main(int argc
, char *argv
[])
163 const char *sopts
= ":uhvr:g:l:p:d:c:";
166 while ((c
= getoptGS(argc
, argv
, sopts
)) != -1)
176 printf("sbl admin v%s with slib v%d.%d.%d\n", ver
,
177 SBLLIB_VERSION
, SBLLIB_MINOR
, SBLLIB_PATCHLEVEL
);
178 printf("Build %s, %s\n", __DATE__
, __TIME__
);
183 n1
= strtoslib(optargGS
, NULL
, 0);
184 n2
= strtoslib(argv
[optindGS
], NULL
, 0);
185 printf("%" PRIslib
" and %" PRIslib
" are", n1
, n2
);
186 if (!slib_isrp(n1
, n2
))
194 n1
= strtoslib(optargGS
, NULL
, 0);
195 n2
= strtoslib(argv
[optindGS
], NULL
, 0);
196 printf("gcf(%" PRIslib
", %" PRIslib
") = %" PRIslib
"\n",
197 n1
, n2
, slib_gcf(n1
, n2
));
203 n1
= strtoslib(optargGS
, NULL
, 0);
204 n2
= strtoslib(argv
[optindGS
], NULL
, 0);
205 printf("lcm(%" PRIslib
", %" PRIslib
") = %" PRIslib
"\n",
206 n1
, n2
, slib_lcm(n1
, n2
));
212 n1
= strtoslib(optargGS
, NULL
, 0);
213 n2
= strtoslib(argv
[optindGS
], NULL
, 0);
220 n
= strtoslib(optargGS
, NULL
, 0);
221 printf("%" PRIslib
" is", n
);
224 puts(" a prime number");
229 fprintf(stderr
, "admin: unknown option -%c\n", optoptGS
);
233 fprintf(stderr
, "admin: option -%c needs an argument\n",
250 "\tadmin [-uhv] [-r <n1> <n2>] [-g <n1> <n2>] [-l <n1> <n2>]\n"
251 "\t[-p <n1> <n2>] [-d <num>] [-c <exp>]\n"
253 "\t-u\tInteractive mode, same as no options\n"
254 "\t-h\tShow this page.\n"
255 "\t-v\tShow version.\n"
256 "\t-r\tShow whether n1 and n2 are coprime.\n"
257 "\t-g\tGreatest common factor(divisor) of n1 and n2.\n"
258 "\t-l\tLeast common multiple of n1 and n2.\n"
259 "\t-p\tPrint prime numbers between n1 and n2.\n"
260 "\t-d\tPrimality test.\n");
264 fprintf(stderr
, "\nHmm... You've entered something I don't know!\n");