Thread Script zur Berechnung des Pythagoras? (17 answers)
Opened by ulix at 2005-04-06 16:43

Ronnie
 2005-04-07 00:28
#53477 #53477
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Dies wäre eine Möglichkeit:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl

use strict;
use warnings;

our %opts;
my $lookup;

for (@ARGV) {$opts{$1}=$2 if (/([ABC]{1})=(\d+\.?\d*)/)}
$lookup = join '', sort keys %opts;
die "I need two sides!\n" unless (length($lookup)==2);
my $lut = {
'AB' => sub {return 'C='.sqrt($opts{A}**2+$opts{B}**2)},
'AC' => sub {return 'B='.sqrt($opts{C}**2-$opts{A}**2)},
'BC' => sub {return 'A='.sqrt($opts{C}**2-$opts{B}**2)},
};
print &{$lut->{$lookup}}, "\n";

EDIT1: Elegantere Version eingestellt.
EDIT2: Perl-Tags durch Code-Tags ersetzt.
EDIT3: Abbruch bei mehr oder weniger als zwei Seiten des Dreiecks\n\n

<!--EDIT|Ronnie|1112859654-->

View full thread Script zur Berechnung des Pythagoras?