aboutsummaryrefslogtreecommitdiff
path: root/expr.c
diff options
context:
space:
mode:
Diffstat (limited to 'expr.c')
-rw-r--r--expr.c69
1 files changed, 33 insertions, 36 deletions
diff --git a/expr.c b/expr.c
index 2210a4e6..d204859d 100644
--- a/expr.c
+++ b/expr.c
@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
+ */
/*
* This is only used to compute sensor values, hence the only variable supported is '@'.
* The '`' operator (ln(x)) is not available, nor multi-line formulas.
@@ -71,11 +71,11 @@ static inline gint priority(char operation)
case '(':
return 0;
}
-
+
return 0;
}
-GSList *math_infix_to_postfix(GSList *infix)
+GSList *math_infix_to_postfix(GSList * infix)
{
MathToken *stack[500];
gint t_sp = 0;
@@ -89,7 +89,8 @@ GSList *math_infix_to_postfix(GSList *infix)
if (t->type == TOKEN_OPERATOR && t->val.op == '(') {
stack[++t_sp] = t;
} else if (t->type == TOKEN_OPERATOR && t->val.op == ')') {
- for (top = stack[t_sp]; t_sp != 0 && top->val.op != '('; top = stack[t_sp])
+ for (top = stack[t_sp]; t_sp != 0 && top->val.op != '(';
+ top = stack[t_sp])
postfix = g_slist_append(postfix, stack[t_sp--]);
t_sp--;
} else if (t->type != TOKEN_OPERATOR) {
@@ -97,7 +98,8 @@ GSList *math_infix_to_postfix(GSList *infix)
} else if (t_sp == 0) {
stack[++t_sp] = t;
} else {
- while (t_sp != 0 && priority(t->val.op) <= priority(stack[t_sp]->val.op))
+ while (t_sp != 0
+ && priority(t->val.op) <= priority(stack[t_sp]->val.op))
postfix = g_slist_append(postfix, stack[t_sp--]);
stack[++t_sp] = t;
}
@@ -113,7 +115,7 @@ static inline gfloat __result(gfloat op1, gfloat op2, gchar operation)
{
switch (operation) {
case '^':
- return powf(op1, op2);
+ return powf(op1, op2);
case '+':
return op1 + op2;
case '-':
@@ -123,18 +125,18 @@ static inline gfloat __result(gfloat op1, gfloat op2, gchar operation)
case '*':
return op1 * op2;
}
-
+
return 0;
}
-gfloat math_postfix_eval(GSList *postfix, gfloat at_value)
+gfloat math_postfix_eval(GSList * postfix, gfloat at_value)
{
GSList *p;
gfloat stack[500];
gint sp = 0;
-
+
memset(stack, 0, sizeof(gfloat) * 500);
-
+
for (p = postfix; p; p = p->next) {
MathToken *t = (MathToken *) p->data;
@@ -155,7 +157,7 @@ gfloat math_postfix_eval(GSList *postfix, gfloat at_value)
return stack[sp];
}
-GSList *math_string_to_infix(gchar *string)
+GSList *math_string_to_infix(gchar * string)
{
GSList *infix = NULL;
gchar *expr = string;
@@ -168,12 +170,7 @@ GSList *math_string_to_infix(gchar *string)
} else if (strchr("-.1234567890", *expr)) {
gfloat value;
- sscanf(expr, "%f", &value);
-
- while (*expr && strchr(".1234567890", *expr))
- expr++;
- expr--;
-
+ expr += sscanf(expr, "%f", &value);
infix = g_slist_append(infix, new_value(value));
} else if (!isspace(*expr)) {
g_print("Invalid token: [%c][%d]\n", *expr, *expr);
@@ -185,43 +182,43 @@ GSList *math_string_to_infix(gchar *string)
return infix;
}
-void math_infix_free(GSList *infix, gboolean free_tokens)
+void math_infix_free(GSList * infix, gboolean free_tokens)
{
GSList *p;
-
+
if (!free_tokens)
- for (p = infix; p; p = g_slist_delete_link(p, p));
- else
- for (p = infix; p; p = g_slist_delete_link(p, p)) {
- MathToken *t = (MathToken *)p->data;
- g_free(t);
- }
+ for (p = infix; p; p = g_slist_delete_link(p, p));
+ else
+ for (p = infix; p; p = g_slist_delete_link(p, p)) {
+ MathToken *t = (MathToken *) p->data;
+ g_free(t);
+ }
}
-GSList *math_string_to_postfix(gchar *string)
+GSList *math_string_to_postfix(gchar * string)
{
GSList *infix;
GSList *postfix;
-
+
infix = math_string_to_infix(string);
if (!infix)
- return NULL;
-
+ return NULL;
+
postfix = math_infix_to_postfix(infix);
math_infix_free(infix, FALSE);
-
+
return postfix;
}
-gfloat math_string_eval(gchar *string, gfloat at_value)
+gfloat math_string_eval(gchar * string, gfloat at_value)
{
GSList *postfix;
gfloat val;
-
+
postfix = math_string_to_postfix(string);
val = math_postfix_eval(postfix, at_value);
math_postfix_free(postfix, TRUE);
-
+
return val;
}
@@ -229,14 +226,14 @@ gfloat math_string_eval(gchar *string, gfloat at_value)
int main(void)
{
GSList *postfix;
-
+
gchar *expr = "0.9*(@+(5.2*0.923+3*(2.0)))";
postfix = math_string_to_postfix(expr);
g_print("%s = %f (must be 18.71964)\n", expr,
- math_postfix_eval(postfix, 10));
+ math_postfix_eval(postfix, 10));
math_postfix_free(postfix, TRUE);
-
+
return 0;
}
#endif