Simplify tests for ParseExp.cpp
This commit is contained in:
parent
8b0f386121
commit
fe302d2ef5
190
src/ParseExp.cpp
190
src/ParseExp.cpp
|
@ -280,21 +280,21 @@ double ParseExp::EvaluateExpr(struct parse_opt* root, bool del)
|
|||
switch (type)
|
||||
{
|
||||
case ADD:
|
||||
P(std::cout << l << " + " << r << std::endl);
|
||||
P(std::cout << "\t" << l << " + " << r << std::endl);
|
||||
return l+r;
|
||||
case SUB:
|
||||
P(std::cout << l << " - " << r << std::endl);
|
||||
P(std::cout << "\t" << l << " - " << r << std::endl);
|
||||
return l-r;
|
||||
case MUL:
|
||||
P(std::cout << l << " * " << r << std::endl);
|
||||
P(std::cout << "\t" << l << " * " << r << std::endl);
|
||||
return l*r;
|
||||
case DIV:
|
||||
P(std::cout << l << " / " << r << std::endl);
|
||||
P(std::cout << "\t" << l << " / " << r << std::endl);
|
||||
return l/r;
|
||||
// case MOD:
|
||||
// return EvaluateExpr(root->l) % EvaluateExpr(root->r);
|
||||
case EXP:
|
||||
P(std::cout << l << " ^ " << r << std::endl);
|
||||
P(std::cout << "\t" << l << " ^ " << r << std::endl);
|
||||
res = 1;
|
||||
while (r--)
|
||||
res *= l;
|
||||
|
@ -309,147 +309,55 @@ double ParseExp::EvaluateExpr(struct parse_opt* root, bool del)
|
|||
#ifdef DEBUG
|
||||
using namespace ParseExp;
|
||||
|
||||
static char* e1;
|
||||
static inline int test(char* expr, double target_result)
|
||||
{
|
||||
struct parse_opt root, *r;
|
||||
char* e = e1;
|
||||
double res;
|
||||
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, expr);
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
res = EvaluateExpr(&root, true);
|
||||
|
||||
res *= 100;
|
||||
res = ((long)res)/100.0;
|
||||
|
||||
if (res == target_result)
|
||||
std::cout << e << " = " << res << "\n";
|
||||
else
|
||||
{
|
||||
std::cout << e << "(exp) " << target_result << " != " << "(res) " << res << "\n";
|
||||
throw "Invalid result !";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char* e1 = new char[100] ;
|
||||
char* e = e1;
|
||||
struct parse_opt root, *r;
|
||||
e1 = new char[100] ;
|
||||
|
||||
try
|
||||
{
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
strcpy(e1, "4*3");
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "4*3+5");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "3+3+3");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "3+3+3+3");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "4+3*5");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "-4+3*5/2");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "5+-4");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "5--4");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "4*(3+2)");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "(3+2)*4");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "4*(3+2)+5");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "5+(3+2)*4");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "3+(3+(3+3))");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "3+(3+(3+(3+3)))*5");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "5+(3/(6+8--5)*9)*4");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
ParseExp::ParseExp(&e1, r, false);
|
||||
std::cout << e << " = " << EvaluateExpr(&root, true) << "\n";
|
||||
|
||||
e1 = e;
|
||||
memset(e1, 0, 100);
|
||||
strcpy(e1, "5+(3*(6+8--5)/9)*4");
|
||||
memset(&root, 0, sizeof(root));
|
||||
r = &root;
|
||||
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";
|
||||
test("4*3", 12);
|
||||
test("4*3+5", 17);
|
||||
test("3+3+3", 9);
|
||||
test("3+3+3+3", 12);
|
||||
test("4+3*5", 19);
|
||||
test("-4+3*5/2", 3.5);
|
||||
test("5+-4", 1);
|
||||
test("5--4", 9);
|
||||
test("4*(3+2)", 20);
|
||||
test("(3+2)*4", 20);
|
||||
test("4*(3+2)+5", 25);
|
||||
test("5+(3+2)*4", 25);
|
||||
test("3+(3+(3+3))", 12);
|
||||
test("3+(3+(3+(3+3)))*5", 63);
|
||||
test("5+(3/(6+8--5)*9)*4", 10.68);
|
||||
test("78.83-(.39+6.46+3.54+.23+1.57)", 66.64);
|
||||
}
|
||||
catch (int e)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user