#!/usr/bin/perl use strict; use warnings; use Carp (); local $SIG{__WARN__} = \&Carp::cluck; # affiche le caractère $c sub afficheCaractere# char $c { my ($c) = @_; print "$c "; } # affiche $n fois le caractère $c # ne revient pas à la ligne après. sub ligneSansReturn# int $n, int $c { my ($n, $c) = @_; for (my $i = 0 ; $i < $n ; $i++) { afficheCaractere ($c); } } # affiche $n fois le caractère $c # revient à la ligne après. sub ligneAvecReturn# int $n, int $c { ligneSansReturn (@_); print "\n" } # affiche n espaces, revient à la ligne après. sub nEspaces# int $n { ligneSansReturn (@_, " "); } # affiche $c à la colonne $n, affiche des espaces avant. # ne revient pas à la ligne après # les indices commencent à 1. sub unCaractereSansReturn# int $n, int $c { my ($n, $c) = @_; nEspaces ($n - 1); afficheCaractere ($c); } # affiche $c à la colonne $n, affiche des espaces avant. # revient à la ligne après sub unCaractereAvecReturn# int $n, int $c { unCaractereSansReturn (@_); print "\n"; } # affiche $c aux colonnes $i et $j # revient à la ligne après. sub deuxCaracteres# int $i, int $j, char $c { my ($i, $j, $c) = @_; unCaractereSansReturn ($i, $c) ; unCaractereAvecReturn ($j - $i, $c); } # affiche un carré de coté $n. sub carre# int $n { my ($n) = @_; ligneAvecReturn ($n, "*"); for (my $i = 1 ; $i < $n ; $i++) { deuxCaracteres (1, $n, "*"); } ligneAvecReturn ($n, "*"); } # affiche un chapeau dont la pointe (non affichée) # est sur la colonne $centre en utilisant des $c. sub chapeau# int $centre, char $c. { my ($centre, $c) = @_; for (my $i = $centre - 1, my $j = $centre + 1 ; $i > 1; $i--, $j++) { deuxCaracteres($i, $j, $c); } } # affiche un chapeau retourné dont la pointe (non affichée) # est sur la colonne $centre en utilisant des $c. sub chapeauInverse# int $centre, char $c. { my ($centre, $c) = @_; for (my $i = 2, my $j = 2 * ($centre - 1) ; $i < $centre; $i++, $j--) { deuxCaracteres($i, $j, $c); } } # affiche un losange de coté $n. sub losange# int $n { my ($n) = @_; unCaractereAvecReturn($n, "*"); chapeau($n, "*"); deuxCaracteres(1, 2*$n - 1, "*"); chapeauInverse($n, "*"); unCaractereAvecReturn($n, "*"); } # affiche un losange de coté $n. sub croix# int $n { my ($n) = @_; deuxCaracteres(1, 2*$n - 1, "*"); chapeauInverse($n, "*"); unCaractereAvecReturn($n, "*"); chapeau($n, "*"); deuxCaracteres(1, 2*$n - 1, "*"); } my $n = 5; $n = $ARGV[0] if (defined($ARGV[0])); carre $n; losange $n; croix $n;