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.
精彩评论