Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/licence_2/calculatrice/old/02.pl

(Deskargatu)
#!/usr/bin/perl -w

my $ans = 0;
my @func = (undef,"conv","add","sous","mul","div");



sub clear()
{
    print "\e[2J";	# clear screen 
    print "\e[1,1H";	# position 1,1
}


sub menu()
{
$rep = 0;
while($rep ne "6")
{
    print <<_EOF_MENU_;
    

    /\\_______.oOo. \e[1mCalculatrice Binaire\e[0m .oO__________/\\
    |                                                 |
    |   \e[1m1\e[0m     -   Convertir un entier en binaire      |
    |   \e[1m2\e[0m     -   Addition binaire                    |
    |   \e[1m3\e[0m     -   Soustraction binaire                |
    |   \e[1m4\e[0m     -   Multiplication binaire              |
    |   \e[1m5\e[0m     -   Division binaire                    |
    |   \e[1m6\e[0m     -   Sortir                              |    
    \\_________________________________________________/

    => CURRENT ANS : $ans    
    
_EOF_MENU_
my $rep = &lecture;
if ($rep) { &programme($rep); }
}
}



sub lecture()
{
    print "Veuillez entrer un choix entre 1 et 5 : ";
    my $entree = <STDIN>;
    chomp($entree);
    while ($entree !~ /^[12345]$/)
    {
	print "Choix incorrect \"$entree\" (de 1 a 5) : Veuillez reentrer un choix.\n";
	$entree = <STDIN>;
        chomp($entree);	
    }
    return $entree;

}


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]+$/);







}



sub conv()
{
    &clear;
    my  $num;
    my @arr;
    do
    {
        print "Entrer un entier : ";
        $num = <STDIN>;
	chomp($num);	
            }
        until ($num =~ /^\-?[0-9]+(?:\.[0-9]+)?$/);
    my $i=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;
    
    
    for ($puissance .. 22) { @bin = ("0",@bin); }
    @bin = reverse @bin;
    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);
    return $ans;
}
    





&clear();
&menu();