一个求和函数

2016-10-13 20:21:11来源:CSDN作者:g13731463274人点击

[root@mysql1 perl]# cat 25.pl 
#!/usr/bin/env perl


use strict;
use warnings FATAL => 'all';


sub total {
  my $sum;
  foreach (@_) {
    $sum+=$_;
  }
  $sum;
}


print "the numbers from 1 to 1000 add up to ",total(1..1000),"./n";


my @fred=qw{1 3 5 7 9};
my $fred_total=total(@fred);
print "the total of /@fred is $fred_total./n";
print "the total of (@fred) is $fred_total./n";


print "enter some numbers on separate lines: ";
my $user_total=total(<STDIN>);
print "the total of those numbers is $user_total./n";


[root@mysql1 perl]# ./25.pl   
the numbers from 1 to 1000 add up to 500500.
the total of @fred is 25.
the total of (1 3 5 7 9) is 25.
enter some numbers on separate lines: 1
2
3
the total of those numbers is 6.

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台