forked from cripi-interface-development/dz4-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (63 loc) · 2.86 KB
/
script.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
var f; //ôëàã ïîÿâëåíèÿ îïåðàòîðà
var x; //ââîäèìûé ñèìâîë
var a1; //ïåðâûé îïåðàíä
var a2; //âòîðîé îïåðàíä
var op; //îïåðàòîð
var eq; //ôëàã íàæàòîãî "="
var symbl1; //êîëè÷åñòâî çíàêîâ â ïåðâîì îïåðàíäå
var symbl2; //êîëè÷åñòâî çíàêîâ âî âòîðîì îïåðàíäå
var res=0;
var mes='Ïðèâåò! ß êàëüêóëÿòîð, íàïèñàííûé Îëåé Ëåâèòñêîé =)';
$(document).ready(function() {
$('div').click(function() {
x = $(this).attr('id'); //ïðèñâàèâàåì êàæäîé êíîïî÷êå ñâîé èäåíòèôèêàòîð
if (x=="i0") x=0;
if (x=="i1") x=1;
if (x=="i2") x=2;
if (x=="i3") x=3;
if (x=="i4") x=4;
if (x=="i5") x=5;
if (x=="i6") x=6;
if (x=="i7") x=7;
if (x=="i8") x=8;
if (x=="i9") x=9;
if (x=="sum") x=10;
if (x=="sub") x=11;
if (x=="mul") x=12;
if (x=="dev") x=13;
if (x=="del") x=14;
if (x=="calc") x=15;
if (!isFinite(f)){ //ïðîâåðÿåì, ÷òî åù¸ íå âñòðåòèëè îïåðàòîð, çíà÷èò âñ¸ ýòî ïåðâîå ÷èñëî
if(!isFinite(a1)) {
if (x==14){document.getElementById("expression").innerHTML=""; document.getElementById("result").innerHTML=""; document.getElementById("message").innerHTML=mes;}
if (x<10){a1=x; symbl1=1; eq=0; document.getElementById("expression").innerHTML=a1; document.getElementById("result").innerHTML=""; }
}
else {
if (x<10 && symbl1<8) {a1=a1*10+x; symbl1++; eq=0; document.getElementById("expression").innerHTML=a1;}
else{
if (x==10) {f=1; op=x; eq=0; document.getElementById("expression").innerHTML+="+";}
if (x==11) {f=1; op=x; eq=0; document.getElementById("expression").innerHTML+="-";}
if (x==12) {f=1; op=x; eq=0; document.getElementById("expression").innerHTML+="*";}
if (x==13) {f=1; op=x; eq=0; document.getElementById("expression").innerHTML+="/";}
if (x==14) {f=NaN; a1=NaN; eq=0; document.getElementById("expression").innerHTML=""; document.getElementById("message").innerHTML=mes;}
}
}
}
else{ //òàê êàê îïåðàòîð óæå áûë, çíà÷èò ïîøëî âòîðîå ÷èñëî
if (x>10 && x!=15 && eq!=1) {document.getElementById("message").innerHTML="Sorry! Òåïåðü íàäî ââåñòè âòîðîå ÷èñëî"}
if(!isFinite(a2) && (x<10)) {a2=x; symbl2=1; eq=1; document.getElementById("expression").innerHTML+=a2; document.getElementById("message").innerHTML="×òîáû óçíàòü îòâåò, æìè ="}
else {
if (x<10 && symbl2<8) {a2=a2*10+x; eq=1; symbl2++; document.getElementById("expression").innerHTML+=x;}
if (x==14) {f=NaN; a1=NaN; a2=NaN; eq=0; document.getElementById("expression").innerHTML=""; document.getElementById("message").innerHTML=mes;}
}
}
if(x==15 && eq==1){ //äîïîëíèòåëüíàÿ ïðîâåðêà íà òî, ÷òî "=" íàæàëè ïîñëå âòîðîãî îïåðàíäà
if (op==10) {f=NaN; res=a1+a2; a1=NaN; a2=NaN; eq=0;}
if (op==11) {f=NaN; res=a1-a2; a1=NaN; a2=NaN; eq=0;}
if (op==12) {f=NaN; res=a1*a2; a1=NaN; a2=NaN; eq=0;}
if (op==13) {f=NaN; res=a1/a2; a1=NaN; a2=NaN; eq=0;}
document.getElementById("result").innerHTML='='+res;
document.getElementById("message").innerHTML="ïðàâäà êëàññíî? =)"
}
});
});