forked from criezy/script_cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
equation_module.cpp
executable file
·157 lines (149 loc) · 5.43 KB
/
equation_module.cpp
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
/*
* Copyright (C) 2000, 2013 Thierry Crozat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact: [email protected]
*/
#include "equation_parser.h"
#include "modules.h"
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#ifdef USE_READLINE
#include <readline/readline.h>
#endif
void printEquationModuleHelp(int mode) {
switch (mode) {
case 1:
printf("The recognized constants are:\n");
printf(" - PI %f\n", M_PI);
break;
case 2:
printf("The recognized functions are:\n");
printf(" - sqrt(x) The square root of x.\n");
printf(" - cbrt(x) The cubic root of x.\n");
printf(" - pow(x, y) x raised to the power of y.\n");
printf(" - exp(x) The exponential of x (e raised to the power of x).\n");
printf(" - log10(x) The base 10 logarithm of x.\n");
printf(" - log(x) The natural logarithm of x.\n");
printf(" - ln(x) Same as log(x)\n");
printf(" - ceil(x) Round x up (the smallest integer bigger or equal to x).\n");
printf(" - floor(x) Round x down (the biggest integer smaller or equal to x).\n");
printf(" - fabs(x) Absolute value of x (x if x is positive, -x otherwise).\n");
printf(" - abs(x) Same as fabs(x)\n");
printf(" - cos(x) The cosine of x.\n");
printf(" - sin(x) The sine of x.\n");
printf(" - tan(x) The tangent of x.\n");
printf(" - asin(x) The arc sine of x.\n");
printf(" - acos(x) The arc cosine of x.\n");
printf(" - atan(x) The arc tangent of x.\n");
printf(" - sinh(x) The hyperbolic sine of x.\n");
printf(" - cosh(x) The hyperbolic cosine of x.\n");
printf(" - tanh(x) The hyperbolic tangent of x.\n");
printf(" - if (x, y, z) If x is true (not equal to zero) return y, otherwise return z.\n");
printf(" - print(x [, y, \"text\", z...]) Print the passed values and strings.\n");
break;
case 3:
printf("The recognized operators are:\n");
printf(" - x + y Add y to x.\n");
printf(" - x - y Substract y from x.\n");
printf(" - x * y Multiply x and y.\n");
printf(" - x / y Divide x by y.\n");
printf(" - x^y Raise x to the power of y (same as pow(x, y)).\n");
printf(" - +x or -x Unary plus and minus operators (change the sign of x).\n");
printf(" - x && y Logical and (return true - i.e. 1 - if x and y are not null).\n");
printf(" - x || y Logical or (return true - i.e. 1 - if either x or y - or both - is not null).\n");
printf(" - x == y Test equality (return true - i.e. 1 - if x is equal to y).\n");
printf(" - x != y Test inequality (return true - i.e. 1 - if x and y are different).\n");
printf(" - x < y Return true if x is smaller than y.\n");
printf(" - x <= y Return true if x is smaller or equal than y.\n");
printf(" - x > y Return true if x is greater than y.\n");
printf(" - x >= y Return true if x is greater or equal than y.\n");
printf(" - x = y Asignement (set x to y and return y).\n");
break;
default:
printf("Evaluates C-like mathematical expressions.\n");
printf("Type 'help topic' to get help on a specific topic.\n");
printf("Topics are: 'constants', 'functions', 'operators'\n");
printf("Type 'quit' or 'exit' to quit the program.\n");
break;
}
}
String readLine(const char* prompt, bool strip_eol) {
#ifdef USE_READLINE
char *line = readline(prompt);
String str = line;
if (line != NULL && *line != 0)
add_history(line);
free(line);
if (!strip_eol)
str += "\n";
return str;
#else
if (prompt != NULL && *prompt != 0)
printf("%s", prompt);
return readLine(strip_eol, stdin);
#endif
}
String readLine(bool strip_eol, FILE* stream) {
char buffer[256];
String line;
while (!feof(stream) && (line.isEmpty() || line[line.length() - 1] != '\n')) {
if (fgets(buffer, 256, stream) != NULL)
line += buffer;
}
if (strip_eol)
return line.left(-2);
return line;
}
void runEquationModule() {
printf("Starting simple equation mode.\nType 'help' to get some help.\n");
EquationParser parser;
StringList variables;
while (1) {
// Read a line
String line = readLine("> ");
if (line.isEmpty())
continue;
// Check if it is exit
if (line == "exit" || line == "quit")
break;
// Check if it is help
if (line.startsWith("help") && (line.length() == 4 || isspace(line[4]))) {
String topic = line.right(4).trimmed();
int t = 0;
if (topic == "constants")
t = 1;
else if (topic == "functions")
t = 2;
else if (topic == "operators")
t = 3;
printEquationModuleHelp(t);
continue;
}
// Treat it as an equation
if (!parser.parse(line, variables)) {
if (parser.nbErrors() == 0)
printf("Syntax error...\n");
else {
printf("Equation contains %d error(s):\n", parser.nbErrors());
for (int error = 0 ; error < parser.nbErrors() ; ++error)
printf(" %d: %s\n", error+1, parser.getError(error).c_str());
}
continue;
}
printf("%.12g\n", parser.evaluate());
}
}