Du bist hier: Snippet-Verzeichnis » HTML/JavaScript (65)
Sprache:

Calculator

Sprache: English
Programmiersprache: JavaScript
Veröffentlicht von: tfg [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1306


Beschreibung

A java script calculator

Code

1 <script> 2 // Author: Thamir Ghaslaan 3 // Created on: november 1999 4 // comments: hotthamir@hotmail.com 5 // Saudi Arabia 6 // submited to sourceforge.net on october 10th 2000 7 // source forge id: tfg 8 </script> 9 10 <HTML> 11 <HEAD> 12 <TITLE>Thamir's Calculator</TITLE> 13 <SCRIPT> 14 15 //flags to mark previous function 16 var opFlag=false //indicates previous entry was an operator 17 var eqFlag=false //indicates previous function was a calculation 18 19 20 function checkDec() { 21 //prevents multiple decimal points in entry 22 if (document.calc.screen.value.indexOf('.') == -1){ 23 enterVal('.') 24 return 25 } 26 27 if ((document.calc.screen.value.indexOf('.') >= 0) && ((eqFlag) || (opFlag))){ 28 enterVal('.') 29 return 30 } 31 32 } 33 34 function enterVal(num){ 35 //determine how to handle entry based on previous function 36 37 38 if (eqFlag) { 39 //last entry gave a total, no operators entered, so replace both stores 40 document.calc.input.value=num 41 document.calc.screen.value=num 42 eqFlag=false 43 return 44 } 45 46 if ((!opFlag) && (!eqFlag)){ 47 //no operators or totals entered, still entering numbers 48 document.calc.input.value += num 49 document.calc.screen.value += num 50 return 51 } 52 53 if (opFlag) { 54 //operator entered, change screen value, build on input 55 document.calc.screen.value =num 56 document.calc.input.value += num 57 opFlag=false 58 return 59 } 60 61 } 62 63 function compute() { 64 //if last entry is an operator, input is not complete so do nothing 65 66 len=document.calc.input.value.length 67 var oneChar=document.calc.input.value.charAt(len-1) 68 if ((oneChar == '+') || (oneChar == '-') || (oneChar == '*') || 69 (oneChar == '/')) { 70 return 71 } 72 73 document.calc.screen.value = fixIt(eval(document.calc.input.value)) 74 document.calc.input.value = fixIt(eval(document.calc.input.value)) 75 76 eqFlag=true 77 78 79 } 80 81 //a fix for JavaScript's crappy math handling 82 function fixIt(num) { 83 return(Math.round(num*10000000)/10000000); 84 } 85 86 87 function operator(op) { 88 //can't enter operator before entering a value 89 if (document.calc.screen.value == ''){ 90 return 91 } 92 93 //if last entry is not an operator, use the one just entered 94 eqFlag=false 95 len=document.calc.input.value.length 96 var oneChar=document.calc.input.value.charAt(len-1) 97 if ((oneChar != '+') && (oneChar != '-') && (oneChar != '*') && 98 (oneChar != '/') && (oneChar != '.')){ 99 document.calc.screen.value = eval(document.calc.input.value) 100 document.calc.input.value = eval(document.calc.input.value) 101 document.calc.input.value += op 102 103 } 104 else { 105 //if last entry is an operator, replace it with this one 106 //this returns input store less operator 107 document.calc.input.value=parseFloat(document.calc.input.value) 108 document.calc.input.value+=op 109 } 110 opFlag=true 111 } 112 113 function clearAll() { 114 document.calc.input.value = '' 115 document.calc.screen.value='' 116 117 } 118 119 function clearEntry() { 120 //don't do anything if the last entry is not a number 121 len=document.calc.input.value.length 122 var oneChar=document.calc.input.value.charAt(len-1) 123 if ((oneChar == '+') || (oneChar == '-') || (oneChar == '*') || 124 (oneChar == '/')) { 125 return 126 } 127 128 if (document.calc.screen.value == ''){ 129 //nothing here to clear 130 return 131 } 132 133 if (document.calc.screen.value==document.calc.input.value) { 134 //only one entry, so clear it 135 document.calc.screen.value='' 136 document.calc.input.value='' 137 return 138 } 139 //get last number entered and clear it, return previous value to screen 140 str1=document.calc.screen.value 141 str2=document.calc.input.value 142 ind=str2.lastIndexOf(str1) 143 document.calc.input.value=document.calc.input.value.substring(0,ind) 144 document.calc.screen.value=parseFloat(document.calc.input.value) 145 146 } 147 function sqrRT() { 148 149 if (document.calc.input.value == ''){ 150 //nothing entered, do nothing 151 return 152 } 153 154 document.calc.screen.value=Math.sqrt(document.calc.screen.value) 155 document.calc.input.value=document.calc.screen.value 156 eqFlag=true 157 } 158 159 function sqr() { 160 161 if (document.calc.input.value == ''){ 162 return 163 } 164 165 document.calc.screen.value=(document.calc.screen.value)*(document.calc.screen.value) 166 document.calc.input.value=document.calc.screen.value 167 eqFlag=true 168 } 169 170 function neg() {//get last entry and negate it 171 str1=document.calc.screen.value 172 str2=document.calc.input.value 173 ind=str2.lastIndexOf(str1) 174 175 document.calc.input.value=document.calc.input.value.substring(0,ind) 176 document.calc.screen.value=(document.calc.screen.value)*-1 177 document.calc.input.value += "(" + document.calc.screen.value + ")" 178 //placed in () to solve double operator problem 179 } 180 181 182 function pcnt() { 183 184 if (document.calc.input.value == ''){ //nothing here 185 return 186 } 187 //if last entry is an operator, do nothing 188 len=document.calc.input.value.length 189 var oneChar=document.calc.input.value.charAt(len-1) 190 if ((oneChar == '+') || (oneChar == '-') || (oneChar == '*') || 191 (oneChar == '/')) { 192 return 193 } 194 //check that there are two numbers entered 195 if ((document.calc.input.value.indexOf('+') == -1)&& 196 (document.calc.input.value.indexOf('-') == -1)&& 197 (document.calc.input.value.indexOf('*') == -1)&& 198 (document.calc.input.value.indexOf('/') == -1)){ 199 return 200 } 201 //calculate percentage 202 203 if ((document.calc.input.value.indexOf('+') != -1) || 204 (document.calc.input.value.indexOf('-') != -1)) { 205 206 percent=((document.calc.screen.value)/100)*parseFloat(document.calc.input.value) 207 208 } 209 210 else { 211 percent=((document.calc.screen.value)/100) 212 213 } 214 215 //replace last entry with percentage and evaluate expression 216 str1=document.calc.screen.value 217 str2=document.calc.input.value 218 ind=str2.lastIndexOf(str1) 219 220 document.calc.input.value=document.calc.input.value.substring(0,ind) 221 document.calc.input.value+=percent 222 document.calc.screen.value = eval(document.calc.input.value) 223 document.calc.input.value = eval(document.calc.input.value) 224 225 226 eqFlag=true 227 } 228 229 function memory_p() { 230 if (document.calc.screen.value ==''){ 231 return 232 } 233 //add screen value to memory store 234 document.calc.memory.value+= "+" + document.calc.screen.value 235 document.calc.memory.value=eval(document.calc.memory.value) 236 eqFlag=true 237 document.calc.mem_win.value='M' //indicator of something in memory 238 239 } 240 241 function memory_s() { 242 243 if (document.calc.screen.value ==''){ 244 return 245 } 246 247 document.calc.memory.value += "-(" + (document.calc.screen.value) +")" 248 document.calc.memory.value=eval(document.calc.memory.value) 249 250 eqFlag=1 251 document.calc.mem_win.value='M' 252 253 } 254 255 function memory_r() { 256 //recall memory store to screen 257 document.calc.screen.value='' 258 enterVal(document.calc.memory.value) 259 eqFlag=true 260 261 262 263 } 264 265 function memory_c() { 266 //clear memory indicator 267 document.calc.memory.value=0 268 document.calc.mem_win.value='' 269 270 } 271 272 function constant(what) { 273 if (what == 'pi') { 274 document.calc.screen.value='' 275 enterVal(Math.PI) 276 eqFlag=true 277 278 } 279 } 280 281 </SCRIPT> 282 </HEAD> 283 <BODY BGCOLOR="#FFFFFF"> 284 285 286 287 <CENTER> 288 <FORM NAME="calc"> 289 <INPUT TYPE=hidden NAME=memory VALUE=0 SIZE=16><INPUT TYPE=hidden NAME=input SIZE=16> 290 <table border=5><tr valign=center align=center><td bgcolor="#483D8B"> 291 292 <INPUT TYPE="text" NAME="mem_win" SIZE=1 VALUE=''> 293 <INPUT TYPE="text" NAME="screen" SIZE="20"></td></tr> 294 <tr><td BGCOLOR="#2f4f4f"> 295 <TABLE BORDER=1> 296 297 <TR ALIGN="center"> 298 <TD><INPUT TYPE="button" NAME="mem_p" VALUE=" M+ " OnClick="memory_p()"></TD> 299 <TD><INPUT TYPE="button" NAME="mem_s" VALUE=" M- " OnClick="memory_s()"></TD> 300 <TD><INPUT TYPE="button" NAME="mem_r" VALUE=" MR " OnClick="memory_r()"></TD> 301 <TD><INPUT TYPE="button" NAME="mem_c" VALUE=" MC " OnClick="memory_c()"></TD></TR> 302 303 304 <TR ALIGN="center"> 305 <TD><INPUT TYPE="button" NAME="sq_root" VALUE=" sqrt " OnClick="sqrRT()"></TD> 306 <TD><INPUT TYPE="button" NAME="square" VALUE=" sqr " OnClick="sqr()"></TD> 307 <TD><INPUT TYPE="button" NAME="percent" VALUE=" % " OnClick="pcnt()"></TD> 308 <TD><INPUT TYPE="button" NAME="negate" VALUE=" +/- " OnClick="neg()"></TD></TR> 309 310 311 <TR ALIGN="center"> 312 <TD><INPUT TYPE="button" NAME="seven" VALUE=" 7 " OnClick="enterVal('7')"></TD> 313 <TD><INPUT TYPE="button" NAME="eight" VALUE=" 8 " OnClick="enterVal('8')"></TD> 314 <TD><INPUT TYPE="button" NAME="nine" VALUE=" 9 " OnClick="enterVal('9')"></TD> 315 <TD><INPUT TYPE="button" NAME="div" VALUE=" / " OnClick="operator('/')"></TD></TR> 316 317 <TR ALIGN="center"> 318 <TD><INPUT TYPE="button" NAME="four" VALUE=" 4 " OnClick="enterVal('4')"></TD> 319 <TD><INPUT TYPE="button" NAME="five" VALUE=" 5 " OnClick="enterVal('5')"></TD> 320 <TD><INPUT TYPE="button" NAME="six" VALUE=" 6 " OnClick="enterVal('6')"></TD> 321 <TD><INPUT TYPE="button" NAME="times" VALUE=" x " OnClick="operator('*')"></TD></TR> 322 323 <TR ALIGN="center"> 324 <TD><INPUT TYPE="button" NAME="one" VALUE=" 1 " OnClick="enterVal('1')"></TD> 325 <TD><INPUT TYPE="button" NAME="two" VALUE=" 2 " OnClick="enterVal('2')"></TD> 326 <TD><INPUT TYPE="button" NAME="three" VALUE=" 3 " OnClick="enterVal('3')"></TD> 327 <TD><INPUT TYPE="button" NAME="minus" VALUE=" - " OnClick="operator('-')"></TD></TR> 328 329 <TR ALIGN="center"> 330 <TD><INPUT TYPE="button" NAME="zero" VALUE=" 0 " OnClick="enterVal('0')"></TD> 331 <TD><INPUT TYPE="button" NAME="deci" VALUE=" . " OnClick="checkDec()"></TD> 332 <TD><INPUT TYPE="button" NAME="doIt" VALUE=" = " OnClick="compute()"></TD> 333 <TD><INPUT TYPE="button" NAME="plus" VALUE=" + " OnClick="operator('+')"></TD></TR> 334 335 <TR ALIGN="center"> 336 <TD COLSPAN=2><INPUT TYPE="button" NAME="clear" VALUE="CLEAR" OnClick="clearAll()"></TD> 337 </TR> 338 </TABLE> 339 </td></tr></table> 340 341 </FORM> 342 </CENTER> 343 344 </BODY> 345 </HTML> 346 347

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS