Update action steps
[slib.git] / sbltool.c
blob7553d083e316fb80d122126ff8cd3d998ab08618
1 /*
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/>.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
25 #include <slib.h>
26 #include <slib/getopt.h>
27 #include <slib/math.h>
28 #include <slib/stack.h>
30 #define INT_LEN 22
31 #define chk_fgets(s, l, f) \
32 if (!fgets((s), (l), (f))) \
33 exit((puts(""), 0))
35 const char *ver = "4.1.0";
37 void usage(void);
39 void eio(void);
41 int ui()
43 int selection = 0;
44 char buffer[INT_LEN];
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/> "
49 "for details.\n");
50 helpme:
51 printf("1: Print program version\n"
52 "2: Prime numbers in a range\n"
53 "3: Primality test\n"
54 "4: Coprime test\n"
55 "5: Greatest common factor\n"
56 "6: Lowest common multiple\n");
57 for (;;)
59 printf("(admin) ");
60 fflush(stdout);
61 chk_fgets(buffer, INT_LEN, stdin);
62 selection = strtod(buffer, NULL);
63 if (selection == 0)
64 /* It is a character */
65 selection = buffer[0];
66 switch (selection)
68 case '\n':
69 case ' ':
70 goto helpme;
71 case 1:
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__);
75 break;
76 case 2:
78 slib_uint num1, num2;
79 printf("Minimum: ");
80 fflush(stdout);
81 chk_fgets(buffer, INT_LEN, stdin);
82 num1 = strtoslib(buffer, NULL, 0);
83 printf("Maximum: ");
84 fflush(stdout);
85 chk_fgets(buffer, INT_LEN, stdin);
86 num2 = strtoslib(buffer, NULL, 0);
87 slib_prtpn(num1, num2);
88 break;
90 case 3:
92 slib_uint num;
93 printf("Which number to test: ");
94 fflush(stdout);
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");
99 else
100 printf("Not a prime number!\n");
101 break;
103 case 4:
105 slib_uint num1, num2;
106 printf("First number: ");
107 fflush(stdout);
108 chk_fgets(buffer, INT_LEN, stdin);
109 num1 = strtoslib(buffer, NULL, 0);
110 printf("Second number: ");
111 fflush(stdout);
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");
116 else
117 printf("They are not coprime!\n");
118 break;
120 case 5:
122 slib_uint num1, num2;
123 printf("First number: ");
124 fflush(stdout);
125 chk_fgets(buffer, INT_LEN, stdin);
126 num1 = strtoslib(buffer, NULL, 0);
127 printf("Second number: ");
128 fflush(stdout);
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));
133 break;
135 case 6:
137 slib_uint num1, num2;
138 printf("First number: ");
139 fflush(stdout);
140 chk_fgets(buffer, INT_LEN, stdin);
141 num1 = strtoslib(buffer, NULL, 0);
142 printf("Second number: ");
143 fflush(stdout);
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));
148 break;
150 case 'q':
151 return 0;
152 case 'h':
153 goto helpme;
154 default:
155 printf("%s: Unknown option\n", buffer);
156 goto helpme;
160 int main(int argc, char *argv[])
162 int c;
163 const char *sopts = ":uhvr:g:l:p:d:c:";
164 if (argc > 1)
166 while ((c = getoptGS(argc, argv, sopts)) != -1)
168 switch (c)
170 case 'u':
171 return ui();
172 case 'h':
173 usage();
174 return 0;
175 case 'v':
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__);
179 break;
180 case 'r':
182 slib_uint n1, n2;
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))
187 printf(" not");
188 puts(" coprime");
189 break;
191 case 'g':
193 slib_uint 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));
198 break;
200 case 'l':
202 slib_uint 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));
207 break;
209 case 'p':
211 slib_uint n1, n2;
212 n1 = strtoslib(optargGS, NULL, 0);
213 n2 = strtoslib(argv[optindGS], NULL, 0);
214 slib_prtpn(n1, n2);
215 break;
217 case 'd':
219 slib_uint n;
220 n = strtoslib(optargGS, NULL, 0);
221 printf("%" PRIslib " is", n);
222 if (!slib_ispn(n))
223 printf(" not");
224 puts(" a prime number");
225 break;
227 case '?':
228 usage();
229 fprintf(stderr, "admin: unknown option -%c\n", optoptGS);
230 return 1;
231 case ':':
232 usage();
233 fprintf(stderr, "admin: option -%c needs an argument\n",
234 optoptGS);
235 return 1;
236 default:;
239 return 0;
241 else
243 return ui();
247 void usage(void)
249 printf("Usage:\n"
250 "\tadmin [-uhv] [-r <n1> <n2>] [-g <n1> <n2>] [-l <n1> <n2>]\n"
251 "\t[-p <n1> <n2>] [-d <num>] [-c <exp>]\n"
252 "Options:\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");
262 void eio(void)
264 fprintf(stderr, "\nHmm... You've entered something I don't know!\n");
265 exit(2);