-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkris.js
118 lines (114 loc) · 2.88 KB
/
kris.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function action1(a) {
document.getElementById("inpt").focus();
if (a=="D") {
document.getElementById("display1").innerHTML="";
document.getElementById("display2").innerHTML="";
document.getElementById("inpt").value="";
return;
}
var str1="0123456789.";
var str2=document.getElementById("inpt").value;
if (a=="C") {
str2=str2.substr(0,str2.length-1);
document.getElementById("inpt").value=str2;
return;
}
var cstr=document.getElementById("display1").innerHTML;
if (str1.includes(a)) {
if (a==".") {
if (str2.length>0) {
if (str2.includes(".")) {return;}
}
}
str2=str2 + a;
document.getElementById("inpt").value=str2;return;
} else {
if (str2==""){
if (cstr.substr(cstr.length-1,1)!="=") {
document.getElementById("display1").innerHTML=cstr.substr(0,cstr.length-1)+a;
return;
} else {
var str3=document.getElementById("display2").innerHTML;
if (str3!="") {
document.getElementById("display1").innerHTML=str3+a;
}
return;}
} else {
if (cstr.substr(cstr.length-1,1)=="=") {
document.getElementById("display1").innerHTML=str2+a;
document.getElementById("display2").innerHTML=str2;
document.getElementById("inpt").value="";
return;}
}
cstr=cstr+str2;
document.getElementById("display1").innerHTML=cstr+a;
cstr=cstr.replaceAll('√','~');
var ans=cstr.replaceAll(/[^\d.]/g, "#");
var numpart=ans.split("#");
ans=cstr.replaceAll(/[\d.]/g, "");
var mstring=numpart[0];
var mval=0;
for (var i=0;i<ans.length;i++ ) {
if (ans[i]=="^") {
mval=Math.pow(mstring,numpart[i + 1]);
mval=mval.toFixed(13);
cstr=cstr.replaceAll(mstring + "^" + numpart[i + 1],mval);
mstring=mval;
}
else if (ans[i]=="~") {
mval=Math.pow(mstring,1/numpart[i + 1]);
mval=mval.toFixed(13);
cstr=cstr.replaceAll(mstring + "~" + numpart[i + 1],mval);
mstring=mval;
} else {
mstring=numpart[i + 1];
}
}
ans = eval(cstr);
ans=ans.toFixed(13);
ans=parseFloat(ans);
document.getElementById("display2").innerHTML=ans;
document.getElementById("inpt").value="";
}
}
function myFunction (evt) {
var kCode = (evt.which) ? evt.which : evt.keyCode
if (kCode>95&&kCode<106) {
} else if (kCode==8) {
} else if (kCode==110) {
}
else if (kCode == 39) {action1("^");
evt.preventDefault();
return false;}
else if (kCode == 220) {action1("√");
evt.preventDefault();
return false;}
else if (kCode == 107) {action1("+");
evt.preventDefault();
return false;}
else if (kCode == 109) {action1("-");
evt.preventDefault();
return false;}
else if (kCode == 187) {action1("=");
evt.preventDefault();
return false;}
else if (kCode == 106) {action1("*");
evt.preventDefault();
return false;}
else if (kCode == 111) {action1("/");
evt.preventDefault();
return false;}
else if (kCode == 46) {action1("D");
evt.preventDefault();
return false;}
else if (kCode == 13) {action1("=");
evt.preventDefault();
return false;}
else if (kCode == 61) {action1("=");
evt.preventDefault();
return false;}
else {
evt.preventDefault();
return false;
}
}