Merge lp:~rpadovani/ubuntu-calculator-app/improvePushFunction141209 into lp:ubuntu-calculator-app

Proposed by Riccardo Padovani
Status: Merged
Merged at revision: 29
Proposed branch: lp:~rpadovani/ubuntu-calculator-app/improvePushFunction141209
Merge into: lp:ubuntu-calculator-app
Prerequisite: lp:~rpadovani/ubuntu-calculator-app/moveJavascript141209
Diff against target: 193 lines (+105/-49)
2 files modified
app/engine/formula.js (+94/-0)
app/ubuntu-calculator-app.qml (+11/-49)
To merge this branch: bzr merge lp:~rpadovani/ubuntu-calculator-app/improvePushFunction141209
Reviewer Review Type Date Requested Status
Bartosz Kosiorek Approve
Review via email: mp+244125@code.launchpad.net

Commit message

Improved checks when push a new digit

Description of the change

Refactored how the calculator checks if a digit is valid: not use anymore vars, but parse all the formula and checks all requisites are valid

To post a comment you must log in.
Revision history for this message
Bartosz Kosiorek (gang65) wrote :

One small comment

review: Needs Information
Revision history for this message
Bartosz Kosiorek (gang65) wrote :

Delete is not working there, but it was already resolved in lp:~rpadovani/ubuntu-calculator-app/improveDeleteFunction141209
branch.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'app/engine/formula.js'
2--- app/engine/formula.js 2014-12-09 13:10:50 +0000
3+++ app/engine/formula.js 2014-12-09 13:10:51 +0000
4@@ -88,3 +88,97 @@
5
6 return engineFormulaToDisplay;
7 }
8+
9+/**
10+ * Function to check if an operator could be added to the formula
11+ *
12+ * @param char operatorToAdd: the operator that will be added to the formula
13+ * @param string longFormula: the actual formula
14+ * @return bool: true if the operator could be added, false otherwise
15+ */
16+function couldAddOperator(operatorToAdd, longFormula) {
17+ // No two operators one after other
18+ if (isOperator(longFormula.slice(-1))) {
19+ // But a minus after a * or a / is allowed
20+ if (!(operatorToAdd === "-" && (longFormula.slice(-1) === "*" ||
21+ longFormula.slice(-1) === "/"))) {
22+ return false;
23+ }
24+ }
25+
26+ // No operator after a dot
27+ if (longFormula.slice(-1) === ".") {
28+ return false;
29+ }
30+
31+ // No operator after an open brackets, but minus
32+ if (longFormula.slice(-1) === "(" && operatorToAdd !== "-") {
33+ return false;
34+ }
35+
36+ // No operator at beginning (but minus)
37+ if (longFormula === "" && operatorToAdd !== "-") {
38+ return false;
39+ }
40+
41+ return true;
42+}
43+
44+/**
45+ * Function to check if could be add a dot at the end of a formula
46+ *
47+ * @param string longFormula: the formula where we have to add the dot
48+ * @return bool: true if the dot could be added, false otherwhise
49+ */
50+function couldAddDot(longFormula) {
51+ // A dot could be only after a number
52+ if (isNaN(longFormula.slice(-1))) {
53+ return false;
54+ }
55+
56+ // If is after a number and it's the first dot of the calc it could be added
57+ if (longFormula.indexOf('.') === -1) {
58+ return true;
59+ }
60+
61+ // If there is already a dot we have to check if it isn't in the same operation
62+ // So we take all the string since the last occurence of dot to the end
63+ var lastOperation = longFormula.substring(longFormula.lastIndexOf('.') + 1);
64+
65+ // If there isn't something different from a number we can't add a dot
66+ if (!isNaN(lastOperation)) {
67+ return false;
68+ }
69+
70+ // If the only thing different from a number is a pi we cannot add a dot
71+ if (lastOperation.indexOf('pi') !== -1) {
72+ if (!isNaN(lastOperation.replace('pi', ''))) {
73+ return false;
74+ }
75+ }
76+
77+ return true;
78+}
79+
80+/**
81+ * Function to check if could be add a close bracket at the end of a formula
82+ *
83+ * @param string longFormula: the formula where we have to add the close bracket
84+ * @return bool: true if the close bracket could be added, false otherwhise
85+ */
86+function couldAddCloseBracket(longFormula) {
87+ // Don't close a bracket just after opened it
88+ if (longFormula.slice(-1) === "(") {
89+ return false;
90+ }
91+
92+ // Calculate how many brackets are opened
93+ numberOfOpenedBrackets = (longFormula.match(/\(/g) || []).length -
94+ (longFormula.match(/\)/g) || []).length;
95+
96+ if (numberOfOpenedBrackets < 1) {
97+ return false;
98+ }
99+
100+ return true;
101+}
102
103=== modified file 'app/ubuntu-calculator-app.qml'
104--- app/ubuntu-calculator-app.qml 2014-12-09 13:10:50 +0000
105+++ app/ubuntu-calculator-app.qml 2014-12-09 13:10:51 +0000
106@@ -58,11 +58,6 @@
107 // Becomes true after an user presses the "="
108 property bool isLastCalculate: false;
109
110- property int numberOfOpenedBrackets: 0;
111-
112- // Property needed to
113- property bool isAllowedToAddDot: true;
114-
115 property var decimalPoint: Qt.locale().decimalPoint
116
117 /**
118@@ -70,10 +65,6 @@
119 * place the result in right vars
120 */
121 function deleteLastFormulaElement() {
122- if (longFormula[longFormula.length - 1] === ".") {
123- isAllowedToAddDot = true;
124- }
125-
126 longFormula = Formula.deleteLastFormulaElement(isLastCalculate, longFormula);
127 shortFormula = longFormula;
128 displayedInputText = returnFormulaToDisplay(longFormula);
129@@ -81,40 +72,17 @@
130
131 function validateStringForAddingToFormula(stringToAddToFormula) {
132 if (Formula.isOperator(stringToAddToFormula)) {
133- if ((longFormula === '') && (stringToAddToFormula !== '-')) {
134- // Do not add operator at beginning
135- return false;
136- }
137-
138- if ((Formula.isOperator(previousVisual) && previousVisual !== ")")) {
139- // Not two operator one after otQt.locale().decimalPointher
140- return false;
141- }
142- }
143-
144- if (isNaN(stringToAddToFormula)) {
145- if (stringToAddToFormula !== ".") {
146- isAllowedToAddDot = true
147- }
148- }
149-
150- if (stringToAddToFormula[stringToAddToFormula.length - 1] === "(") {
151- numberOfOpenedBrackets = numberOfOpenedBrackets + 1
152- } else if (stringToAddToFormula === ")") {
153- // Do not allow closing brackets after opening bracket
154- // and do not allow adding too much closing brackets
155- if ((previousVisual === "(") || (numberOfOpenedBrackets < 1)) {
156- return false;
157- }
158- numberOfOpenedBrackets = numberOfOpenedBrackets - 1
159- } else if (stringToAddToFormula === ".") {
160- //Do not allow to have two decimal separators in the same number
161- if (isAllowedToAddDot === false) {
162- return false;
163- }
164-
165- isAllowedToAddDot = false;
166- }
167+ return Formula.couldAddOperator(stringToAddToFormula, longFormula);
168+ }
169+
170+ if (stringToAddToFormula === ".") {
171+ return Formula.couldAddDot(longFormula);
172+ }
173+
174+ if (stringToAddToFormula === ")") {
175+ return Formula.couldAddCloseBracket(longFormula);
176+ }
177+
178 return true;
179 }
180
181@@ -197,12 +165,6 @@
182 calculationHistory.addCalculationToDatabase(longFormula, result);
183 longFormula = result;
184 shortFormula = result;
185- numberOfOpenedBrackets = 0;
186- if (result % 1 != 0) {
187- isAllowedToAddDot = false;
188- } else {
189- isAllowedToAddDot = true;
190- }
191 }
192
193 CalculationHistory {

Subscribers

People subscribed via source and target branches