1: #!/usr/bin/perl -w
2: # i know everyone has written a calculator.
3: # but im proud of this.
4: # its the first useful thing ive written.
5: # here we go
6:
7:
8: use strict;
9:
10: my $operation;
11: my $int;
12: my $int2;
13: my $answer;
14: my $runagain;
15:
16: calculator();
17:
18: sub calculator {
19: print "What operation (Add, Sub, Mult, Divd) do you want to do? ";
20: chomp($operation = <>);
21: print "Enter the first number: ";chomp($int = <>);
22: print "Enter the second number: ";chomp($int2 = <>);
23: if ($operation =~ /^a/) {
24: $answer = $int + $int2;
25: } elsif ($operation =~ /^s/) {
26: $answer = $int - $int2;
27: } elsif ($operation =~ /^m/) {
28: $answer = $int * $int2;
29: } elsif ($operation =~ /^d/) {
30: $answer = $int / $int2;
31: }
32: print "The answer is $answer\n";
33: print "Do you want to run this again? ";chomp($runagain = <>);
34: if ($runagain =~ /^y/) {
35: calculator();
36: } else {
37: print "goodbye\n";
38: }
39: }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Calculator
by merlyn (Sage) on Mar 09, 2001 at 04:40 UTC | |
|
Re: Perl Calculator
by damian1301 (Curate) on Mar 09, 2001 at 05:49 UTC | |
|
Re: Perl Calculator
by Desdinova (Friar) on Mar 09, 2001 at 10:18 UTC | |
|
Re: Perl Calculator
by -> (Initiate) on Mar 30, 2001 at 01:13 UTC |