Bug in expression parser, negative mark before parenthesis considered as positive.

This commit is contained in:
Grégory Soutadé 2017-01-01 17:20:25 +01:00
parent 0b037cff7c
commit 320d8689f6
2 changed files with 38 additions and 20 deletions

View File

@ -1,3 +1,10 @@
v0.7 (11/11/2016)
** User **
Set background calendar color to red or yellow when one account is negative or less than 200
** Dev **
** Bugs **
v0.6 (08/10/2016)
** User **
Set autofocus attribute to user in index.php

View File

@ -1,5 +1,5 @@
/*
Copyright 2010-2012 Grégory Soutadé
Copyright 2010-2016 Grégory Soutadé
This file is part of KissCount.
@ -258,7 +258,7 @@ void ParseExp::ParseExp(char** expr, struct parse_opt* root, bool needParenthesi
double ParseExp::EvaluateExpr(struct parse_opt* root, bool del)
{
double l, r;
double l, r, res;
char type;
type = root->type;
@ -284,7 +284,7 @@ double ParseExp::EvaluateExpr(struct parse_opt* root, bool del)
return l+r;
case SUB:
P(std::cout << l << " - " << r << std::endl);
return l+r;
return l-r;
case MUL:
P(std::cout << l << " * " << r << std::endl);
return l*r;
@ -295,7 +295,10 @@ double ParseExp::EvaluateExpr(struct parse_opt* root, bool del)
// return EvaluateExpr(root->l) % EvaluateExpr(root->r);
case EXP:
P(std::cout << l << " ^ " << r << std::endl);
return l*r;
res = 1;
while (r--)
res *= l;
return res;
case CST:
return l;
default:
@ -317,7 +320,7 @@ int main()
memset(&root, 0, sizeof(root));
r = &root;
strcpy(e1, "4*3");
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -325,7 +328,7 @@ int main()
strcpy(e1, "4*3+5");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -333,7 +336,7 @@ int main()
strcpy(e1, "3+3+3");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -341,7 +344,7 @@ int main()
strcpy(e1, "3+3+3+3");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -349,7 +352,7 @@ int main()
strcpy(e1, "4+3*5");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -357,7 +360,7 @@ int main()
strcpy(e1, "-4+3*5/2");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -365,7 +368,7 @@ int main()
strcpy(e1, "5+-4");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -373,7 +376,7 @@ int main()
strcpy(e1, "5--4");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -381,7 +384,7 @@ int main()
strcpy(e1, "4*(3+2)");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -389,7 +392,7 @@ int main()
strcpy(e1, "(3+2)*4");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -397,7 +400,7 @@ int main()
strcpy(e1, "4*(3+2)+5");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -405,7 +408,7 @@ int main()
strcpy(e1, "5+(3+2)*4");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -413,7 +416,7 @@ int main()
strcpy(e1, "3+(3+(3+3))");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -421,7 +424,7 @@ int main()
strcpy(e1, "3+(3+(3+(3+3)))*5");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -429,7 +432,7 @@ int main()
strcpy(e1, "5+(3/(6+8--5)*9)*4");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
@ -437,7 +440,15 @@ int main()
strcpy(e1, "5+(3*(6+8--5)/9)*4");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp(&e1, r, false);
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
e1 = e;
memset(e1, 0, 100);
strcpy(e1, "78.83-(.39+6.46+3.54+.23+1.57)");
memset(&root, 0, sizeof(root));
r = &root;
ParseExp::ParseExp(&e1, r, false);
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
}
catch (int e)