PHP :: Aufgabe #27 :: Lösung #1
4 Lösungen

#27
Abstand zweier Punkte
Anfänger - PHP
von Dome
- 03.01.2013 um 01:09 Uhr
Schreiben Sie ein Programm, welches den Abstand zweier Punkte berechnet. Zuvor müssen die Koordinaten beider Punkte abgefragt werden.
Konsolenausgabe:
x1:1
y1:1
x2:2
y2:2
1.4142135623730951
#1

von bibir (1870 Punkte)
- 09.09.2014 um 12:05 Uhr

<?php function berechne(){ if(!(isset($_POST['x1']) && isset($_POST['y1']) && isset($_POST['x2']) && isset($_POST['y2']) && filter_var($_POST['x1'], FILTER_VALIDATE_FLOAT) && filter_var($_POST['x2'], FILTER_VALIDATE_FLOAT) && filter_var($_POST['y1'], FILTER_VALIDATE_FLOAT) && filter_var($_POST['y2'], FILTER_VALIDATE_FLOAT))){ return 'Nur Zahlenwerte angeben.'; } else { $erg =sqrt(pow(($_POST['x1']-$_POST['x2']),2) + pow(($_POST['y1']-$_POST['y2']),2)); return 'Abstand zwischen ('.$_POST['x1'].', '.$_POST['y1'].') und ('.$_POST['x2'].', '.$_POST['y2'].'): '.$erg; } } ?> <!DOCTYPE html> <html> <head> <title>Abstand berechnen</title> </head> <body> <form action="./abstand.php" name="abstand" method="POST"> <table> <tr> <td>x1:</td> <td><input type="text" name="x1" size="2" /></td> </tr> <tr> <td>y1:</td> <td><input type="text" name="y1" size="2" /></td> </tr> <tr> <td>x2:</td> <td><input type="text" name="x2" size="2" /></td> </tr> <tr> <td>y2:</td> <td><input type="text" name="y2" size="2" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="berechnen" /></td> </tr> </table> </form> <?php echo berechne(); ?> </body> </html>
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1