[gnome-calculator/60-split-out-a-backend-library] gcalc: implemented solving constants
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/60-split-out-a-backend-library] gcalc: implemented solving constants
- Date: Fri, 4 Jan 2019 16:23:27 +0000 (UTC)
commit 0017454c82364d1f3071e7d60ce2b9d246d7af5e
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Thu Jan 3 17:53:31 2019 -0600
gcalc: implemented solving constants
gcalc/gcalc-gconstant.vala | 3 +++
gcalc/gcalc-gmath-equation.vala | 11 ++++++++++-
gcalc/gcalc-gpolynomial.vala | 2 +-
gcalc/gcalc-gsolver.vala | 13 +++++++++++--
gcalc/gcalc-gterm.vala | 3 ++-
gcalc/gcalc-term.vala | 10 ++++------
tests/gcalc-solving-basic.vala | 26 ++++++++++++++++++++++++++
7 files changed, 57 insertions(+), 11 deletions(-)
---
diff --git a/gcalc/gcalc-gconstant.vala b/gcalc/gcalc-gconstant.vala
index e95c9b00..9d54b4f0 100644
--- a/gcalc/gcalc-gconstant.vala
+++ b/gcalc/gcalc-gconstant.vala
@@ -108,5 +108,8 @@ public class GCalc.GConstant : GExpression, Constant {
}
return "%g".printf (real ());
}
+ public override Result solve () {
+ return new GResult (this) as Result;
+ }
}
diff --git a/gcalc/gcalc-gmath-equation.vala b/gcalc/gcalc-gmath-equation.vala
index 56c82981..96b865ff 100644
--- a/gcalc/gcalc-gmath-equation.vala
+++ b/gcalc/gcalc-gmath-equation.vala
@@ -18,5 +18,14 @@
* Authors:
* Daniel Espinosa <esodan gmail com>
*/
-public class GCalc.GMathEquation : GExpression, MathEquation {}
+public class GCalc.GMathEquation : GExpression, MathEquation {
+ public override Result solve () {
+ if (expressions.get_n_items () == 0) {
+ var err = new GErrorResult ("No expressions found in equation");
+ return new GResult.with_error ((Expression) new GExpression (), (ErrorResult) err) as Result;
+ }
+ var e = expressions.get_item (0) as Expression;
+ return e.solve ();
+ }
+}
diff --git a/gcalc/gcalc-gpolynomial.vala b/gcalc/gcalc-gpolynomial.vala
index 09f3e550..cc7abaeb 100644
--- a/gcalc/gcalc-gpolynomial.vala
+++ b/gcalc/gcalc-gpolynomial.vala
@@ -34,7 +34,7 @@ public class GCalc.GPolynomial : GExpression, Polynomial {
continue;
}
try {
- current.sum (t);
+ current = current.sum (t) as Term;
} catch (GLib.Error err) {
var nerr = new GErrorResult (err.message);
return new GResult.with_error ((Expression) new GExpression (), (ErrorResult) nerr) as Result;
diff --git a/gcalc/gcalc-gsolver.vala b/gcalc/gcalc-gsolver.vala
index 8a90e0da..c3cffadf 100644
--- a/gcalc/gcalc-gsolver.vala
+++ b/gcalc/gcalc-gsolver.vala
@@ -31,10 +31,19 @@ public class GCalc.GSolver : Object, Solver {
Result res;
try {
p.parse (str, equation_manager);
- res = new GResult ((Expression) equation_manager.equations.get_item (0)) as Result; // FIXME: This
should return a constant object
+ if (equation_manager.equations.get_n_items () == 0) {
+ var err = new GErrorResult ("No equations found after parsing");
+ res = new GResult.with_error ((Expression) new GExpression (), (ErrorResult) err) as Result;
+ }
+ var eq = equation_manager.equations.get_item (0) as MathEquation;
+ if (eq == null) {
+ var err = new GErrorResult ("No equations found after parsing");
+ res = new GResult.with_error ((Expression) new GExpression (), (ErrorResult) err) as Result;
+ }
+ res = eq.solve ();
} catch (GLib.Error e) {
var err = new GErrorResult (e.message);
- res = new GResult.with_error ((Expression) new GExpression (), (ErrorResult) err) as Result; // FIXME:
This should return a constant object
+ res = new GResult.with_error ((Expression) new GExpression (), (ErrorResult) err) as Result;
}
return res;
}
diff --git a/gcalc/gcalc-gterm.vala b/gcalc/gcalc-gterm.vala
index 3998c7f8..8bb18227 100644
--- a/gcalc/gcalc-gterm.vala
+++ b/gcalc/gcalc-gterm.vala
@@ -18,5 +18,6 @@
* Authors:
* Daniel Espinosa <esodan gmail com>
*/
-public class GCalc.GTerm : GExpression, Term {}
+public class GCalc.GTerm : GExpression, Term {
+}
diff --git a/gcalc/gcalc-term.vala b/gcalc/gcalc-term.vala
index 258ba529..2167ee3f 100644
--- a/gcalc/gcalc-term.vala
+++ b/gcalc/gcalc-term.vala
@@ -40,12 +40,10 @@ public interface GCalc.Term : Object, Expression {
if (current == null) {
current = e;
first = false;
- } else {
- if (current is Constant) {
- if (current_operator != null) {
- if (e is Minus && e is Multiply) {
- current = (current as Constant).multiply (e as Constant);
- }
+ } else if (current is Constant) {
+ if (current_operator != null) {
+ if (e is Minus && e is Multiply) {
+ current = (current as Constant).multiply (e as Constant);
}
}
}
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 6489772f..76372fdf 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -76,6 +76,32 @@ class Tests {
assert (c3.real () == -10.0);
assert (c3.imag () == -15.0);
});
+ Test.add_func ("/gcalc/solve/constant",
+ ()=>{
+ try {
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("1", eqman);
+ assert (eqman.equations.get_n_items () == 1);
+ var eq = eqman.equations.get_item (0) as MathEquation;
+ assert (eq != null);
+ var e = eq.expressions.get_item (0) as Polynomial;
+ assert (e != null);
+ var t = e.expressions.get_item (0) as Term;
+ assert (t != null);
+ var c = t.expressions.get_item (0) as Constant;
+ assert (c != null);
+ var res = c.solve ();
+ assert (res != null);
+ assert (res.expression != null);
+ var rc = res.expression as Constant;
+ assert (rc != null);
+ message ("Constant Result: %s", rc.to_string ());
+ assert (rc.real () == 1.0);
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ });
return Test.run ();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]