76 lines
2.7 KiB
HTML
76 lines
2.7 KiB
HTML
<!-- Manuel Vichi 3^AIN - Esercizio 1 -->
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Generatore Bit di Parita'</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
margin: 30px;
|
|
}
|
|
input, button {
|
|
padding: 10px;
|
|
margin: 10px 0;
|
|
}
|
|
label {
|
|
font-weight: bold;
|
|
}
|
|
.result {
|
|
margin-top: 20px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Generatore bit di parita'</h1>
|
|
|
|
<label for="num">Inserisci un numero in base 2 (in 7 bit):</label>
|
|
<input type="number" id="num" required>
|
|
<br><br>
|
|
<label for="paritypeer">Parita' pari</label>
|
|
<input type="radio" id="paritypeer" name="parity" value="paritypeer" checked>
|
|
<br>
|
|
<label for="paritypeer">Parita' dispari</label>
|
|
<input type="radio" id="paritynotpeer" name="parity" value="paritynotpeer">
|
|
<br><br>
|
|
<button onclick="generate()">Genera</button>
|
|
|
|
<div class="result">
|
|
<p><strong>Risultato parita':</strong> <span id="parityResult"></span></p>
|
|
</div>
|
|
<script>
|
|
function generate() {
|
|
let original = String(document.getElementById("num").value);
|
|
for (let i = 0; i < original.length; i++) {
|
|
if (original[i] != "0" && original[i] != "1") {
|
|
alert("Il numero deve essere binario!");
|
|
return;
|
|
}
|
|
}
|
|
let num = parseInt(original);
|
|
if (original.length != 7) {
|
|
alert("Il numero deve essere su 7 bit!");
|
|
return;
|
|
}
|
|
else {
|
|
let unProcioneInCalore = 0;
|
|
for (let i = 0; i < original.length; i++) {
|
|
if (original[i] == "0") {
|
|
unProcioneInCalore++;
|
|
}
|
|
}
|
|
if (document.getElementById('paritypeer').checked && unProcioneInCalore % 2 == 0) {
|
|
document.getElementById('parityResult').textContent = original + " 1";
|
|
}
|
|
else if (document.getElementById('paritynotpeer').checked && unProcioneInCalore % 2 != 0) {
|
|
document.getElementById('parityResult').textContent = original + " 1";
|
|
}
|
|
else {
|
|
document.getElementById('parityResult').textContent = original + " 0";
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |