Commit 285fc0de by kmicha

Add new file

parent 65ddf9fd
Showing with 153 additions and 0 deletions
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
// Struct to represent a geographical coordinate
struct Coordinate {
double latitude;
double longitude;
};
// Struct to represent a point with ID and total distance for the line
struct Punkt {
int ID;
double totalDistance;
Punkt* next;
};
// Class to handle file operations and calculations
class FileHandler {
public:
// Function to read coordinates from a file
static vector<Coordinate> readCoordinatesFromFile(const string& filename) {
vector<Coordinate> coordinates;
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening file: " << filename << endl;
return coordinates;
}
string line;
while (getline(file, line)) {
vector<string> parts = split(line, ',');
if (parts.size() != 2) {
cerr << "Invalid line in file: " << line << endl;
continue;
}
double lat, lon;
try {
lat = stod(parts[0]);
lon = stod(parts[1]);
} catch (const exception& e) {
cerr << "Error parsing coordinates: " << e.what() << endl;
continue;
}
coordinates.push_back({lat, lon});
}
file.close();
return coordinates;
}
private:
// Function to split a string by delimiter
static vector<string> split(const string& str, char delimiter) {
vector<string> tokens;
string token;
istringstream tokenStream(str);
while (getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
};
// Class to handle Punkt operations
class PunktHandler {
public:
// Function to create and append a new Punkt to the linked list
static void appendPunkt(Punkt*& head, int id, double distance) {
Punkt* newPunkt = new Punkt{id, distance, nullptr};
if (head == nullptr) {
head = newPunkt;
} else {
Punkt* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newPunkt;
}
}
// Function to display the linked list of Punkts
static void displayPunkts(const Punkt* head) {
const Punkt* current = head;
while (current != nullptr) {
cout << "ID: " << current->ID << ", Total distance: " << current->totalDistance << " kilometers" << endl;
current = current->next;
}
}
// Function to free memory allocated for Punkts
static void deletePunkts(Punkt* head) {
while (head != nullptr) {
Punkt* temp = head;
head = head->next;
delete temp;
}
}
};
// Function to calculate the distance between two coordinates using Haversine formula
double distanceBetweenCoordinates(const Coordinate& coord1, const Coordinate& coord2) {
const double R = 6371.0; // Earth radius in kilometers
double dlat = (coord2.latitude - coord1.latitude) * M_PI / 180.0;
double dlon = (coord2.longitude - coord1.longitude) * M_PI / 180.0;
double a = sin(dlat / 2) * sin(dlat / 2) +
cos(coord1.latitude * M_PI / 180.0) * cos(coord2.latitude * M_PI / 180.0) *
sin(dlon / 2) * sin(dlon / 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
return R * c;
}
int main() {
string filename;
cout << "Enter filename: ";
cin >> filename;
// Read coordinates from file
vector<Coordinate> coordinates = FileHandler::readCoordinatesFromFile(filename);
while (coordinates.empty()) {
cout << "No coordinates read from file. Please try again." << endl;
cout << "Enter filename: ";
cin >> filename;
coordinates = FileHandler::readCoordinatesFromFile(filename);
}
// Calculate total distance for the line
double totalDistance = 0.0;
for (size_t i = 0; i < coordinates.size() - 1; ++i) {
totalDistance += distanceBetweenCoordinates(coordinates[i], coordinates[i + 1]);
}
// Create a Punkt for the line and save total distance
Punkt* head = nullptr;
PunktHandler::appendPunkt(head, 0, totalDistance);
// Display the Punkt
cout << "Total distance for line: " << totalDistance << " kilometers" << endl;
cout << "Punkt:" << endl;
PunktHandler::displayPunkts(head);
// Free memory allocated for Punkt
PunktHandler::deletePunkts(head);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment