e ricominciano gli esercizi
scrivere un programma che stampi a video l'orario corrente in formato binario
ecco wikipedia in proposito:
http://it.wikipedia.org/wiki/Orologio_binario
http://it.wikipedia.org/wiki/Binary-coded_decimal
ed ecco la mia soluzione (dannato perl, perchè, PERCHÈnon esiste una funzione dec2bin ? :evil
ed ecco l'output:
scrivere un programma che stampi a video l'orario corrente in formato binario
ecco wikipedia in proposito:
http://it.wikipedia.org/wiki/Orologio_binario
http://it.wikipedia.org/wiki/Binary-coded_decimal
ed ecco la mia soluzione (dannato perl, perchè, PERCHÈnon esiste una funzione dec2bin ? :evil
Codice:
#!/usr/bin/perl
($s, $m, $h) = localtime();
if (length($h) == 1) {$h = "0".$h}
$q=$h;
$r=$h;
$h1=substr($h,0,1);
$h2=substr($h,1,1);
if (length($m) == 1) {$m = "0".$m}
$w=$m;
$t=$m;
$m1=substr($m,0,1);
$m2=substr($m,1,1);
if (length($s) == 1) {$s = "0".$s}
$e=$s;
$y=$s;
$s1=substr($s,0,1);
$s2=substr($s,1,1);
while ($h1>0) {
if ($h1 % 2 == 0) { $str1 .= 0; }
else { $str1 .= 1; }
$h1 = int($h1/2);
}
$str1 .= "0" while length($str1) < 4;
$str1 = reverse($str1);
while ($m1>0) {
if ($m1 % 2 == 0) { $str2 .= 0; }
else { $str2 .= 1; }
$m1 = int($m1/2);
}
$str2 .= "0" while length($str2) < 4;
$str2 = reverse($str2);
while ($s1>0) {
if ($s1 % 2 == 0) { $str3 .= 0; }
else { $str3 .= 1; }
$s1 = int($s1/2);
}
$str3 .= "0" while length($str3) < 4;
$str3 = reverse($str3);
while ($h2>0) {
if ($h2 % 2 == 0) { $str4 .= 0; }
else { $str4 .= 1; }
$h2 = int($h2/2);
}
$str4 .= "0" while length($str4) < 4;
$str4 = reverse($str4);
while ($m2>0) {
if ($m2 % 2 == 0) { $str5 .= 0; }
else { $str5 .= 1; }
$m2 = int($m2/2);
}
$str5 .= "0" while length($str5) < 4;
$str5 = reverse($str5);
while ($s2>0) {
if ($s2 % 2 == 0) { $str6 .= 0; }
else { $str6 .= 1; }
$s2 = int($s2/2);
}
$str6 .= "0" while length($str6) < 4;
$str6 = reverse($str6);
while ($q>0) {
if ($q % 2 == 0) { $str7 .= 0; }
else { $str7 .= 1; }
$q = int($q/2);
}
$str7 .= "0" while length($str7) < 6;
$str7 = reverse($str7);
while ($w>0) {
if ($w % 2 == 0) { $str8 .= 0; }
else { $str8 .= 1; }
$w = int($w/2);
}
$str8 .= "0" while length($str8) < 6;
$str8 = reverse($str8);
while ($e>0) {
if ($e % 2 == 0) { $str9 .= 0; }
else { $str9 .= 1; }
$e = int($e/2);
}
$str9 .= "0" while length($str9) < 6;
$str9 = reverse($str9);
@x1 = split//,$str1;
@x2 = split//,$str4;
@x3 = split//,$str2;
@x4 = split//,$str5;
@x5 = split//,$str3;
@x6 = split//,$str6;
@x7 = split//,$str7;
@x8 = split//,$str8;
@x9 = split//,$str9;
print "\n BCD encoding\n";
print " ^^^^^^^^^^^^\n\n";
print " $x2[0] $x4[0] $x6[0] \n";
print " $x2[1] $x3[1] $x4[1] $x5[1] $x6[1] \n";
print " $x1[2] $x2[2] $x3[2] $x4[2] $x5[2] $x6[2] \n";
print " $x1[3] $x2[3] $x3[3] $x4[3] $x5[3] $x6[3] \n\n";
print "\n True binary encoding\n";
print " ^^^^^^^^^^^^^^^^^^^^\n\n";
print " @x7\n @x8\n @x9\n\n";
print " Real time\n";
print " ^^^^^^^^^\n\n";
print " $r:$t:$y\n\n";
print " Il segnale orario vi è offerto da Oromis92\n\n";
ed ecco l'output: