C++ :: Aufgabe #28
5 Lösungen
Abstand zweier Punkte
Anfänger - C++
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
Lösungen:
#include <iostream>
#include <math.h>
using namespace std;
// prototypen
double hypotenuse(double ankathete, double gegenkathete);
int main()
{
double x1 = 0.0, y1 = 0.0, x2 = 0.0, y2 = 0.0;
cout << "Punkt 1:" << endl;
cout << "x-position: ";
cin >> x1;
cout << "y-position: ";
cin >> y1;
cout << "Punkt 2:" << endl;
cout << "x-position: ";
cin >> x2;
cout << "y-position: ";
cin >> y2;
cout << hypotenuse(x2 - x1, y2 - y1) << endl;
system("pause");
return 0;
}
double hypotenuse(double ankathete, double gegenkathete)
{
return (sqrt(ankathete * ankathete + gegenkathete * gegenkathete));
}gruß
coco
#include <iostream>
void main()
{
long double x, y, a, b;
std::cout << "x1:";
std::cin >> x;
std::cout << "y1:";
std::cin >> y;
std::cout << "x2:";
std::cin >> a;
std::cout << "y2:";
std::cin >> b;
std::cout << sqrt( (x-a)*(x-a) + (y-b) * (y-b) ) << std::endl;
}
Eine schnelle Lösung findet sich durch ein wenig C++11 und einem kleinen, extra dafür entworfenen Klassentemplate Point:
C-Code
Zu beachten ist, dass dies eine IMO hässliche Variante ist. Man könnte alles sehr viel allgemeiner und flexibler gestalten.
#include <iostream>
#include <cmath>
#include <iterator>
template<typename T>
T square(T a) { return a*a; }
template<typename T>
struct Point
{
T x, y;
static Point from_command_line()
{
#define do_for(what) (std::cout << #what ": ", *std::istream_iterator<T>(std::cin))
return { do_for(x), do_for(y) };
}
double distance_to(Point const& p)
{
return ::sqrt( square( ::abs(p.y - y) ) + square( ::abs(p.x - x) ) );
}
};
#include <iomanip>
#include <limits>
int main()
{
auto p1 = Point<int>::from_command_line(),
p2 = Point<int>::from_command_line();
std::cout << "distance: " << std::setprecision( std::numeric_limits<double>::digits10 ) << std::fixed << p1.distance_to(p2);
}Konsolenausgabe:
x: 1
y: 1
x: 2
y: 2
distance: 1.414213562373095
Zu beachten ist, dass dies eine IMO hässliche Variante ist. Man könnte alles sehr viel allgemeiner und flexibler gestalten.
#include <iostream>
using namespace std;
double Abstand(double x1,double x2,double y1,double y2);
int main (void)
{
double x1,x2,y1,y2;
cout<<"x1: ";
cin>>x1;
cout<<"x2: ";
cin>>x2;
cout<<"y1: ";
cin>>y1;
cout<<"y2: ";
cin>>y2;
cout<<"\n";
cout<<Abstand(x1,x2,y1,y2)<<endl;
system("Pause");
}
double Abstand(double x1,double x2,double y1,double y2)
{
double ergebnis;
return ergebnis=sqrt(pow((x2-x1),2)+pow((y2-y1),2));
}
#include <iostream>
#include <cmath>
using namespace std;
struct Point {
float x;
float y;
};
int main() {
Point p1, p2;
cout << "x1: ";
cin >> p1.x;
cout << "y1: ";
cin >> p1.y;
cout << "x2: ";
cin >> p2.x;
cout << "y2: ";
cin >> p2.y;
cout << "\nAbstand: " << sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2)) << "\n";
}