[
Thread Prev][
Thread Next] >
Date Index
>
Thread Index
C++ -> Perl
J.-C. Zeus -
Thu Jan 10 10:27:49 2002
Sie, isch haett' da gern a Problem...
Bin grad dabei, mir wenigstens ein paar Grundkenntnisse in C++ anzueignen (und
das freiwillig!!!), dabei bin ich auf folgendes kleines Prograemmsche
gestossen:
//: C03:CatsInHats.cpp
// Simple demonstration of recursion
#include <iostream>
using namespace std;
void removeHat( char cat ) {
for ( char c = 'A'; c < cat; c++ )
cout << " ";
if ( cat <= 'Z') {
cout << "cat " << cat << endl;
removeHat( cat + 1 ); // Recursive call
}
else
cout << "VOOM!!!" << endl;
}
int main() {
removeHat('A');
} ///:~
(Bruce Eckel: Thinking in C++, Volume One)
Wie wuerdet ihr das nach Perl uebersetzen? Ich habe zwar eine Loesung, finde
sie aber extrem unperlish:
#!/usr/bin/perl
sub removeHat {
my( $cat ) = @_;
for ( $c = ord 'A'; $c < $cat; $c++ ) { print " " }
if ( $cat <= ord 'Z') {
print "cat ", chr $cat, "\n";
removeHat( $cat + 1 ); # Recursive call
}
else { print "VOOM!!!\n" }
}
removeHat( ord 'A');
Hat jemand eine bessere Idee?
VG, Zeus.
Next: