📄 expreval.html
字号:
A + or - sign is invalid because
this is handled elsewhere by the libray.</li>
</ul>
Parameters:
<ul>
<li>*num - number to check</li>
</ul>
Returns:
<ul>
<li>0 on invalid. anything else on valid</li>
</ul>
</li>
</ul>
</p>
</blockquote>
</div>
<div align="left" class="container">
<h2><a name="Compiling">Compiling the ExprEval library</a></h2>
<p>Compiling the ExprEval library is pretty simple. Just
compile all of the source files (*.c) and link them into
a library. You need to keep "expreval.h" and "expreval.hpp"
for the header files.</p>
<p>The library used to have an option of fast variable access or
slow variable access by using EXPR_FAST_VAR_ACCES. Now,
fast variable access is used whether EXPR_FAST_VAR_ACCESS
is defined or not.</p>
<p>You may have to make some changes to the library. I've
tried to make doing so as simple as possible. If you
need to change the include files or some macros or whatnot,
edit the file "exprincl.h" This file includes any other files
needed. For example, on Borland compilers you will
need to change the include of "memory.h" to "mem.h".
If some functions are not provided for your system, Edit the
file "exprincl.c". You will need to add the functions as
they are used by the library, and make them call the correct
function for your system and return the value as the library
expects. You should not have to change to much. I have
tried to stick as close to ANSI/ISO C as I can.</p>
<p><b>Example:</b>
<blockquote>
<pre>
/* This example assumes that your compiler does not have
the malloc or free functions. */
void *malloc(int size)
{
/* Assume that you must request pages of memory instead */
int pages_needed;
/* Figure out how many pages are needed */
pages_needed = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
/* Assume alloc_pages allocates a number of pages and
returns a pointer to them or NULL */
return alloc_pages(pages_needed);
}
void free(void *ptr)
{
dealloc_pages(ptr);
}
</pre>
</blockquote>
</p>
</div>
<div align="left" class="container">
<h2><a name="Drawbacks">Drawbacks/Problems</a></h2>
<p>The following is a list of some basic drawbacks of this
library:
<ul>
<li>This library is an expression evaluator, and nothing
else. It does not simplify expressions and it
does not do advanced math such as calculating the
integrals and differentials of expression.</li>
<li>This library has no way of detecting overflows
except for those caused by the internal math
routines. Adding two very very large numbers
may cause an overflow to occur.</li>
<li>This library is not super easy to use in an application.
It has been designed to give much control to the
developer. Because of this, the function/value lists
are seperate from the expression objects, allowing
the developer to use them however they need.</li>
<li>There is no way to delete a single function, variable,
or constant from a list. This is because I see no
real need to do so because of the way the library
works. There is no need to delete function from
a function list or constants from a constant list.
There are are also no decent reasons to delete
variables from a variable list until you are completely
done and delete all of them.</li>
</ul>
</p>
</div>
<div align="left" class="container">
<h2><a name="Example">Example Use</a></h2>
<p>This is an example application of this library. It is a
graphics program that calculates the color value of a
pixel based on it's X,Y co-ordinate. It uses a made-up
image library called graphic-lib</p>
<blockquote>
<pre>
/* Include files */
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include "graphiclib.h"
#include "expreval.h"
char *transerr(int err)
{
/* Translate error code into message */
}
void gen_image(char *name, int w, int h, char *expr);
{
exprFuncList *f;
exprValList *v;
exprValList *c;
exprObj *o;
int x, y, err;
jmp_buf jumper;
int image;
EXPRTYPE r, g, b;
/* Error handling */
err = setjmp(jumper);
if(err)
{
if(err != ID_IMAGENOERROR)
printf("Error %d occurred: %s\n", err, transerr(err));
exprFree(o);
exprFreeFuncList(f);
exprFreeValList(v);
exprFreeValList(c);
image_free(image);
return;
}
/* Set up lists */
/* Function list */
err = exprFuncListCreate(&f);
if(err != EXPR_ERROR_NOERROR)
longjmp(jumper, err);
err = exprFuncListInit(f);
if(err != EXPR_ERROR_NOERROR)
{
printf("Function list init error. Functions may not be available.\n");
}
/* Variable list */
err = exprValListCreate(&v);
if(err != EXPR_ERROR_NOERROR)
longjmp(jumper, err);
/* Constant list */
err = exprValListCreate(&c);
if(err != EXPR_ERROR_NOERROR)
{
printf("Constants not available\n");
}
else
{
err = exprValListInit(c);
if(err != EXPR_ERROR_NOERROR)
printf("Constant list init error. Constants may not be available.\n");
}
/* Create and parse the expression */
/* Create */
err = exprCreate(&o, f, v, c, NULL, 0);
if(err != EXPR_ERROR_NOERROR)
longjmp(jumper, err);
/* Parse expression */
err = exprParse(o, expr);
if(err != EXPR_ERROR_NOERROR)
longjmp(jumper, err);
/* Create the image */
image = image_create(w, h);
if(image == 0)
{
longjmp(jumper, ID_IMAGECREATEERROR);
}
/* Add width and height to variable list */
exprValListAdd(v, "w", (EXPRTYPE)w);
exprValListAdd(v, "h", (EXPRTYPE)h);
for(y = 0; y < h; y++)
{
for(x = 0; x < w; x++)
{
/* Add x and y */
exprValListAdd(v, "x", (EXPRTYPE)x);
exprValListAdd(v, "y", (EXPRTYPE)y);
/* Eval expression, ignoring errors */
exprEval(o);
/* Get colors */
exprValListGet(v, "r", &r);
exprValListGet(v, "g", &g);
exprValListGet(v, "b", &b);
/* Set pixel */
image_setpixel(image, x, y, (int)r, (int)g, (int)b);
}
}
/* Save image */
image_save(image, name);
/* Done */
longjmp(jumper, ID_IMAGENOERROR);
}
void main(void)
{
char name[MAXPATH]
char tmp[10];
char expr[4096];
int sx, sy;
printf("Image name to save: ");
gets(name);
printf("Image width: ");
gets(tmp);
sx = atoi(tmp);
printf("Image height: ");
gets(tmp);
sy = atoi(tmp);
printf("Color Expression (Use x, y, w, h Set r, g, b): ");
gets(expr);
gen_image(name, sx, sy, expr);
}
</pre>
</blockquote>
</div>
<div align="left" class="container">
<h2><a name="ExampleFast">Example Use with Fast Variable Access</a></h2>
<p>This is an example application of this library. It is a
graphics program that calculates the color value of a
pixel based on it's X,Y co-ordinate. It uses a made-up
image library called graphic-lib. It uses faster variable
access by using the exprValListGetAddress function.</p>
<blockquote>
<pre>
/* Include files */
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include "graphiclib.h"
#include "expreval.h"
char *transerr(int err)
{
/* Translate error code into message */
}
void gen_image(char *name, int w, int h, char *expr);
{
exprFuncList *f;
exprValList *v;
exprValList *c;
exprObj *o;
int x, y, err;
jmp_buf jumper;
int image;
EXPRTYPE *v_x, *v_y;
EXPRTYPE *v_r, *v_g, *v_b;
/* Error handling */
err = setjmp(jumper);
if(err)
{
if(err != ID_IMAGENOERROR)
printf("Error %d occurred: %s\n", err, transerr(err));
exprFree(o);
exprFreeFuncList(f);
exprFreeValList(v);
exprFreeValList(c);
image_free(image);
return;
}
/* Set up lists */
/* Function list */
err = exprFuncListCreate(&f);
if(err != EXPR_ERROR_NOERROR)
longjmp(jumper, err);
err = exprFuncListInit(f);
if(err != EXPR_ERROR_NOERROR)
{
printf("Function list init error. Functions may not be available.\n");
}
/* Variable list */
err = exprValListCreate(&v);
if(err != EXPR_ERROR_NOERROR)
longjmp(jumper, err);
/* Constant list */
err = exprValListCreate(&c);
if(err != EXPR_ERROR_NOERROR)
{
printf("Constants not available\n");
}
else
{
err = exprValListInit(c);
if(err != EXPR_ERROR_NOERROR)
printf("Constant list init error. Constants may not be available.\n");
}
/* Create and parse the expression */
/* Create */
err = exprCreate(&o, f, v, c, NULL, 0);
if(err != EXPR_ERROR_NOERROR)
longjmp(jumper, err);
/* Parse expression */
err = exprParse(o, expr);
if(err != EXPR_ERROR_NOERROR)
longjmp(jumper, err);
/* Create the image */
image = image_create(w, h);
if(image == 0)
{
longjmp(jumper, ID_IMAGECREATEERROR);
}
/* Add width and height to variable list */
exprValListAdd(v, "w", (EXPRTYPE)w);
exprValListAdd(v, "h", (EXPRTYPE)h);
/* Add x and y to the list */
exprValListAdd(v, "x", 0.0);
exprValListAdd(v, "y", 0.0);
/* Add r, g, and b to the list */
exprValListAdd(v, "r", 0.0);
exprValListAdd(v, "g", 0.0);
exprValListAdd(b, "b", 0.0);
/* Get addresses. Assume no error */
exprValListGetAddress(v, "x", &v_x);
exprValListGetAddress(v, "y", &v_y);
exprValListGetAddress(v, "r", &v_r);
exprValListGetAddress(v, "g", &v_g);
exprValListGetAddress(v, "g", &v_b);
for(y = 0; y < h; y++)
{
for(x = 0; x < w; x++)
{
/* Directly set the x and y variables */
*v_x = (EXPRTYPE)x;
*v_y = (EXPRTYPE)y;
/* Eval expression, ignoring errors */
exprEval(o);
/* Set pixel, using variables directly */
image_setpixel(image, x, y, (int)(*v_r), (int)(*v_g), (int)(*v_b));
}
}
/* Save image */
image_save(image, name);
/* Done */
longjmp(jumper, ID_IMAGENOERROR);
}
void main(void)
{
char name[MAXPATH]
char tmp[10];
char expr[4096];
int sx, sy;
printf("Image name to save: ");
gets(name);
printf("Image width: ");
gets(tmp);
sx = atoi(tmp);
printf("Image height: ");
gets(tmp);
sy = atoi(tmp);
printf("Color Expression (Use x, y, w, h Set r, g, b): ");
gets(expr);
gen_image(name, sx, sy, expr);
}
</pre>
</blockquote>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -