在此程序中,您输入点的坐标及其速度分量。它应该在输出中给出最接近(0,0,0)的点,以及它与(0,0,0)的距离。这是该计划的主体:
#include "tacka4.h"
#include "vektor1.h"
#include "brzina.h"
#include <iostream>
using namespace std;
int main () {
cout << "Broj tacaka? "; int n; cin >> n;
Tacka** tacke = new Tacka* [n];
for (int i=0; i<n; i++) {
cout << "Koordinate tjemena " << i << "? ";
double x, y, z; cin >> x >> y >> z;
cout << "Komponente brzine? ";
double vx, vy, vz; cin >> vx >> vy >> vz;
tacke[i] = new Tacka (Vektor (x, y, z), Brzina (vx, vy, vz));
}
cout << "\nBroj koraka? "; int k; cin >> k;
cout << "Trajanje koraka? "; double dt; cin >> dt;
const Tacka org;
cout << "ORG " << org << "\n\n";
for (int i=0; i<k; i++) {
for (int j=0; j<n; tacke[j++]->proteklo(dt));
double min = rastojanje (org, *tacke[0]); int m=0;
for (int j=1; j<n; j++) {
double d = rastojanje (org, *tacke[j]);
if (d<min) { min = d; m = j; }
}
cout << i << ' ' << *tacke[m] << ' ' << min << endl;
}
return 0;
}
我实际上得到了这样的东西,例如:
T3102720.3112960.3112960.310528 0.519615
我需要以下表格中的结果:
T3 (0.3, 0.3, 0.3) 0.519615
这是class tacka4.h
#ifndef _tacka4_h_
#define _tacka4_h_
#include "pokretan.h"
#include "vektor1.h"
#include "brzina.h"
class Tacka: public Pokretan {
static int ukId;
const int id;
Vektor r;
Brzina v;
public:
Tacka (const Vektor& rr=Vektor (),
const Brzina& vv=Brzina () )
: r (rr), v (vv), id (++ukId) {}
Tacka (const Tacka& T)
: r (T.r), v (T.v), id(++ukId) {}
Tacka& operator= (const Tacka& T)
{ r = T.r; v = T.v; return *this; }
Pokretan& proteklo (double dt)
{ r = r + v * dt; return *this; }
Vektor R () const {return r; }
friend double rastojanje (const Tacka& T1, const Tacka& T2)
{return + (T1.r + T2.r * -1);}
friend ostream& operator<< (ostream& d, const Tacka& T)
{return d << 'T' << T.id << T.r; }
};
#endif