Thread Perl Rechner (12 answers)
Opened by MauriZze at 2008-06-16 21:07

murphy
 2008-06-16 22:14
#111094 #111094
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
Mein Vorschlag:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/local/bin/perl
use strict;
use warnings;

my %operators = (
  '+' => sub { $_[0] + $_[1] },
  '-' => sub { $_[0] - $_[1] },
  '*' => sub { $_[0] * $_[1] },
  '/' => sub { $_[0] / $_[1] }
);

$|++; # Flush output automatically

print 'First operand:  ';
chomp (my $operand1 = <>);

print 'Operator:       ';
chomp(my $symbol = <>);

print 'Second operand: ';
chomp(my $operand2 = <>);

die "Unknown operator $symbol\n" if (!exists $operators{$symbol});

print "----------------\nResult:         ",
      $operators{$symbol}->($operand1, $operand2),
      "\n";
When C++ is your hammer, every problem looks like your thumb.

View full thread Perl Rechner