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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
class planet
{
private:
float planet_id, x, y, z;
public:
planet ();
planet (float, float, float, float);
planet (const planet ©_planet);
~planet();
void get_details(ofstream &fout);
void generate_planet (ifstream &fin);
planet report_planet ();
float total_route_length (planet *first_planet, planet *second_planet);
};
planet::planet ()
{
planet_id = 0.0;
x = 0.0;
y = 0.0;
z = 0.0;
}
planet::planet (float id, float x_coord, float y_coord, float z_coord)
{
planet_id = id;
x = x_coord;
y = y_coord;
z = z_coord;
}
planet::planet (const planet ©_planet)
{
planet_id = copy_planet.planet_id;
x = copy_planet.x;
y = copy_planet.y;
z = copy_planet.z;
}
planet::~planet()
{
planet_id = 0.0;
x = 0.0;
y = 0.0;
z = 0.0;
}
void planet::get_details (ofstream &fout)
{
cout << "Enter the details for Planet " << endl;
cout << "Enter the Planet's ID: ";
cin >> planet_id;
fout << planet_id << "\t";
cout << "Enter the planet's x coordinate: ";
cin >> x;
fout << x << "\t";
cout << "Enter the planet's y coordinate: ";
cin >> y;
fout << y << "\t";
cout << "Enter the planet's z coordinate: ";
cin >> z;
fout << z << endl;
cout << endl;
}
void planet::generate_planet (ifstream &fin)
{
fin >> planet_id;
fin >> x;
fin >> y;
fin >> z;
}
planet planet::report_planet ()
{
planet *new_planet = new planet;
cout << "Planet's ID: " << planet_id << endl;
cout << "x coordinate: " << x << endl;
cout << "y coordinate: " << y << endl;
cout << "z coordinate: " << z << endl;
cout << "\n";
return *new_planet;
}
float planet::total_route_length (planet *first_planet, planet *second_planet)
{
if ((first_planet -> planet_id)==1)
{
float base_distance = sqrt(pow((first_planet -> x) - 0,2) + pow((first_planet -> y) - 0,2) + pow((first_planet -> z) - 0,2));
float result = base_distance + sqrt(pow((first_planet -> x) - (second_planet -> x),2) + pow((first_planet -> y) - (second_planet -> y),2) + pow((first_planet -> z) - (second_planet -> z),2));
return result;
}
if ((second_planet -> planet_id)==1)
{
float base_distance = sqrt(pow(0 - (second_planet -> x),2) + pow(0 - (second_planet -> y),2) + pow(0 - (second_planet -> z),2));
float result = base_distance + sqrt(pow((first_planet -> x) - (second_planet -> x),2) + pow((first_planet -> y) - (second_planet -> y),2) + pow((first_planet -> z) - (second_planet -> z),2));
return result;
}
if ((first_planet -> planet_id)>1 && (second_planet -> planet_id)>1)
{
float result = sqrt(pow((first_planet -> x) - (second_planet -> x),2) + pow((first_planet -> y) - (second_planet -> y),2) + pow((first_planet -> z) - (second_planet -> z),2));
return result;
}
}
void main()
{
int i, temp_size;
float distance = 0;
planet mars;
planet the_red_one(mars);
ofstream fout;
fout.open ("route1.txt");
cout << "Enter the number of planets: ";
cin >> temp_size;
fout << temp_size << endl;
cout << endl;
for (i=0; i<temp_size; i++)
mars.get_details (fout);
fout.close ();
ifstream fin;
fin.open ("route1.txt");
fin >> temp_size;
planet *the_planets = new planet [temp_size];
for (i=0; i<temp_size; i++)
{
//int j=j+1;
mars.generate_planet (fin);
the_planets[i] = mars.report_planet ();
//if (j>(temp_size-1))
//j = 0;
//distance = distance + mars.total_route_length (the_planets[i], the_planets[j]); //pass pointer to planet to calculate distance between planets
//if (j==0)
//cout << "The Total Route Length is: " << distance << endl;
}
fin.close ();
cin.sync ();
cin.get ();
}
| |