Taschenrechner
erstellt von Dr. Martin Loehnertz
—
zuletzt verändert:
25.11.2015 12:44
Javascriptbasierter Taschenrechner fürs Tablet
calc.html — HTML, 10 KB (11069 bytes)
Dateiinhalt
<!DOCTYPE HTML> <HTML> <HEAD> <STYLE> .Bruch{ float:right; } #NUMPAD {font-size:250%} #OUT {font-size:200%;position:relative;Border:solid;width:50%;text-align:right;height:75px;padding:10px;} input {width:100%;font-size:200%} #Basic {font-size:150%} #Clear {font-size:200% </STYLE> <SCRIPT> var stack,mtop,topnum; function ggt(a,b) { if (a==0 || b==0){return 1;} if (a>b) { if (a%b==0){return b;} else { return(ggt(b,a%b)); } } else {return(ggt(b,a));} } function meval(l,op,r) { var g; res=new mynum(0); res.mode=l.mode|r.mode; if (op=="+") { res.zaehl=l.zaehl*r.nenn+r.zaehl*l.nenn; res.nenn=l.nenn*r.nenn; } if (op=="-") { res.zaehl=l.zaehl*r.nenn-r.zaehl*l.nenn; res.nenn=l.nenn*r.nenn; } if (op=="*") { res.zaehl=l.zaehl*r.zaehl; res.nenn=l.nenn*r.nenn; } if (op=="/") { res.zaehl=l.zaehl*r.nenn; res.nenn=l.nenn*r.zaehl; } if (op=="^") { if(l.mode==0 && r.mode==0 && r.nenn==1) { res.zaehl=Math.pow(l.zaehl,r.zaehl); res.nenn=Math.pow(l.nenn,r.zaehl); } else { res.zaehl=10*Math.pow((l.zaehl/l.nenn),(r.zaehl/r.nenn)); res.nenn=10; res.mode=1; } } if (res.mode==0 && res.nenn==Math.floor(res.nenn) && res.zaehl==Math.floor(res.zaehl)) { g=ggt(Math.abs(res.zaehl),Math.abs(res.nenn)) res.zaehl=res.zaehl/g; res.nenn=res.nenn/g; } while(res.nenn>100000){res.nenn/=10;res.zaehl/=10;} res.lock(); return res; } function op(name,prec) { this.type=1; this.name=name; this.prec=prec; } function mynum(st) { this.type=0; this.zaehl=st; this.nenn=1; this.mode=0; var inmode=0; var frac=1; var sig=""; this.print=function() { if ((this.nenn==0 && inmode!=1) || Math.log(Math.abs(this.zaehl))/Math.log(10)>16) { return "ERROR"; } if (this.mode==1 && this.nenn!=0) { return("<DIV class=\"Zahl\">"+Math.floor(this.zaehl*1000000/this.nenn+0.5)/1000000+"</DIV>"); } else { if (this.nenn==1 || this.nenn==0) { return("<DIV class=\"Zahl\">"+this.zaehl+"</DIV>"); } else { if (this.zaehl<0){sig="<DIV style=\"float:left;\"> <hr solid style=\"padding:0px;margin:0px;width:100%\"> </DIV> ";}else{sig="";} return("<DIV class=\"Bruch\">"+sig+"<DIV style=\"text-align:center;float:right;\">"+Math.abs(this.zaehl)+"<HR solid style=\"padding:0px;margin:0px;width:100%\">"+Math.abs(this.nenn)+"</DIV></DIV>"); } } } this.addnum=function(x) { if (inmode==0){this.zaehl=this.zaehl*10+x;} if (inmode==1){this.nenn=this.nenn*10+x;} if (inmode==2){this.nenn*=10; this.zaehl=this.zaehl*10+x;} } this.key=function(x) { if (x=="f" && inmode==0) { inmode=1; this.nenn=0; } if (x==".") { if (inmode!=3) {inmode=2;} this.mode=1; mupdate(); } } this.lock=function() { inmode=3; } this.clone=function(r) { this.zaehl=r.zaehl; this.nenn=r.nenn; this.mode=r.mode; } } function mclear() { stack=Array(); mtop=0; topnum=0; stack[0]=new mynum(0); mupdate(); } function clearlast() { mtop=topnum; stack[topnum].zaehl=0; stack[topnum].nenn=1; stack[topnum].mode=0; mupdate(); } function num(x) { if (stack[mtop].type==0 && mtop==topnum) { stack[mtop].addnum(x); } else { mtop++; topnum=mtop; stack[mtop]=new mynum(0); stack[mtop].addnum(x); } mupdate(); } function frac() { if (stack[mtop].type==0 && mtop==topnum) { stack[mtop].key("f"); } mupdate(); } function mupdate() { out=document.getElementById("OUT"); out.innerHTML=stack[topnum].print(); } function op1(x) { var help; setsto=0; if (stack[mtop].type==0 && mtop==topnum) { switch(x) { case 1:{ stack[mtop].zaehl*=-1; break; } case 2: { help=Math.sin((Math.PI*stack[mtop].zaehl)/(stack[mtop].nenn*180)); stack[mtop].zaehl=help*10000; stack[mtop].nenn=10000; stack[mtop].mode=1; break; } case 3: { help=Math.cos((Math.PI*stack[mtop].zaehl)/(stack[mtop].nenn*180)); stack[mtop].zaehl=help*10000; stack[mtop].nenn=10000; stack[mtop].mode=1; break; } case 4: { help=Math.tan((Math.PI*stack[mtop].zaehl)/(stack[mtop].nenn*180)); stack[mtop].zaehl=help*10; stack[mtop].nenn=10; stack[mtop].mode=1; break; } case 5: { help=Math.asin(stack[mtop].zaehl/stack[mtop].nenn)*180/Math.PI; stack[mtop].zaehl=help*1000; stack[mtop].nenn=1000; stack[mtop].mode=1; break; } case 6: { help=Math.acos(stack[mtop].zaehl/stack[mtop].nenn)*180/Math.PI; stack[mtop].zaehl=help*1000; stack[mtop].nenn=1000; stack[mtop].mode=1; break; } case 7: { help=Math.atan(stack[mtop].zaehl/stack[mtop].nenn)*180/Math.PI; stack[mtop].zaehl=help*1000; stack[mtop].nenn=1000; stack[mtop].mode=1; break; } case 8: { help=Math.log(stack[mtop].zaehl/stack[mtop].nenn)/Math.log(10); stack[mtop].zaehl=help*1000; stack[mtop].nenn=1000; stack[mtop].mode=1; break; } case 9: { help=Math.log(stack[mtop].zaehl/stack[mtop].nenn); stack[mtop].zaehl=help*1000; stack[mtop].nenn=1000; stack[mtop].mode=1; break; } case 10: { help=Math.exp(stack[mtop].zaehl/stack[mtop].nenn); stack[mtop].zaehl=help*10; stack[mtop].nenn=10; stack[mtop].mode=1; break; } case 11: { help=Math.sqrt(stack[mtop].zaehl/stack[mtop].nenn); stack[mtop].zaehl=help*10; stack[mtop].nenn=10; stack[mtop].mode=1; break; } case 12: { help=Math.sign(stack[mtop].zaehl)*stack[mtop].nenn; stack[mtop].nenn=Math.abs(stack[mtop].zaehl); stack[mtop].zaehl=help; break; } } stack[mtop].lock(); } mupdate(); } function lb() { mtop++; stack[mtop]=new Object; stack[mtop].type=3; } function rb() { if (mtop>=0 && stack[mtop].type==3) { mtop--; return; } while (mtop>=2 && stack[mtop].type==0 && stack[mtop-1].type==1 && stack[mtop-2].type==0) { stack[mtop-2]=meval(stack[mtop-2],stack[mtop-1].name,stack[mtop]); mtop-=2; topnum-=2; } if (mtop>0 && stack[mtop-1].type==3) { stack[mtop-1]=stack[mtop]; mtop--; topnum=mtop; } mupdate(); } function op2(x) { setsto=0; if (stack[mtop].type==0) { stack[mtop].lock(); while (mtop>=2 && stack[mtop].type==0 && stack[mtop-1].type==1 && stack[mtop-1].prec>=opt[x].prec && stack[mtop-2].type==0) { stack[mtop-2]=meval(stack[mtop-2],stack[mtop-1].name,stack[mtop]); mtop-=2; topnum-=2; } if(x!=0) { mtop=mtop+1; stack[mtop]=new op(opt[x].name,opt[x].prec); } } else { while (mtop>=3 && stack[mtop-1].type==0 && stack[mtop-2].type==1 && stack[mtop-2].prec>=opt[x].prec && stack[mtop-3].type==0) { stack[mtop-3]=meval(stack[mtop-3],stack[mtop-2].name,stack[mtop-1]); mtop-=2; topnum-=2; } if(x!=0) { stack[mtop]=new op(opt[x].name,opt[x].prec); } else { mtop--; op2(0); } } mupdate(); } function dot() { if (stack[mtop].type==0 && mtop==topnum) { stack[mtop].key("."); } else { mtop++; topnum=mtop; stack[mtop]=new mynum(0); stack[mtop].key("."); } mupdate(); } function nopt(x,y) { this.name=x; this.prec=y; } function togglesto() { setsto=1; } function sto(x) { if (setsto==1) { stor[x].clone(stack[topnum]); setsto=0; } else { if (mtop!=topnum) { mtop++; stack[mtop]=new mynum(0); topnum=mtop; } stack[topnum].clone(stor[x]); mupdate(); } } </SCRIPT> </HEAD> <BODY> <H2> WilliCalc </H2> <DIV ID="OUT">gg</DIV> <DIV ID="NUMPAD" style="float:left"> <TABLE> <TR><TD><INPUT TYPE="BUTTON" VALUE=7 onClick="num(7)"></TD><TD><INPUT TYPE="BUTTON" VALUE=8 onClick="num(8)"></TD><TD><INPUT TYPE="BUTTON" VALUE=9 onClick="num(9)"></TD></TD><TD><INPUT TYPE="BUTTON" VALUE="B" onClick="frac()"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE=4 onClick="num(4)"></TD><TD><INPUT TYPE="BUTTON" VALUE=5 onClick="num(5)"></TD><TD><INPUT TYPE="BUTTON" VALUE=6 onClick="num(6)"></TD></TD><TD><INPUT TYPE="BUTTON" VALUE="." onClick="dot()"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE=1 onClick="num(1)"></TD><TD><INPUT TYPE="BUTTON" VALUE=2 onClick="num(2)"></TD><TD><INPUT TYPE="BUTTON" VALUE=3 onClick="num(3)"></TD></TD><TD><INPUT TYPE="BUTTON" VALUE=0 onClick="num(0)"></TD></TR> </TABLE> </DIV> <DIV ID="Basic" style="float:left"> <TABLE> <TR><TD><INPUT TYPE="BUTTON" VALUE="+/-" onClick="op1(1)"></TD><TD><INPUT TYPE="BUTTON" VALUE="CE" onClick="clearlast()"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="*" onClick="op2(3)"></TD><TD><INPUT TYPE="BUTTON" VALUE="/" onClick="op2(4)"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="+" onClick="op2(1)"></TD><TD><INPUT TYPE="BUTTON" VALUE=" - " onClick="op2(2)"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="(" onClick="lb()"></TD><TD><INPUT TYPE="BUTTON" VALUE=")" onClick="rb()"></TD></TR> </TABLE> </DIV> <DIV ID="Clear" style="float:left"> <TABLE> <TR><TD><INPUT TYPE="BUTTON" VALUE="C" onClick="mclear()"></TD></TR> <TR style="height:100px"></TR> <TR> <TD><INPUT TYPE="BUTTON" VALUE=" = " onClick="op2(0)"></TD> </TR> </TABLE> </DIV> <DIV ID="Functions" style="float:left"> <TABLE> <TR><TD><INPUT TYPE="BUTTON" VALUE="sin" onClick="op1(2)"></TD><TD><INPUT TYPE="BUTTON" VALUE="cos" onClick="op1(3)"></TD><TD><INPUT TYPE="BUTTON" VALUE="tan" onClick="op1(4)"></TD</TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="asin" onClick="op1(5)"></TD><TD><INPUT TYPE="BUTTON" VALUE="acos" onClick="op1(6)"></TD><TD><INPUT TYPE="BUTTON" VALUE="atan" onClick="op1(7)"></TD</TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="log" onClick="op1(8)"></TD><TD><INPUT TYPE="BUTTON" VALUE="ln" onClick="op1(9)"></TD><TD><INPUT TYPE="BUTTON" VALUE="e^x" onClick="op1(10)"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="sqr" onClick="op1(11)"><TD><INPUT TYPE="BUTTON" VALUE="1/x" onClick="op1(12)"></TD></TD><TD><INPUT TYPE="BUTTON" VALUE="x^y" onClick="op2(5)"></TD></TR> </TABLE> </DIV> <DIV ID="Mem" style="float:left"> <TABLE> <TR><TD><INPUT TYPE="BUTTON" VALUE="A" onClick="sto(0)"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="B" onClick="sto(1)"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="C" onClick="sto(2)"></TD></TR> <TR><TD><INPUT TYPE="BUTTON" VALUE="STO" onClick="togglesto()"></TD></TR> </TABLE> <script> setsto=0; opt=Array(); opt[0]=new nopt("=",0) opt[1]=new nopt("+",2) opt[2]=new nopt("-",2) opt[3]=new nopt("*",4) opt[4]=new nopt("/",4) opt[5]=new nopt("^",6) stor=Array(); stor[0]=new mynum(0); stor[1]=new mynum(0); stor[2]=new mynum(0); mclear(); </script> </BODY> </HTML>