C: Länge einer Strecke im Raum berechnen
Thursday, 18 June 2009
Aufgabenstellung:
2 Punkte (bestehend aus 3 Koordinaten) sollen vom Benutzer eingegeben werden können. Daraus soll dann die Länge einer Strecke im Raum berechnet werden.
Die Formel:

/** * Headerdatei fuer Streckenberechnung * Funktiondeklarierung * * Rainer Hihn */ #include#include /* * functions */ struct point readPoint(); float distance(struct point point1, struct point point2); /* * structs */ struct point { float rX; float rY; float rZ; };
/**
* Funktionen von 'Laenge einer Strecke im Raum'
*
* Rainer Hihn
*/
#include "strecke.h"
struct point readPoint()
{
struct point userPoint;
printf("Bitte x-Koordinate eingeben \n");
scanf("%62f", &userPoint.rX);
printf("Bitte y-Koordinate eingeben \n");
scanf("%62f", &userPoint.rY);
printf("Bitte z-Koordinate eingeben \n");
scanf("%62f", &userPoint.rZ);
return userPoint;
}
float distance(struct point point1, struct point point2)
{
float rNewX = 0;
float rNewY = 0;
float rNewZ = 0;
float rDistance = 0;
rNewX = point1.rX - point2.rX;
rNewY = point1.rY - point2.rY;
rNewZ = point1.rZ - point2.rZ;
rDistance = sqrt((rNewX * rNewX) + (rNewY * rNewY) + (rNewZ * rNewZ));
return rDistance;
}
* Laenge einer Strecke im Raum
*
* Rainer Hihn
*/
#include "strecke.h"
int main(void)
{
/**
* Variablendeklaration
*/
struct point firstPoint;
struct point secondPoint;
printf("Entfernung berechnen \n");
firstPoint = readPoint();
secondPoint = readPoint();
printf("Die Entfernung von Punkt 1 nach Punkt 2 ist: %f \n", distance(firstPoint, secondPoint));
return 0;
}