vendredi 5 août 2016

Write a perl calculator

Hello I new to pearl and I learning the basics and the idea is write a calculator that can run, and perform the basic operations like multiplication, addition, ...Using as well RegEx (wich is new for me). Now the problem is that I'm getting 0 no matter what and I was wondering if any one can check the code and maybe tell me if I did something wrong also I will like to ask about chomp is it better than STDIN?

My Perl code is this:

sub only_number
{
        ($num) = @_;

        do{
                print "Type a Number: ";
                $num = <STDIN>;
                if($num =~ /\D+.*\n$/){
                        print "Thats is not a number. >:[ \n";
                }
        } until($num = $num + 0);
};
&only_number($num1);

do{
        print "What do you want to do? ";
        $operator = <STDIN>;
        if($operator !~ /\/|\*|\-|\+/){
                print "Thats not a valid character (-_-;).\n"
        }
} until ($operator =~ /\/|\*|\-|\+/);

&only_number($num2);

if($operator eq "*\n"){
        $result = $num1*$num2;
}
elsif($operator eq "/\n"){
        $result = $num1/$num2;
}
elsif($operator eq "+\n"){
        $result = $num1+$num2;
}
elsif($operator eq "-\n"){
        $result = $num1-$num2;
}
print "your answer is $result\n";

exit;

Am I missing something?

Aucun commentaire:

Enregistrer un commentaire