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
|
#include <iostream>
#include <fstream> //For ofstream and ifstream variables
#include <cmath> //For sqrt(), pow()
#include <iomanip> //For fixed, showpoint, setprecision
#include <string> //For string objects
using namespace std;
int main()
{
//Data Abstraction:
double x_start = 0.0,
x_end = 0.0,
x_data_1 = 0.0,
x_data_2 = 0.0,
y_start = 0.0,
y_end = 0.0,
y_data_1 = 0.0,
y_data_2 = 0.0,
distance = 0.0,
total_distance = 0.0;
int attempts = 0;
string file_name;
string command;
ifstream in;
ofstream out;
//Input:
do
{
cout << "Please Enter The Name Of The Data File: ";
cin >> file_name;
in.open(file_name.c_str());
if(!in.is_open() && attempts < 3)
cout << "ERROR: File did NOT open - try again." << endl;
attempts++;
}
while (!in.is_open() && attempts < 3);
if(!in.is_open())
cout << "\nERROR: File did NOT open in 3 attempts." << endl;
//Open File and calculate distances
else
{
in.ignore( 13, '\n' );
in.ignore( 80, '\n' );
//Reading File
while (in >> command)
{
if (command == "START")
{
in >> x_start >> y_start;
in >> command;
if (command == "DATA")
{
in >> x_data_1 >> y_data_1;
total_distance += sqrt(pow((x_data_1 - x_start), 2.0)
+ pow((y_data_1 - y_start), 2.0));
}
else
{
in >> x_end >> y_end;
total_distance += sqrt(pow((x_start - x_end), 2.0)
+ pow((y_start - y_end), 2.0));
}
}
if (command == "DATA")
{
in >> x_data_1 >> y_data_1;
in >> command;
if(command == "DATA")
{
in >> x_data_2 >> y_data_2;
total_distance += sqrt(pow((x_data_2 - x_data_1), 2.0)
+ pow((y_data_2 - y_data_1), 2.0));
}
else
{
in >> x_end >> y_end;
total_distance += sqrt(pow((x_data_1 - x_end), 2.0)
+ pow((y_data_1 - y_end), 2.0));
}
}
if(command == "STOP")
{
in >> x_end >> y_end;
total_distance += sqrt(pow((x_data_1 - x_end), 2.0)
+ pow((y_data_1 - y_end), 2.0));
}
}
distance += sqrt(pow((x_start - x_end), 2.0)
+ pow((y_start - y_end), 2.0));
//Displaying Results to stdout and output file
out.open("GPS.report.txt");
cout << fixed << showpoint << setprecision(1);
out << fixed << showpoint << setprecision(1);
cout << "\nFinal Location: " << "(" << x_end << ", "
<< y_end << ")" << endl;
cout << "Total distance traveled: " << total_distance << endl;
cout << "Distance to starting point " << distance << endl;
out << "Final Location: " << "(" << x_end << ", "
<< y_end << ")" << endl;
out << "Total distance traveled: " << total_distance << endl;
out << "Distance to starting point " << distance;
out.close();
in.close();
}
return 0;
}
|