开发者

Changing from c++ to perl

I have unusual problem, there's no perl chat room so i decided开发者_开发知识库 to ask little questions here.

 #include <cstdio> 

 double end, tmp; 
 int test; 

 int main(void) {
     scanf("%d", &test);
     for (int i = 0; i < test; i++) {
         scanf("%*s%*s%lf", &tmp);
         end += tmp;
     }
     end /= (double)test;
     printf("%.2lf", end); 
     return 0;
 }

I have this little C++ code. How to write in Perl something like this ? I mean how to read string and ignore it and how to read double numbers and output them.


The following code is roughly equivalent to yours:

my $divisor = <STDIN>;
chomp $divisor;

my $dividend = 0;

while (<STDIN>) {
  my ($ignore1, $ignore2, $term) = split;
  chomp $term;
  $dividend += $term;
}

print( ($dividend / $divisor) . $/ );

It of course doesn't check for proper input, divide by zero or anything like that, but neither does the original code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜