1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
double calcDistance(double x1, double y1, double z1, double x2, double y2, double z2) {
double sqr =((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
double dist = (sqrt(sqr));
return dist;
}
int main() {
double x1, x2, y1, y2, z1, z2;
std::vector<double> dis;
ifstream fin("input.txt");
ofstream fout("output.txt");
double distance;
while (fin.good()) {
fin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
distance = calcDistance(x1, y1, z1, x2, y2, z2);
dis.push_back(distance);
fout << x1 << " " << y1 << " " << z1 << " " << x2 << " " << y2 << " " << z2 << " distance = " << distance << endl;
}
sort(dis.begin(), dis.end());
ofstream fout2("output_sorted.txt");
for (vector<double>::iterator itr = dis.begin(); itr != dis.end(); ++itr)
fout2 << *itr <<endl;
return 0;
}
|