Old stuff/ecole_etude_fac_de_pau/licence_2/calculatrice/backup2/tk.pl
(Deskargatu)
#!/usr/bin/perl -w
use Tk;
my $top = MainWindow->new;
my $ans = "0"x32;
my @func = (undef,"conv","add","sous","mul","div");
sub clear()
{
print "\e[2J"; # clear screen
print "\e[1,1H"; # position 1,1
}
sub menu()
{
$top->Button(
-text=>'Convertir en Binaire',
-command => sub { &conv },
)->pack;
$top->Button(
-text=>'Addition Binaire',
-command => sub { &add },
)->pack;
$top->Button(
-text=>'Soustraction Binaire',
-command => sub { &sous },
)->pack;
$top->Button(
-text=>'Multiplication Binaire',
-command => sub { &mul },
)->pack;
$top->Button(
-text=>'Division Binaire',
-command => sub { &div },
)->pack;
$top->Button(
-text=>'Exit',
-command => sub { exit },
)->pack;
$top->Label(
-textvariable => \$ans,
)->pack;
MainLoop;
}
sub programme($)
{
my $prop = shift;
$ans = &{$func[$prop]};
}
sub add()
{
&clear;
print "Addition avec $ans : \n";
do
{
print "Entrer un entier : ";
$num = <STDIN>;
chomp($num);
}
until ($num =~ /^[0-9]+$/);
return $num;
$ans = $num;
$top->update();
}
sub conv()
{
my $conv_win;
my @relief = qw/-relief sunken/;
my $num;
&clear;
# my $num;
my @arr;
do
{
$conv_win = MainWindow->new;
$conv_win->label('Entrer un nombre binaire : ');
$conv_win->button(
-text=>'Exit',
-command => sub { last }
);
$num = $conv_win->Entry(@relief)->pack;
}
until ($num =~ /^\-?[0-9]+(?:\.[0-9]+)?$/);
sub conv_real($)
{
$num = shift;
undef($virgule);
if ($num =~ /\.(\d+)$/) { $virgule = "0.".$1; }
else {$virgule = "0"; }
$signe = $num > 0 ? 0 : 1;
if ($signe) { $num*=-1; }
my @bin;
while ($num > 1)
{
push(@bin,($num%2));
$num=int($num/2);
}
push(@bin,$num);
my $puissance = (scalar @bin)-1;
@bin = reverse @bin;
my @virg;
for ($puissance .. 22)
{
$virgule = $virgule * 2;
if ($virgule >= 1) { $virgule -=1; @virg = ("1",@virg); }
else { @virg=("0",@virg); }
}
@bin = (@bin,reverse @virg);
shift(@bin);
my $exp=127+$puissance;
my $i="";
while($exp > 1)
{
$i=($exp % 2)."$i";
$exp=int($exp/2);
}
$i=$exp."$i";
$ans =$signe.$i.join("",@bin);
print "Signe $signe\n";
print "Expossant : $exp = $i ";
print "Mantisse ".join("",@bin)."\n";
return $ans;
$top->update();
}
&clear();
&menu();