Merge lp:~f-riccardo87/ubuntu-calculator-app/plus-minus-improved into lp:~ubuntu-calculator-dev/ubuntu-calculator-app/old_trunk

Proposed by Riccardo Ferrazzo
Status: Merged
Approved by: Dalius
Approved revision: 32
Merged at revision: 32
Proposed branch: lp:~f-riccardo87/ubuntu-calculator-app/plus-minus-improved
Merge into: lp:~ubuntu-calculator-dev/ubuntu-calculator-app/old_trunk
Diff against target: 150 lines (+62/-31)
2 files modified
Simple/SimplePage.qml (+62/-30)
engine.js (+0/-1)
To merge this branch: bzr merge lp:~f-riccardo87/ubuntu-calculator-app/plus-minus-improved
Reviewer Review Type Date Requested Status
Dalius (community) Approve
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Review via email: mp+153844@code.launchpad.net

Commit message

Plus-minus logic improved

Description of the change

Plus minus logic improved

To post a comment you must log in.
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Dalius (dalius-sandbox) wrote :

I would love to use unit-tested this.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Simple/SimplePage.qml'
2--- Simple/SimplePage.qml 2013-03-18 08:18:19 +0000
3+++ Simple/SimplePage.qml 2013-03-18 15:41:32 +0000
4@@ -5,64 +5,96 @@
5 Page {
6
7 property var context: new CALC.Context;
8- property var scanner;
9+ property var scanner: new CALC.Scanner(engineFormula.join(''), context);
10 property string answer: ''
11- property var screenFormula: [{_text:'', _operation:'', _number:''}]
12+ property var screenFormula: [{_text:'', _operation: '', _number:''}]
13 property var engineFormula: ['']
14
15 function formulaPush(visual, engine) {
16+ if(screenFormula.length === 0){
17+ screenFormula.push({_text:'', _operation: '', _number:''});
18+ }
19+ if(engineFormula.length === 0){
20+ engineFormula.push('');
21+ }
22 try{
23- scanner = new CALC.Scanner(engineFormula.join('') + engine, context);
24+ var n_scanner = new CALC.Scanner(engineFormula.join('') + engine, context);
25 } catch(exception) {
26- console.log(exception)
27+ console.log("errore",exception)
28 return;
29 }
30- if(scanner.tokens.length > 1){
31- switch(scanner.tokens.last().type){
32+ if(n_scanner.tokens.length > 1){
33+ switch(n_scanner.tokens.last().type){
34 case CALC.T_PLUS:
35 case CALC.T_MINUS:
36 case CALC.T_DIV:
37 case CALC.T_TIMES:
38- if(scanner.tokens[scanner.tokens.length-2].type & CALC.T_OPERATOR) return;
39+ if(n_scanner.tokens[n_scanner.tokens.length-2].type & CALC.T_OPERATOR) return;
40+ scanner = n_scanner;
41 screenFormula.push({_text:'', _operation: '', _number:''});
42 screenFormula[screenFormula.length-1]._operation = visual;
43+ engineFormula.push('');
44 engineFormula[engineFormula.length-1] += engine;
45- engineFormula.push('');
46 formulaView.currentOperatorsChanged();
47 return;
48 case CALC.T_UNARY_PLUS:
49 case CALC.T_UNARY_MINUS:
50- if(scanner.tokens[scanner.tokens.length-2].type === CALC.T_UNARY_MINUS ||
51- scanner.tokens[scanner.tokens.length-2].type === CALC.T_UNARY_PLUS) return;
52+ if(n_scanner.tokens[n_scanner.tokens.length-2].type === CALC.T_UNARY_MINUS ||
53+ n_scanner.tokens[n_scanner.tokens.length-2].type === CALC.T_UNARY_PLUS ) return;
54 break;
55 }
56 }
57+ scanner = n_scanner;
58 screenFormula[screenFormula.length-1]._number += visual;
59 engineFormula[engineFormula.length-1] += engine;
60 formulaView.currentOperatorsChanged();
61 }
62
63 function changeSign(){
64- if(scanner !== undefined && scanner.tokens.length > 0){
65- switch(scanner.tokens.last().type){
66- case CALC.T_NUMBER:
67- if(scanner.tokens.length > 1 && scanner.tokens[scanner.tokens.length-2].type === CALC.T_UNARY_MINUS){
68- scanner.tokens.splice(scanner.tokens.length-2,1);
69- scanner.tokens.last().value = scanner.tokens.last().value * -1
70+ var prev_tokens = [];
71+ var add_minus = true;
72+ screenFormula.pop();
73+ engineFormula.pop();
74+ if(scanner.tokens.length === 0){
75+ prev_tokens.push(new CALC.Token('-', CALC.T_UNARY_MINUS));
76+ }
77+ while(scanner.tokens.length > 0 ){
78+ var last = scanner.tokens.pop();
79+ if(last.type & CALC.T_OPERATOR &&
80+ last.type !== CALC.T_UNARY_MINUS &&
81+ last.type !== CALC.T_UNARY_PLUS ){
82+ if(add_minus){
83+ prev_tokens.push(new CALC.Token('-', CALC.T_UNARY_MINUS));
84 }
85- scanner.tokens.last().value = scanner.tokens.last().value * -1
86- screenFormula[screenFormula.length-1]._number = (scanner.tokens.last().value).toString();
87- engineFormula[engineFormula.length-1] = (scanner.tokens.last().value).toString();
88- break;
89- case CALC.T_UNARY_MINUS:
90- scanner.tokens.pop();
91- screenFormula[screenFormula.length-1]._number = '';
92- engineFormula.pop();
93- break;
94- default:
95- formulaPush('-', '-');
96- }
97- } else { formulaPush('-','-'); }
98+ prev_tokens.push(last);
99+ break;
100+ }
101+ if(last.type === CALC.T_UNARY_MINUS){
102+ add_minus = false;
103+ continue;
104+ }
105+ prev_tokens.push(last);
106+ if(scanner.tokens.length === 0 && add_minus){
107+ prev_tokens.push(new CALC.Token('-', CALC.T_UNARY_MINUS));
108+ }
109+ }
110+ while(prev_tokens.length > 0){
111+ var last = prev_tokens.pop();
112+ var engine = last.value.toString();
113+ var visual = engine;
114+ switch(last.type){
115+ case CALC.T_TIMES:
116+ visual = '×';
117+ break;
118+ case CALC.T_DIV:
119+ visual = '÷';
120+ break;
121+ case CALC.T_MINUS:
122+ visual = "−";
123+ break;
124+ }
125+ formulaPush(visual, engine);
126+ }
127 formulaView.currentOperatorsChanged();
128 }
129
130@@ -92,7 +124,7 @@
131
132 function clear(){
133 answer = '';
134- screenFormula = [{_text:'', _operation:'', _number:''}];
135+ screenFormula = [{_text:'', _operation: '', _number:''}];
136 engineFormula = [''];
137 scanner = new CALC.Scanner(engineFormula.join(''), context);
138 formulaView.currentOperatorsChanged();
139
140=== modified file 'engine.js'
141--- engine.js 2013-03-14 15:38:48 +0000
142+++ engine.js 2013-03-18 15:41:32 +0000
143@@ -204,7 +204,6 @@
144 pop: Array.prototype.pop,
145 shift: Array.prototype.shift,
146 unshift: Array.prototype.unshift,
147- splice: Array.prototype.splice,
148
149 first: function first() {
150 // this is actualy faster than checking "this.length" everytime.

Subscribers

People subscribed via source and target branches