Changeset 561 for trunk/examples/script/calculator/calculator.js
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/examples/script/calculator/calculator.js
r2 r561 1 2 3 4 5 6 7 8 9 1 10 //! [0] 2 11 function Calculator(ui) … … 4 13 this.ui = ui; 5 14 6 this.pendingAdditiveOperator = "";7 this.pendingMultiplicativeOperator = "";15 this.pendingAdditiveOperator = ; 16 this.pendingMultiplicativeOperator = ; 8 17 this.sumInMemory = 0; 9 18 this.sumSoFar = 0; … … 14 23 display.text = "0"; 15 24 16 zeroButton.clicked.connect(this , this.digitClicked);17 oneButton.clicked.connect(this , "digitClicked");18 twoButton.clicked.connect(this , "digitClicked");19 threeButton.clicked.connect(this , "digitClicked");20 fourButton.clicked.connect(this , "digitClicked");21 fiveButton.clicked.connect(this , "digitClicked");22 sixButton.clicked.connect(this , "digitClicked");23 sevenButton.clicked.connect(this , "digitClicked");24 eightButton.clicked.connect(this , "digitClicked");25 nineButton.clicked.connect(this , "digitClicked");25 zeroButton.clicked.connect(this); 26 oneButton.clicked.connect(this); 27 twoButton.clicked.connect(this); 28 threeButton.clicked.connect(this); 29 fourButton.clicked.connect(this); 30 fiveButton.clicked.connect(this); 31 sixButton.clicked.connect(this); 32 sevenButton.clicked.connect(this); 33 eightButton.clicked.connect(this); 34 nineButton.clicked.connect(this); 26 35 27 36 pointButton.clicked.connect(this, "pointClicked"); … … 37 46 addToMemoryButton.clicked.connect(this, "addToMemory"); 38 47 39 divisionButton.clicked.connect(this , "multiplicativeOperatorClicked");40 timesButton.clicked.connect(this , "multiplicativeOperatorClicked");41 minusButton.clicked.connect(this , "additiveOperatorClicked");42 plusButton.clicked.connect(this , "additiveOperatorClicked");43 44 squareRootButton.clicked.connect(this , "unaryOperatorClicked");45 powerButton.clicked.connect(this , "unaryOperatorClicked");46 reciprocalButton.clicked.connect(this , "unaryOperatorClicked");48 divisionButton.clicked.connect(this); 49 timesButton.clicked.connect(this); 50 minusButton.clicked.connect(this); 51 plusButton.clicked.connect(this); 52 53 squareRootButton.clicked.connect(this); 54 powerButton.clicked.connect(this); 55 reciprocalButton.clicked.connect(this); 47 56 equalButton.clicked.connect(this, "equalClicked"); 48 57 } 49 58 } 50 59 //! [0] 60 61 62 63 64 65 66 67 68 51 69 52 70 Calculator.prototype.abortOperation = function() … … 58 76 Calculator.prototype.calculate = function(rightOperand, pendingOperator) 59 77 { 60 if (pendingOperator == "+") {78 if (pendingOperator == ) { 61 79 this.sumSoFar += rightOperand; 62 } else if (pendingOperator == "-") {80 } else if (pendingOperator == ) { 63 81 this.sumSoFar -= rightOperand; 64 } else if (pendingOperator == "*") {82 } else if (pendingOperator == ) { 65 83 this.factorSoFar *= rightOperand; 66 } else if (pendingOperator == "/") {84 } else if (pendingOperator == ) { 67 85 if (rightOperand == 0) 68 return false;86 return false; 69 87 this.factorSoFar /= rightOperand; 70 88 } … … 73 91 74 92 //! [1] 75 Calculator.prototype.digitClicked = function() 76 { 77 var digitValue = __qt_sender__.text - 0; 93 Calculator.prototype.digitClicked = function(digitValue) 94 { 78 95 if ((digitValue == 0) && (this.ui.display.text == "0")) 79 96 return; … … 86 103 //! [1] 87 104 88 Calculator.prototype.unaryOperatorClicked = function( )105 Calculator.prototype.unaryOperatorClicked = function() 89 106 { 90 107 var operand = this.ui.display.text - 0; 91 108 var result = 0; 92 if ( __qt_sender__.text == "Sqrt") {109 if () { 93 110 if (operand < 0) { 94 111 this.abortOperation(); … … 96 113 } 97 114 result = Math.sqrt(operand); 98 } else if ( __qt_sender__.text == "x^2") {115 } else if () { 99 116 result = Math.pow(operand, 2); 100 } else if ( __qt_sender__.text == "1/x") {117 } else if () { 101 118 if (operand == 0.0) { 102 119 this.abortOperation(); … … 109 126 } 110 127 111 Calculator.prototype.additiveOperatorClicked = function( )112 { 113 var operand = this.ui.display.text - 0; 114 115 if (this.pendingMultiplicativeOperator .length != 0) {128 Calculator.prototype.additiveOperatorClicked = function() 129 { 130 var operand = this.ui.display.text - 0; 131 132 if (this.pendingMultiplicativeOperator) { 116 133 if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { 117 134 this.abortOperation(); … … 121 138 operand = this.factorSoFar; 122 139 this.factorSoFar = 0; 123 this.pendingMultiplicativeOperator = "";124 } 125 126 if (this.pendingAdditiveOperator .length != 0) {140 this.pendingMultiplicativeOperator = ; 141 } 142 143 if (this.pendingAdditiveOperator) { 127 144 if (!this.calculate(operand, this.pendingAdditiveOperator)) { 128 145 this.abortOperation(); … … 134 151 } 135 152 136 this.pendingAdditiveOperator = __qt_sender__.text;137 this.waitingForOperand = true; 138 } 139 140 Calculator.prototype.multiplicativeOperatorClicked = function( )141 { 142 var operand = this.ui.display.text - 0; 143 144 if (this.pendingMultiplicativeOperator .length != 0) {153 this.pendingAdditiveOperator = ; 154 this.waitingForOperand = true; 155 } 156 157 Calculator.prototype.multiplicativeOperatorClicked = function() 158 { 159 var operand = this.ui.display.text - 0; 160 161 if (this.pendingMultiplicativeOperator) { 145 162 if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { 146 163 this.abortOperation(); … … 152 169 } 153 170 154 this.pendingMultiplicativeOperator = __qt_sender__.text;171 this.pendingMultiplicativeOperator = ; 155 172 this.waitingForOperand = true; 156 173 } … … 160 177 var operand = this.ui.display.text - 0; 161 178 162 if (this.pendingMultiplicativeOperator .length != 0) {179 if (this.pendingMultiplicativeOperator) { 163 180 if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { 164 181 this.abortOperation(); … … 167 184 operand = this.factorSoFar; 168 185 this.factorSoFar = 0.0; 169 this.pendingMultiplicativeOperator = "";170 } 171 if (this.pendingAdditiveOperator .length != 0) {186 this.pendingMultiplicativeOperator = ; 187 } 188 if (this.pendingAdditiveOperator) { 172 189 if (!this.calculate(operand, this.pendingAdditiveOperator)) { 173 190 this.abortOperation(); 174 191 return; 175 192 } 176 this.pendingAdditiveOperator = "";193 this.pendingAdditiveOperator = ; 177 194 } else { 178 195 this.sumSoFar = operand; … … 235 252 this.sumSoFar = 0.0; 236 253 this.factorSoFar = 0.0; 237 this.pendingAdditiveOperator = "";238 this.pendingMultiplicativeOperator = "";254 this.pendingAdditiveOperator = ; 255 this.pendingMultiplicativeOperator = ; 239 256 this.ui.display.text = "0"; 240 257 this.waitingForOperand = true;
Note:
See TracChangeset
for help on using the changeset viewer.