-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
221 lines (196 loc) · 6.39 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// eeval
// version 1.0
//
// a math expression evaluator
//
// main.c
//
// eeval implementation as a command line tool
//
// Copyright (c) 2016 Paolo Bertani - Kalei S.r.l.
// Licensed under the FreeBSD 2-clause license
//
#include "eeval.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Sample (dead) code that shows how to embed `eeval` in your program
void demo()
{
// eeval structure:
EEvaluation eval;
// input:
const char *expr = "-.02+pi+(3*sin(average(1,2,3)^-.03E2))"; // the expression
// output:
double result; // result of the evaluation
EEvalStatus status; // evaluation succedded or failed
const char *error; // description of error
// execute evaluation
status = EEvaluate( &eval, expr, &result );
// expression evaluation failed ?
if( status == EEvalFailure )
{
error = eval.error;
// or...
EEPrintError( &eval );
// ...
}
}
// Evaluates the expressin passed as parameter
// or perform self-test if invoked with "-t".
int main(int argc, const char * argv[])
{
EEvaluation eval;
double result;
long int precision;
char *endptr;
precision = 3; // default
const char *usage =
"\n"
"usage:\n"
"\n"
"eeval [[-p prec] 'expr']\n"
"\n"
"where expr is the expression to evaluate\n"
"and optional prec is the number of decimal digits\n"
"to be printed in the output (between 0 and 20 included)\n"
"\n"
"when invoked from the shell it's recomended\n"
"to place the expression between 'single' quotes\n"
"\n"
"if invoked without parameters usage and license info is printed\n"
"\n"
"supported operator are:\n"
"\n"
"+ plus\n"
"- minus\n"
"* multiplication\n"
"/ division\n"
"^ exponentiation\n"
"! factorial (using Gamma function)\n"
"\n"
"supported function are:\n"
"\n"
"sin(r) sine\n"
"cos(r) cosine\n"
"tan(r) tangent\n"
"asin(n) arcsin\n"
"acos(n) arccos\n"
"atan(n) arctan\n"
"fact(n) factorial of n; equivalent to n!\n"
"exp(n) equivalent to e^n\n"
"pow(b, n) equivalent to b^n\n"
"log(n) natural logarithm of n (base e)\n"
"log(b, n) logarithm of n with base b\n"
"max(n1, n2, n3, ...) maximum of one or more numbers\n"
"min(n1, n2, n3, ...) minimum of one or more numbers\n"
"average(n1, n2, ...) average of one or more numbers\n"
"avg(n1, n2, ...) abbreviated form of the above\n"
"\n"
"numbers can be expressed as follows:\n"
"\n"
"0.123 or .123 or 12.3E-2 etc..\n"
"\n"
"recognized constants are:\n"
"\n"
"e euler number\n"
"pi Pi\n"
"\n"
"use round brackets to nest expressions\n"
"whitespace, tabs and newlines are ignored\n"
"\n"
"execute self-test with:\n"
"eeval -t\n"
"(must have been built with test unit)\n"
"\n"
"-------------------------------------------------------------------------------\n"
"\n"
"FreeBSD 2-clause license\n"
"\n"
"Copyright (c) 2016, Paolo Bertani - Kalei S.r.l.\n"
"All rights reserved.\n"
"\n"
"Redistribution and use in source and binary forms, with or without\n"
"modification, are permitted provided that the following conditions are met:\n"
"\n"
"1. Redistributions of source code must retain the above copyright notice, this\n"
" list of conditions and the following disclaimer.\n"
"2. Redistributions in binary form must reproduce the above copyright notice,\n"
" this list of conditions and the following disclaimer in the documentation\n"
" and/or other materials provided with the distribution.\n"
"\n"
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n"
"ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
"WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n"
"DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\n"
"ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n"
"(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n"
"LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n"
"ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n"
"SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
"\n"
"-------------------------------------------------------------------------------\n"
"\n";
if( argc == 2 || argc == 4 )
{
// Three arguments are passed so it is expected to find `-p` option
// followed by the required precision.
if( argc == 4 )
{
if( strncmp( argv[1], "-p", 3 ) == 0)
{
precision = strtol( argv[ 2 ], &endptr, 10 );
if( endptr == argv[2] || *endptr != '\0' )
{
fprintf( stderr, "value specified for precision parameter is not a integer number\n" );
exit( 1 );
}
if( precision < 0 || precision > 20 )
{
fprintf( stderr, "value specified for precision parameter must be between 0 and 20 (included)\n" );
exit( 1 );
}
}
else
{
fprintf( stderr, "%s", usage );
exit( 1 );
}
}
// Requested self-test ? Execute and exit.
if( strncmp( argv[1], "-t", 3 ) == 0 )
{
if( eeval_test )
{
#if eeval_test == true
EEvalExecuteTests();
#endif
exit( 0 );
}
else
{
printf( "Test unit not available\n" );
exit( 1 );
}
}
// The passed expression is evaluated.
// If evaluation succeeds the result is printed.
// If fails then prints the error.
if( EEvaluate( &eval, argv[ argc - 1 ], &result ) == EEvalSuccess )
{
printf( "%.*f\n", (int)precision, result );
}
else
{
EEPrintError( &eval );
exit( 1 );
}
}
else
{
fprintf( stderr, "%s", usage );
exit( 1 );
}
}