PHP :: Aufgabe #44 :: Lösung #1

3 Lösungen Lösungen öffentlich
#44

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.
#1
vote_ok
von bibir (1870 Punkte) - 03.09.2014 um 11:34 Uhr
Quellcode ausblenden PHP-Code
<?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>

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben