From: Heng Li Date: Tue, 16 Aug 2016 21:41:34 +0000 (-0400) Subject: added a 2nd kexpr example X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=7f963fbe5750c5f43fbc4930777d83fbe4dbcb68;p=klib.git added a 2nd kexpr example --- diff --git a/index.html b/index.html index 5a39426..85d981e 100644 --- a/index.html +++ b/index.html @@ -8604,12 +8604,30 @@ int main(int argc, char *argv[]) } ``` -
+
!!Synopsis
 * Functionality: parse a mathematical expression with the [[Shunting-yard algorithm|https://en.wikipedia.org/wiki/Shunting-yard_algorithm]] and evaluate the final value. The library supports functions and variables.
 * Library source code: [[kexpr.h|https://github.com/attractivechaos/klib/blob/master/kexpr.h]] and [[kexpr.c|https://github.com/attractivechaos/klib/blob/master/kexpr.c]]
 * Dependencies: none
 !!Example
+!!!Example 1: basic functionality
+```c
+#include <math.h>
+#include <stdio.h>
+#include "kexpr.h"
+
+int main()
+{
+	kexpr_t *ke;
+	int err;
+	ke = ke_parse("5.1+6*myexp(x)", &err);   // parse expression
+	ke_set_real(ke, "x", 1.0);               // set the value of a variable
+	ke_set_real_func1(ke, "myexp", exp);     // define a math function
+	printf("%g\n", ke_eval_real(ke, &err));  // compute the expression
+	return 0;
+}
+```
+!!!Example 2: complete command-line calculator
 ```c
 // to compile: gcc -O2 this-prog.c kexpr.c -o kexpr-demo
 // to run:     ./kexpr-demo "5.6+exp(x)" x=1.1