[gcalctool] Handle equations like (x+3y)(2x-4y)



commit 794dd70a22286c9302062640881ec06f27358e7f
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon Mar 22 14:51:50 2010 +1100

    Handle equations like (x+3y)(2x-4y)

 NEWS                     |    2 ++
 src/mp-equation-parser.y |    4 +---
 src/unittest.c           |   13 +++++++++++--
 3 files changed, 14 insertions(+), 5 deletions(-)
---
diff --git a/NEWS b/NEWS
index dc38577..5a522e5 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,8 @@ Overview of changes in gcalctool 5.29.92
     * Handle powers in convesion units, (e.g. 1m² in mm²)
 
     * Don't clear ans when clearing display (Bug #613552)
+    
+    * Handle equations like (x+3y)(2x-4y)
 
 Overview of changes in gcalctool 5.29.92
 
diff --git a/src/mp-equation-parser.y b/src/mp-equation-parser.y
index 0f019de..96d0fc9 100644
--- a/src/mp-equation-parser.y
+++ b/src/mp-equation-parser.y
@@ -200,9 +200,8 @@ unit:
 
 exp:
   '(' exp ')' {mp_set_from_mp(&$2, &$$);}
+| exp '(' exp ')' {mp_multiply(&$1, &$3, &$$);}
 | '|' exp '|' {mp_abs(&$2, &$$);}
-| '|' tVARIABLE '|' {get_variable(yyscanner, $2, 1, &$$); mp_abs(&$$, &$$); free($2);} /* FIXME: Shouldn't need this rule but doesn't parse without it... */
-| '|' tNUMBER tVARIABLE '|' {get_variable(yyscanner, $3, 1, &$$); mp_multiply(&$2, &$$, &$$); mp_abs(&$$, &$$); free($3);} /* FIXME: Shouldn't need this rule but doesn't parse without it... */
 | exp '^' exp {mp_xpowy(&$1, &$3, &$$);}
 | exp tSUPNUM {mp_xpowy_integer(&$1, $2, &$$);}
 | exp tNSUPNUM {mp_xpowy_integer(&$1, $2, &$$);}
@@ -231,7 +230,6 @@ variable:
   term {mp_set_from_mp(&$1, &$$);}
 | tFUNCTION exp {if (!get_function(yyscanner, $1, &$2, &$$)) YYABORT; free($1);}
 | tFUNCTION tSUPNUM exp {if (!get_function(yyscanner, $1, &$3, &$$)) YYABORT; mp_xpowy_integer(&$$, $2, &$$); free($1);}
-| tVARIABLE exp {set_error(yyscanner, PARSER_ERR_UNKNOWN_FUNCTION, $1); free($1); YYABORT;}
 | tVARIABLE tSUPNUM exp {set_error(yyscanner, PARSER_ERR_UNKNOWN_FUNCTION, $1); free($1); YYABORT;}
 | tSUBNUM tROOT exp {mp_root(&$3, $1, &$$);}
 | tROOT exp {mp_sqrt(&$2, &$$);}
diff --git a/src/unittest.c b/src/unittest.c
index defb1ab..147480e 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -254,8 +254,12 @@ test_equations()
     test("y", "3", 0);
     test("z", "", PARSER_ERR_UNKNOWN_VARIABLE);
     test("2y", "6", 0);
-    test("y2", "", PARSER_ERR_UNKNOWN_FUNCTION);
-    test("y(2)", "", PARSER_ERR_UNKNOWN_FUNCTION);
+    test("y2", "", PARSER_ERR_INVALID);
+    test("y 2", "", PARSER_ERR_INVALID);
+    test("2z", "", PARSER_ERR_UNKNOWN_VARIABLE);  
+    test("z2", "", PARSER_ERR_UNKNOWN_VARIABLE);
+    test("z 2", "", PARSER_ERR_UNKNOWN_VARIABLE);
+    test("z(2)", "", PARSER_ERR_UNKNOWN_VARIABLE);
     test("y²", "9", 0);
     test("2y²", "18", 0);
     test("xÃ?y", "6", 0);
@@ -269,6 +273,11 @@ test_equations()
     test("2xy²", "36", 0);
     test("2x²y²", "72", 0);
     test("x²yx²y", "144", 0);
+    test("x³+2x²â??5", "11", 0);
+    test("2(x+3y)", "22", 0);
+    test("x(x+3y)", "22", 0);
+    test("(x+3y)(2x-4y)", "â??88", 0);
+    test("2x²+2xyâ??12y²", "â??88", 0);
 
     test("Ï?", "3.141592654", 0);
     test("e", "2.718281828", 0);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]