-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (104 loc) · 3.05 KB
/
Copy pathscript.js
File metadata and controls
132 lines (104 loc) · 3.05 KB
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
function to_binary(number){
return (number).toString(2);
}
function to_decimal(binary){
return parseInt(binary, 2);
}
function extend_to_bits(binary, bits = 32){
var result = bits - binary.length;
var zero_fill = "0".repeat(result);
return zero_fill+binary;
}
function to_binaryC2(number, bits = 32){
if (number >= 0){
number = to_binary(number);
return extend_to_bits(number, bits);
}
else{
number = Math.pow(2, bits) + number;
number = to_binary(number);
return extend_to_bits(number, bits);
}
}
function to_decimalC2(binary){
bits = binary.length;
decimal = to_decimal(binary);
if (binary[0] == '0'){
return decimal;
}
else{
return -(Math.pow(2, bits)) + decimal;
}
}
f1.addEventListener("submit", verifyNumber);
function verifyNumber(e){
var bits = parseInt(document.f1.bits.value);
if (bits > 32 || bits <2){
alert("Valor Máximo = 32 e Valor minímo = 2");
document.f1.bits.value = 32;
bits = 32;
}
var b = bits -1;
var num = parseInt(document.f1.num.value);
if ((num >= Math.pow(2,b) || (num < - Math.pow(2,b)))){
alert("Número Inválido!");
}
else{
updateHist(num, bits);
writeResult(num, bits);
}
e.preventDefault();
};
function updateHist(num, bits){
var binNum = to_binaryC2(num, bits);
var tr = document.createElement("tr");
var tdN = document.createElement("td");
var tdB = document.createElement("td");
tr.setAttribute("class", "trHist");
tdN.setAttribute("class", "tdN");
tdB.setAttribute("class", "tdB");
var nodeN = document.createTextNode(num);
var nodeB = document.createTextNode(binNum);
tdN.appendChild(nodeN);
tdB.appendChild(nodeB);
tr.appendChild(tdN);
tr.appendChild(tdB);
var data = document.getElementById("tData");
data.appendChild(tr);
}
function writeResult(num, bits){
var binNum = to_binaryC2(num, bits);
var pRes = document.getElementById("pRes");
pRes.innerHTML = "Resultado:"+'\n'+binNum;
};
// seqGen = document.getElementById("bt1");
fSeq.addEventListener("submit", writeHist);
function writeHist(e){
var min = parseInt(document.fSeq.min.value);
var max = parseInt(document.fSeq.max.value);
var bits = parseInt(document.f1.bits.value)
for(var i = min; i <= max; i++){
updateHist(i,bits);
}
e.preventDefault();
};
function clearHist(){
document.getElementById("tData").innerHTML = "";
}
function clearItem(){
alert(document.getElementById("tData").target.id);
}
fToDec.addEventListener("submit", writeDec);
function writeDec(e){
var bin = document.fToDec.binValue.value;
var len = bin.length;
var number = to_decimalC2(bin);
resToDec = document.getElementById("pToDec");
if (bin == to_binaryC2(number,len)) {
pToDec.innerHTML = "resultado:"+"<br>"+number;
}
else {
pToDec.innerHTML = "O número que você digitou não é um número binário";
}
e.preventDefault();
}