-
Notifications
You must be signed in to change notification settings - Fork 2
/
calcul.js
185 lines (168 loc) · 4.07 KB
/
calcul.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var stock = document.getElementById("display");
var egal = document.getElementById("=");
var cuurentChar;
var perviouseChar;
var operations = ["+", "-", "*", "/", "%", "²"];
var operationsSpecial = ["*", "/", "%", "²"];
var alphabet = ["a","z","e","r","t","y","i","o","q","s","d","f","g","h","h","j","k","l","m","w","x","c","v","n"]
var operationsNumber = [
"+",
"-",
"*",
"/",
"%",
"²",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
];
var ans;
var care;
var content = document.getElementsByTagName("body")[0];
var darkMode = document.getElementById("dark-change");
let trad = document.querySelector("select")
let age = document.querySelector('#age')
let bin = document.querySelector('#bin')
let roman = document.querySelector('#ra')
let anss = document.querySelector('#Ans')
let factt = document.querySelector('#fact')
darkMode.addEventListener("click", function () {
darkMode.classList.toggle("active");
content.classList.toggle("night");
});
document.addEventListener("keydown", function (e) {
if (operationsNumber.includes(e.key)) {
console.log(e.key)
stock.value = stock.value + e.key;
}
else{
stock.value = remove(stock.value)
}
});
document.addEventListener('keydown', function(e) {
if(e.key == "Enter"){
console.log(stock.value)
egal.onclick()
}
});
function mo9abil(){
stock.value = -(stock.value)
}
function clearStock() {
stock.value = "";
}
function clearoneStock() {
stock.value = stock.value.slice(0, -1);
}
function addNumber(c) {
stock.value += c;
numChar = stock.value.length;
cuurentChar = c;
getPreviousChar();
}
function clalculate(){
stock.value = eval(stock.value)
ans= stock.value;
if (stock.value == "Infinity") {
stock.value = "Cannot divide by zero";
}
}
function historique() {
stock.value = ans;
}
function getPreviousChar() {
perviouseChar = stock.value.substring(numChar - 2, numChar - 1);
checkSyntax();
}
function checkSyntax() {
if (operationsSpecial.includes(cuurentChar) && numChar == 1) {
removeChar();
}
if (operations.includes(perviouseChar) && operations.includes(cuurentChar)) {
if (perviouseChar == cuurentChar) {
removeChar();
} else {
overwrite();
}
}
}
function removeChar() {
stock.value = stock.value.substring(0, numChar - 1);
}
function overwrite() {
stock.value =
stock.value.slice(0, numChar - 2) + stock.value.slice(numChar - 1);
}
function carre() {
care = stock.value.slice(0, numChar - 2);
stock.value = Math.pow(care, 2);
ans = stock.value;
}
function binaire() {
let N = stock.value;
let binary = "";
while (N !== 0) {
let r = N % 2;
N = Math.floor(N / 2);
binary = r + binary;
}
stock.value = binary;
}
function Age() {
var d = new Date();
var year = d.getFullYear();
NewAge = year - stock.value;
stock.value = "Votre Age ==> " + NewAge;
}
function racine() {
stock.value = Math.sqrt(stock.value);
}
function fact() {
var f = 1;
for (var i = 1; i <=stock.value ; i++) {
f = i * f;
}
stock.value = "Le factorielle est ==>"+f;
}
function romai() {
let t = [[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"],
[100, "C"], [90, "XC"], [50, "L"], [40, "XL"],
[10, "X"], [5, "V"], [4, "IV"], [1, "I"]];
let ts = [];
for (let i = 0; i < t.length; i++) {
let num = t[i][0];
let ch = t[i][1];
while (stock.value >= num) {
stock.value -= num;
ts.push(ch);
}
}
stock.value = ts.join("");
}
function traduction() {
if (trad.selectedIndex == 0) {
age.innerHTML="Age"
bin.innerHTML="Bin"
roman.innerHTML="Roman"
factt.innerHTML="Fact"
anss.innerHTML="Ans"
}
if (trad.selectedIndex == 1) {
age.innerHTML="العمر"
bin.innerHTML="بينير"
roman.innerHTML="رومان"
factt.innerHTML="عاملي"
anss.innerHTML="سجل"
}
}
addEventListener("change",()=>{
traduction()
}
)