Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #Author @roobre (Roberto Santalla)
- use strict;
- #--Código que he tomado prestado--------------------------------
- package List::Permutor;
- use strict;
- use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
- require Exporter;
- @ISA = qw(Exporter);
- @EXPORT = qw();
- $VERSION = '0.022';
- sub new {
- my $class = shift;
- my $items = [ @_ ];
- bless [ $items, [ 0..$#$items ] ], $class;
- }
- sub reset {
- my $self = shift;
- my $items = $self->[0];
- $self->[1] = [ 0..$#$items ];
- 1; # No useful return value
- }
- sub peek {
- my $self = shift;
- my $items = $self->[0];
- my $rv = $self->[1];
- @$items[ @$rv ];
- }
- sub next {
- my $self = shift;
- my $items = $self->[0];
- my $rv = $self->[1]; # return value array
- return unless @$rv;
- my @next = @$rv;
- # The last N items in @next (for 1 <= N <= @next) are each
- # smaller than the one before. Move those into @tail.
- my @tail = pop @next;
- while (@next and $next[-1] > $tail[-1]) {
- push @tail, pop @next;
- }
- # Then there's one more. Right?
- if (defined(my $extra = pop @next)) {
- # The extra one exchanges with the next larger one in @tail
- my($place) = grep $extra < $tail[$_], 0..$#tail;
- ($extra, $tail[$place]) = ($tail[$place], $extra);
- # And the next order is what you get by assembling the three
- $self->[1] = [ @next, $extra, @tail ];
- } else {
- # Guess that's all....
- $self->[1] = [];
- }
- return @$items[ @$rv ];
- }
- #--Fin del préstamo--------------------------------------------
- package main;
- sub factor {
- my @permutacion = @_;
- my $elementos = $#permutacion + 1;
- my $factor = 1;
- for(my $i = 0; $i < $elementos - 1; $i++){
- for(my $j = $i + 1; $j <= $elementos - 1; $j++){
- if($permutacion[$i] > $permutacion[$j]){
- $factor = $factor * -1;
- }
- }
- }
- return $factor;
- }
- sub printdet {
- my @determinante = @_;
- my $orden = $#determinante + 1;
- for(my $i = 0; $i < $orden; $i++) {
- print("| ");
- for(my $j = 0; $j < $orden; $j++) {
- print($determinante[$i][$j] . " ");
- }
- print("|");
- print("\n");
- }
- return 1;
- }
- sub calculardet {
- my @determinante = @_;
- my $orden = $#determinante + 1;
- my $valor;
- if ($orden == 1) {
- $valor = $determinante[0][0];
- } else {
- $valor = 0;
- my $fila = 1;
- my $factor;
- my $perm = new List::Permutor 0..$orden-1;
- while (my @set = $perm->next){
- $factor = &factor(@set);
- #print("Usando la permutación @set\n");
- #print("Factor $factor \n");
- for(my $i = 0; $i < $orden; $i++){
- my $permfila = $set[$i];
- $fila = $fila * $determinante[$i][$permfila];
- }
- $fila = $fila * $factor;
- #print("La \"fila\" vale $fila\n\n");
- $valor = $valor + $fila;
- $fila = 1;
- }
- }
- return $valor;
- }
- system("clear");
- print("\n==Cálculo del determinante de una matriz==\n");
- print("Ingresa el orden de la matriz: ");
- our $orden = <>;
- chomp($orden);
- my @determinante;
- print("\n");
- for (my $i = 0; $i < $orden; $i++){
- for (my $j = 0; $j < $orden; $j++){
- print("Introduce el elemento (");
- print($i+1);
- print(",");
- print($j+1);
- print("): ");
- $determinante[$i][$j] = <>;
- chomp($determinante[$i][$j]);
- }
- }
- #Mostrar el determinante. Saldrá descuadrado con números de dos dígitos =)
- print("\n");
- print("Determinante recibido: \n");
- &printdet(@determinante);
- print("\n\n");
- print("Valor del determinante: " . &calculardet(@determinante));
- print("\n\n");
- print("Presiona <Enter> para salir...");
- my $foo = <>;
- exit(0);
Advertisement
Add Comment
Please, Sign In to add comment