PHP :: Aufgabe #44
3 Lösungen

Matrizen um 90 Grad drehen
Anfänger - PHP
von pocki
- 11.11.2013 um 16:38 Uhr
Erstelle ein Programm, welches als Eingabe eine Matrix beliebiger Größe entgegennimmt und diese dann um 90 Grad dreht.
Beispiel an einer 4x4 Matrix:
Eingabematrix mit den Elementen:
a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44
sollte diese Ausgabematrix erzeugen:
a41 a31 a21 a11
a42 a32 a22 a12
a43 a33 a23 a13
a44 a34 a24 a14
Das Programm sollte für unterschiedlich großen und auch mit nicht quadratischen Matrizen funktionieren.
Beispiel an einer 4x4 Matrix:
Eingabematrix mit den Elementen:
a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44
sollte diese Ausgabematrix erzeugen:
a41 a31 a21 a11
a42 a32 a22 a12
a43 a33 a23 a13
a44 a34 a24 a14
Das Programm sollte für unterschiedlich großen und auch mit nicht quadratischen Matrizen funktionieren.
Lösungen:

<?php if(!isset($_GET['step'])){ $step = 1; } else { $step = $_GET['step']; } function print_step(){ global $step; $disp = ''; switch($step){ case 2: $disp = get_matrix_form(); break; case 3: $disp = display_rotation(); break; default: $disp = get_size_form(); } return $disp; } function get_size_form(){ $disp = 'Bitte die Größe der Matrix angeben: <form name="sizes" action="./matrix_drehen.php?step=2" method="POST"> <table> <tr> <th>Zeilen:</th> <td><input type="text" name="rows" /></td> </tr> <tr> <th>Spalten:</th> <td><input type="text" name="cols" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="weiter" /></td> </tr> </table> </form>'; return $disp; } function get_matrix_form(){ $disp = 'Bitte die '.$_POST['rows'].'x'.$_POST['cols'].' Matrix eingeben: <form name="sizes" action="./matrix_drehen.php?step=3&r='.$_POST['rows'].'&c='.$_POST['cols'].'" method="POST"> <table>'; for($i = 0; $i < $_POST['rows']; $i++){ $disp .= '<tr>'; for($j = 0; $j < $_POST['cols']; $j++){ $disp .= '<td><input type="text" name="m_'.$i.'_'.$j.'" size="2"/></td>'; } $disp .= '</tr>'; } $disp .= ' <tr> <td colspan="'.$_POST['cols'].'"><input type="submit" value="weiter" /></td> </tr> </table> </form>'; return $disp; } function display_rotation(){ $matrix = array(); // matrix einlesen for($i = 0; $i < $_GET['r']; $i++){ for($j = 0; $j < $_GET['c']; $j++){ $matrix[$i][$j] = $_POST['m_'.$i.'_'.$j]; } } // rotieren $new_matrix = rotate_matrix($matrix); // ausgeben $disp = '<table>'; for($i = 0; $i < $_GET['c']; $i++){ $disp .= '<tr>'; for($j = 0; $j < $_GET['r']; $j++){ $disp .= '<td>'.$new_matrix[$i][$j].'</td>'; } $disp .= '</tr>'; } $disp .= '</table>'; return $disp; } function rotate_matrix($matrix){ $new_matrix = array(); for($i = 0; $i < $_GET['r']; $i++){ for($j = 0; $j < $_GET['c']; $j++){ $new_matrix[$j][$i] = $matrix[$i][$j]; } } return $new_matrix; } ?> <!DOCTYPE html> <html> <head> <title>Primzahlen</title> </head> <body> <p> <?php echo print_step(); ?> </p> </body> </html>

<?php $zwischending; $matrix = array(); $matrix[0][0] = 'a11'; $matrix[0][1] = 'a12'; $matrix[0][2] = 'a13'; $matrix[0][3] = 'a14'; $matrix[1][0] = 'a21'; $matrix[1][1] = 'a22'; $matrix[1][2] = 'a23'; $matrix[1][3] = 'a24'; $matrix[2][0] = 'a31'; $matrix[2][1] = 'a32'; $matrix[2][2] = 'a33'; $matrix[2][3] = 'a34'; $matrix[3][0] = 'a41'; $matrix[3][1] = 'a42'; $matrix[3][2] = 'a43'; $matrix[3][3] = 'a44'; foreach ($matrix as $key) { foreach ($key as $inhalt) { echo $inhalt."__"; } echo "<br>"; } echo "<br><br>"; $y = 4; for($i = 0; $i < 2; $i++) { $y--; $zwischending = $matrix[$i][$i]; $matrix[$i][$i] = $matrix[$y][$i]; $matrix[$y][$i] = $matrix[$y][$y]; $matrix[$y][$y] = $matrix[$i][$y]; $matrix[$i][$y] = $zwischending; } for($i = 1; $i < 3; $i++) { $zwischending = $matrix[0][$i]; $matrix[0][$i] = $matrix[$y][0]; $matrix[$y][0] = $matrix[count($matrix) - 1][$y]; $matrix[count($matrix) - 1][$y] = $matrix[$i][count($matrix) - 1]; $matrix[$i][count($matrix) - 1] = $zwischending; $y--; } foreach ($matrix as $key) { foreach ($key as $inhalt) { echo $inhalt."__"; } echo "<br>"; } ?>

<?php function printField(array $array) { foreach($array as $line) { foreach($line as $item) { echo $item .' '; } echo "\n"; } echo "\n"; } function flipField(array $array) { return array_map('array_reverse', array_map(null, ...$array)); } $field = [ [ 0 , 0 , 0 ], [ 1 , 1 , 1 ], [ 2 , 2 , 2 ] ]; printField($field); $field = flipField($field); printField($field); $field = [ [ 0 , 0 , 0, 0 ], [ 1 , 1 , 1, 1 ], [ 2 , 2 , 2, 2 ] ]; printField($field); $field = flipField($field); printField($field); $field = [ [ 0 , 0 ], [ 1 , 1 , 1 ], [ 2 , 2 , 2, 2 ] ]; printField($field); $field = flipField($field); printField($field);
Konsolenausgabe:
0 0 0
1 1 1
2 2 2
2 1 0
2 1 0
2 1 0
0 0 0 0
1 1 1 1
2 2 2 2
2 1 0
2 1 0
2 1 0
2 1 0
0 0
1 1 1
2 2 2 2
2 1 0
2 1 0
2 1
2