/* calc */
const ires=$('input_result');
const ores=$('output_result');
var iresdat="";
var oresdat="";
var calculated=0;
function calculatei() {
//iresdat=iresdat.replace(/%/g,'/100*');
if(iresdat.indexOf("%")!=-1) {
if(lpara!=0) {
var val1=iresdat.substr(llpara,ladd-llpara-1);
var val2=iresdat.substr(ladd,lpara-ladd-2);
//iresdat=iresdat.replace(pval1,"__pval1__");
//iresdat=iresdat.replace(pval2,"__pval2__");
} else {
var val1=iresdat.substr(lladd,ladd-1);
var val2=iresdat.substr(ladd,iresdat.length-ladd-1);
//var val2=iresdat.substr(ladd,iresdat.length-1);
}
//alert(val1+"*"+val2+"/100");
var sum2=evil(val1+"*"+val2+"/100");
//alert(val1+"*"+val2+"/100");
if(false!==sum2) {
oresdat=iresdat;
var tmpiresdat=iresdat.substr(0,ladd)+"%";
if(lpara!=0) {
tmpiresdat+=")";
if(iresdat.length>lpara) {
llpara=lpara;
}
lpara=iresdat.length;
}
//var tmpiresdat=iresdat.substr(0,ladd)+"";
iresdat=tmpiresdat.replace(/%/g,cact+sum2);
ladd=0;
lladd=0;
//lpara=0;
//llpara=0;
}
} else {
calculated=evil(iresdat);
if(false!==sum2) {
oresdat=iresdat;
try {
iresdat=calculated.toString();
} catch(e) {
iresdat="0";
}
cact="";
ladd=0;
lladd=0;
lpara=0;
llpara=0;
}
}
printCalc();
}
function clearhist() {
$('output_history').innerHTML="";
histidx=0;
}
var histidx=0;
var maxhist=50;
function printCalc() {
//var history=[];
//history.push(ores.innerHTML);
//$('output_history').insertAdjacentHTML('beforeend',oresdat+" = "+iresdat+"<br /><hr />");
if($('chist'+histidx)) {
$('chist'+histidx).parentNode.removeChild($('chist'+histidx));
}
if($('output_history').innerHTML=="") {
$('output_history').insertAdjacentHTML('beforeend',"<div style=\"font-size: 10pt;\"><a href=\"javascript:clearhist();\" target=\"_self\">[Clear history]</a></div>");
}
$('output_history').insertAdjacentHTML('beforeend',"<div id=\"chist"+histidx+"\">"+oresdat+" = "+iresdat+"</div>");
histidx++;
if(histidx>maxhist) {
histidx=0;
}
$('output_history_wrap').scrollTop=$('output_history_wrap').scrollHeight;
ores.innerHTML=(oresdat!="")?oresdat:" ";
ires.innerHTML=(iresdat!="")?iresdat:"0";
}
function updateo() {
if(iresdat!="") {
ores.innerHTML=iresdat;
} else {
ores.innerHTML=" ";
}
if(typeof calculated!=='undefined') {
ires.innerHTML=calculated.toString();
} else {
ires.innerHTML="0";
}
}
var cmode=0; // 0=insert, 1=addition/subtraction/multiply/divide = next insert calculates
var cact=""; // x / - +
var pmode="" // ( = 0 ) = 1
// percent and equal are active, back, paranthesis and dot are inserts
//
function actcheck() {
if(iresdat=="0") iresdat="";
var bool=false;
if(cact!="") {
//iresdat+=cact;
bool=true;
}
return bool;
}
function press_btn9() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="9";
if(exec) calculatei();
else updateo();
}
function press_btn8() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="8";
if(exec) calculatei();
else updateo();
}
function press_btn7() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="7";
if(exec) calculatei();
else updateo();
}
function press_btn6() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="6";
if(exec) calculatei();
else updateo();
}
function press_btn5() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="5";
if(exec) calculatei();
else updateo();
}
function press_btn4() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="4";
if(exec) calculatei();
else updateo();
}
function press_btn3() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="3";
if(exec) calculatei();
else updateo();
}
function press_btn2() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="2";
if(exec) calculatei();
else updateo();
}
function press_btn1() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="1";
if(exec) calculatei();
else updateo();
}
function press_btn0() {
var exec=actcheck();
exec=false;
cact="";
iresdat+="0";
if(exec) calculatei();
else updateo();
}
function press_btnAC() {
cact="";
iresdat="";
oresdat="";
cmode=0;
pmode="";
calculated=0;
ladd=0;
lladd=0;
lpara=0;
llpara=0;
//ires.innerHTML="";
//ores.innerHTML="";
calculatei();
}
function press_btnPara() {
//var exec=actcheck();
var exec=false;
if(pmode==0) {
if(cact!="") {
cact="";
iresdat+="(";
pmode=1;
} else if(iresdat=="" || iresdat=="0" || iresdat==" ") {
iresdat="(";
pmode=1;
}
} else {
cact="";
iresdat+=")";
pmode=0;
}
if(iresdat.length>lpara) {
llpara=lpara;
}
lpara=iresdat.length;
if(exec) calculatei();
else updateo();
}
function press_btnPer() {
//iresdat="%"+iresdat;
//updateo();
//calculatei();
if(ladd>0 && cact=="") {
var exec=actcheck();
if(exec) {
iresdat=iresdat.substr(0,iresdat.length-1);
}
//cact="%";
iresdat+="%";
if(pmode==1) {
//cact="";
iresdat+=")";
pmode=0;
if(iresdat.length>lpara) {
llpara=lpara;
}
lpara=iresdat.length;
}
calculatei();
}
//updateo();
}
function press_btnDiv() {
//updateo();
var exec=actcheck();
if(exec) {
iresdat=iresdat.substr(0,iresdat.length-1);
}
cact="/";
iresdat+="/";
if(iresdat.length>ladd) {
lladd=ladd;
}
ladd=iresdat.length;
//if(exec) calculatei();
updateo();
}
function press_btnMul() {
//updateo();
var exec=actcheck();
if(exec) {
iresdat=iresdat.substr(0,iresdat.length-1);
}
cact="*";
iresdat+="*";
if(iresdat.length>ladd) {
lladd=ladd;
}
ladd=iresdat.length;
//if(exec) calculatei();
updateo();
}
function press_btnSub() {
//updateo();
var exec=actcheck();
if(exec) {
iresdat=iresdat.substr(0,iresdat.length-1);
}
cact="-";
iresdat+="-";
if(iresdat.length>ladd) {
lladd=ladd;
}
ladd=iresdat.length;
//if(exec) calculatei();
updateo();
}
function press_btnAdd() {
//updateo();
var exec=actcheck();
if(exec) {
iresdat=iresdat.substr(0,iresdat.length-1);
}
cact="+";
iresdat+="+";
if(iresdat.length>ladd) {
lladd=ladd;
}
ladd=iresdat.length;
//if(exec) calculatei();
updateo();
}
function press_btnDot() {
//iresdat+=".";
//updateo();
//var exec=actcheck();
var exec=false;
if(iresdat.substr(iresdat.length-1)!=".") {
cact="";
iresdat+=".";
}
if(exec) calculatei();
else updateo();
}
function press_btnBackspace() {
//var exec=actcheck();
//cact="";
var exec=false;
cact="";
if(iresdat.length>0) {
var lch=iresdat.substr(iresdat.length-1,1);
if(lch==")") {
pmode=1;
//cact=lch;
lpara=iresdat.length-1;
llpara=iresdat.lastIndexOf("(");
} else if(lch=="(") {
pmode=0;
lpara=iresdat.length-1;
llpara=0;
}
iresdat=iresdat.substr(0,iresdat.length-1);
lch=iresdat.substr(iresdat.length-1,1);
if(lch=="*" || lch=="/" || lch=="+" || lch=="-") {
cact=lch;
ladd=iresdat.length-1;
lladd=0;
for(var i=iresdat.length-1;i>=0;i--) {
if(i<ladd) {
var ich=iresdat.substr(i,1);
if(ich=="*" || ich=="/" || ich=="+" || ich=="-") {
lladd=i;
break;
}
}
}
}
}
if(exec) calculatei();
else updateo();
}
function press_btnEqual() {
var exec=actcheck();
cact="";
calculatei();
}
$('btn_9').addEventListener("click",press_btn9);
$('btn_8').addEventListener("click",press_btn8);
$('btn_7').addEventListener("click",press_btn7);
$('btn_6').addEventListener("click",press_btn6);
$('btn_5').addEventListener("click",press_btn5);
$('btn_4').addEventListener("click",press_btn4);
$('btn_3').addEventListener("click",press_btn3);
$('btn_2').addEventListener("click",press_btn2);
$('btn_1').addEventListener("click",press_btn1);
$('btn_0').addEventListener("click",press_btn0);
$('btn_ac').addEventListener("click",press_btnAC);
$('btn_para').addEventListener("click",press_btnPara);
var ladd=0;
var lladd=0;
var lpara=0;
var llpara=0;
$('btn_per').addEventListener("click",press_btnPer);
$('btn_divid').addEventListener("click",press_btnDiv);
$('btn_multi').addEventListener("click",press_btnMul);
$('btn_subtr').addEventListener("click",press_btnSub);
$('btn_addit').addEventListener("click",press_btnAdd);
$('btn_dot').addEventListener("click",press_btnDot);
$('btn_back').addEventListener("click",press_btnBackspace);
$('btn_equal').addEventListener("click",press_btnEqual);
function evil(fn) {
try {
return new Function('return ' + fn)();
} catch(e) {
alert("error" + e);
return false;
}
}
Top