commit a049db580776ed41bb4583bfb331d97c5a80900a
Author: Mattias Andrée <maandree_AT_kth.se>
AuthorDate: Sun Jun 19 02:50:31 2016 +0200
Commit: Mattias Andrée <maandree_AT_kth.se>
CommitDate: Sun Jun 19 02:50:31 2016 +0200
Add examples: sum, prod, avg, median
Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
diff --git a/examples/01-sum.c b/examples/01-sum.c
new file mode 100644
index 0000000..45463ed
--- /dev/null
+++ b/examples/01-sum.c
_AT_@ -0,0 +1,36 @@
+/* Calculates the sum of $_AT_ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <zahl.h>
+
+int
+main(int argc, char *argv[])
+{
+ z_t sum, term;
+ jmp_buf env;
+ char *buf;
+ int i;
+
+ if (setjmp(env))
+ return zperror(argv[0]), 1;
+
+ zsetup(env);
+ zinit(sum);
+ zinit(term);
+ zsetu(sum, 0);
+
+ for (i = 1; i < argc; i++) {
+ zsets(term, argv[i]);
+ zadd(sum, sum, term);
+ }
+
+ printf("%s\n", buf = zstr(sum, NULL, 0));
+ free(buf);
+
+ zfree(term);
+ zfree(sum);
+ zunsetup();
+ return 0;
+}
diff --git a/examples/02-prod.c b/examples/02-prod.c
new file mode 100644
index 0000000..8b7af40
--- /dev/null
+++ b/examples/02-prod.c
_AT_@ -0,0 +1,36 @@
+/* Calculates the product of $_AT_ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <zahl.h>
+
+int
+main(int argc, char *argv[])
+{
+ z_t prod, factor;
+ jmp_buf env;
+ char *buf;
+ int i;
+
+ if (setjmp(env))
+ return zperror(argv[0]), 1;
+
+ zsetup(env);
+ zinit(prod);
+ zinit(factor);
+ zsetu(prod, 1);
+
+ for (i = 1; i < argc; i++) {
+ zsets(factor, argv[i]);
+ zmul(prod, prod, factor);
+ }
+
+ printf("%s\n", buf = zstr(prod, NULL, 0));
+ free(buf);
+
+ zfree(factor);
+ zfree(prod);
+ zunsetup();
+ return 0;
+}
diff --git a/examples/03-avg.c b/examples/03-avg.c
new file mode 100644
index 0000000..7aa7645
--- /dev/null
+++ b/examples/03-avg.c
_AT_@ -0,0 +1,38 @@
+/* Calculates the truncated average of $_AT_ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <zahl.h>
+
+int
+main(int argc, char *argv[])
+{
+ z_t sum, term;
+ jmp_buf env;
+ char *buf;
+ int i;
+
+ if (setjmp(env))
+ return zperror(argv[0]), 1;
+
+ zsetup(env);
+ zinit(sum);
+ zinit(term);
+ zsetu(sum, 0);
+
+ for (i = 1; i < argc; i++) {
+ zsets(term, argv[i]);
+ zadd(sum, sum, term);
+ }
+ zseti(term, argc);
+ zdiv(sum, sum, term);
+
+ printf("%s\n", buf = zstr(sum, NULL, 0));
+ free(buf);
+
+ zfree(term);
+ zfree(sum);
+ zunsetup();
+ return 0;
+}
diff --git a/examples/04-median.c b/examples/04-median.c
new file mode 100644
index 0000000..7acff70
--- /dev/null
+++ b/examples/04-median.c
_AT_@ -0,0 +1,63 @@
+/* Calculates the median of $_AT_ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <zahl.h>
+
+int
+main(int argc, char *argv[])
+{
+ struct zahl *values;
+ z_t med, medmod;
+ jmp_buf env;
+ char *buf, *argv0;
+ int i, j;
+
+ argv0 = *argv++, argc--;
+
+ if (!argc) {
+ fprintf(stderr,
+ "%s: cannot calculate median of the empty bag\n",
+ argv0);
+ return 1;
+ }
+
+ values = calloc(argc, sizeof(*values));
+ if (!values)
+ return perror(argv0), 1;
+
+ if (setjmp(env))
+ return zperror(argv0), 1;
+
+ zsetup(env);
+ zinit(med);
+ zinit(medmod);
+
+ /* Since `values` where allocated with
+ * `calloc` it is already cleared and
+ * `zinit` is not necessary. */
+
+ for (i = 0; i < argc; i++)
+ zsets(&values[i], argv[i]);
+
+ qsort(values, argc, sizeof(*values),
+ (int (*)(const void *, const void *))zcmp);
+ i = argc / 2;
+ j = i - !(argc & 1);
+ zadd(med, &values[i], &values[j]);
+ zsetu(medmod, 2);
+ zdivmod(med, medmod, med, medmod);
+
+ printf("%s%s\n", buf = zstr(med, NULL, 0),
+ (const char *[]){"", ".5"}[zodd(medmod)]);
+ free(buf);
+
+ zfree(medmod);
+ zfree(med);
+ for (i = 0; i < argc; i++)
+ zfree(&values[i]);
+ free(values);
+ zunsetup();
+ return 0;
+}
Received on Sun Jun 19 2016 - 02:50:41 CEST
This archive was generated by hypermail 2.3.0
: Sun Jun 19 2016 - 03:00:23 CEST