1# ceval 2A C/C++ header for parsing and evaluation of arithmetic expressions. 3 4[README file is almost identical to that of the <a href="https://github.com/erstan/ceval#readme">ceval</a> library] 5 6## Functions accessibe from main() 7<table> 8<thead><th>Function</th><th>Argument(s)</th><th>Return Value</th></thead> 9<tbody> 10 <tr> 11 <td><code>ceval_result()</code></td> 12 <td>A mathematical expression in the form of a character array or a CPP string</td> 13 <td>The result of the expression as a floating point number</td> 14</tr> 15<tr> 16 <td><code>ceval_tree()</code></td> 17 <td>A mathematical expression in the form of a character array or a CPP string</td> 18 <td>The function prints the parse tree with each node properly indented depending on it's location in the tree structure</td> 19</tr> 20</tbody> 21</table> 22 23## Supported expressions 24Any valid combination of the following operators and functions, with floating point numbers as operands can be parsed by <b>ceval</b>. Parentheses can be used to override the default operator precedences. 25 26* Arithematic operators 27 28`+` (addition), `-` (subtraction), `*` (multiplication), `/` (division), `%` (modulo), `**` (exponentiation), `//` (quotient) 29* Relational operators 30 31`==` (equal), `!=` (not equal), `<` (strictly less), `>` (strictly greater), `<=` (less or equal), `>=` (greater or equal) to compare the results of two expressions 32 33* Single-argument functions 34 35`exp()`, `sqrt()`, `cbrt()`, `sin()`, `cos()`, `tan()`, `asin()`, `acos()`, `atan()`, `sinh()`, `cosh()`, `tanh()`, `abs()`, `ceil()`, `floor()`, `log10()`, `ln()`, `deg2rad()`, `rad2deg()`, `signum()`, `int()`, `frac()`, `fact()` 36 37* Two-argument functions 38 39`pow()`, `atan2()`, `gcd()`, `hcf()`, `lcm()`, `log()` (generalized log(b, x) to any base `b`) 40 41* Pre-defined math constants 42 43`_pi`, `_e` 44 45...pre-defined constants are prefixed with an underscore 46 47* Logical operators 48 49`&&`, `||` and `!` 50 51* Bitwise operators 52 53`&`, `|`, `^`, `<<`, `>>`, `~` 54 55* Other operators 56 57 * `,` (Comma operator) 58 Comma operator returns the result of it's rightmost operand 59 Ex: `2,3` would give `3`; `4,3,0` would be equal to `0`; and `cos(_pi/2,_pi/3,_pi)` would return `cos(_pi)` i.e, `-1` 60 * `e` (e-operator for scientific notation) 61 Using the binary `e` operator, we can use scientific notation in our arithmetic expressions 62 Ex: `0.0314` could be written as `3.14e-2`; `1230000` could be subsituted by `1.23e6` 63 64## Usage 65Include the ceval library using the `#include "PATH_TO_CEVAL.H"` directive your C/C++ project. 66 67The code snippet given below is a console based interpreter that interactively takes in math expressions from stdin, and prints out their parse trees and results. 68 69``` 70//lang=c 71#include<stdio.h> 72#include<stdlib.h> 73 74#include "ceval.h" 75 76int main(int argc, char ** argv) { 77 char expr[100]; 78 while (1) { 79 printf("In = "); 80 fgets(expr, 100, stdin); 81 if (!strcmp(expr, "exit\n")) { 82 break; 83 } else if (!strcmp(expr, "clear\n")) { 84 system("clear"); 85 continue; 86 } else { 87 ceval_tree(expr); 88 printf("\nOut = %f\n\n", ceval_result(expr)); 89 } 90 } 91 return 0; 92} 93``` 94 95## Test Run 96``` 97In = 3*7**2 98 2 99 ** 100 7 101* 102 3 103 104Out = 147.000000 105 106 107In = (3.2+2.8)/2 108 2 109/ 110 2.80 111 + 112 3.20 113 114Out = 3.000000 115 116 117In = _e**_pi>_pi**_e 118 2.72 119 ** 120 3.14 121> 122 3.14 123 ** 124 2.72 125 126Out = 1.000000 127 128 129In = 5.4%2 130 2 131% 132 5.40 133 134Out = 1.400000 135 136 137In = 5.4//2 138 2 139// 140 5.40 141 142Out = 2.000000 143 144 145In = 2*2.0+1.4 146 1.40 147+ 148 2 149 * 150 2 151 152Out = 5.400000 153 154 155In = (5/4+3*-5)+(sin(_pi))**2+(cos(_pi))**2 156 2 157 ** 158 3.14 159 cos 160+ 161 2 162 ** 163 3.14 164 sin 165 + 166 5 167 - 168 * 169 3 170 + 171 4 172 / 173 5 174 175Out = -12.750000 176 177 178In = 3,4,5,6 179 6 180, 181 5 182 , 183 4 184 , 185 3 186 187Out = 6.000000 188 189 190In = tanh(2/3)==(sinh(2/3)/cosh(2/3)) 191 3 192 / 193 2 194 cosh 195 / 196 3 197 / 198 2 199 sinh 200== 201 3 202 / 203 2 204 tanh 205 206Out = 1.000000 207 208 209In = (2+3/3+(3+9.7)) 210 9.70 211 + 212 3 213+ 214 3 215 / 216 3 217 + 218 2 219 220Out = 15.700000 221 222 223In = sin(_pi/2)+cos(_pi/2)+tan(_pi/2) 224 2 225 / 226 3.14 227 tan 228+ 229 2 230 / 231 3.14 232 cos 233 + 234 2 235 / 236 3.14 237 sin 238 239[ceval]: tan() is not defined for odd-integral multiples of _pi/2 240 241Out = nan 242 243 244In = asin(2) 245 2 246asin 247 248[ceval]: Numerical argument out of domain 249 250Out = nan 251 252 253In = exit 254... Program finished with exit code 0 255 256``` 257## Note 258When the `ceval.h` file is included in a C-program, you might require the `-lm` flag to link `math.h` 259 260```shell 261gcc file.c -lm 262``` 263 264