Thread Anfänger Frage Taschenrechner: Erstellung einen Taschenrechners Anfänge (44 answers)
Opened by ASDS at 2007-01-29 12:57

renee
 2007-01-29 15:22
#73776 #73776
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Code: (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl

use strict;
use warnings;

my $formula = '((1 + 2) * 2) + (2 * 3)';

my %hash = ('*' => \&mal,
'-' => \&minus,
'+' => \&plus,
'/' => \&div,);


while($formula =~ s/\(([^\(]*?)\)/calc($1)/eg){
}

print calc($formula);

sub calc{
my ($part) = @_;
my ($op1,$op,$op2) = $part =~ m!(\d+)\s*([\*\+\/\-])\s*(\d+)!;
return 0 unless exists($hash{$op});
return $hash{$op}->($op1,$op2);
}

sub mal{
my ($op1,$op2) = @_;
return $op1 * $op2;
}

sub minus{
my ($op1,$op2) = @_;
return $op1 - $op2;
}

sub plus{
my ($op1,$op2) = @_;
return $op1 + $op2;
}

sub div{
my ($op1,$op2) = @_;
return $op1 / $op2;
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread Anfänger Frage Taschenrechner: Erstellung einen Taschenrechners Anfänge