From 509a9d9981a01ff64c06d0f9003a9247b3ae852e Mon Sep 17 00:00:00 2001 From: Frank Mayer Date: Fri, 17 Jan 2025 13:34:44 +0100 Subject: [PATCH] aufgabe 2 --- .editorconfig | 17 +++++++++++++ .gitignore | 1 + src/DistanceCalculator.cpp | 18 ++++++++++++++ src/DistanceCalculator.h | 26 ++++++++++++++++++++ src/Fahrzeug.cpp | 22 ----------------- src/Fahrzeug.h | 23 ------------------ src/Fuhrpark_1.cpp | 22 ----------------- src/Fuhrpark_1.h | 17 ------------- src/Fuhrpark_2.cpp | 16 ------------ src/Fuhrpark_2.h | 16 ------------ src/Point.cpp | 14 +++++++++++ src/Point.h | 27 ++++++++++++++++++++ src/PointList.cpp | 50 ++++++++++++++++++++++++++++++++++++++ src/PointList.h | 24 ++++++++++++++++++ src/main.cpp | 33 ++++++++++++++----------- 15 files changed, 196 insertions(+), 130 deletions(-) create mode 100644 .editorconfig create mode 100644 src/DistanceCalculator.cpp create mode 100644 src/DistanceCalculator.h delete mode 100644 src/Fahrzeug.cpp delete mode 100644 src/Fahrzeug.h delete mode 100644 src/Fuhrpark_1.cpp delete mode 100644 src/Fuhrpark_1.h delete mode 100644 src/Fuhrpark_2.cpp delete mode 100644 src/Fuhrpark_2.h create mode 100644 src/Point.cpp create mode 100644 src/Point.h create mode 100644 src/PointList.cpp create mode 100644 src/PointList.h diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9613607 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[Makefile] +indent_style = tab + +[*.{c++,cc,cpp,cxx,h,h++,hh,hpp,hxx,inl,ipp,tlh,tli}] +indent_style = space +indent_size = 8 diff --git a/.gitignore b/.gitignore index 76f027f..6f282c4 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ my_project *.dll *.exe *.zip +build/ diff --git a/src/DistanceCalculator.cpp b/src/DistanceCalculator.cpp new file mode 100644 index 0000000..b96381f --- /dev/null +++ b/src/DistanceCalculator.cpp @@ -0,0 +1,18 @@ +#include "DistanceCalculator.h" + +DistanceCalculator *DistanceCalculator::m_pTheInstance = nullptr; + +DistanceCalculator *DistanceCalculator::GetInstance() { + if (!m_pTheInstance) { + m_pTheInstance = new DistanceCalculator(); + // Anmerkung: m_pTheInstance wird niemals freigegeben + } + return m_pTheInstance; +} + +double DistanceCalculator::Distance(const Point &from, const Point &to) const { + double lat1 = GetRadians((from.Latitude() + to.Latitude()) / 2); + double dx = 111.3 * cos(lat1) * (from.Longitude() - to.Longitude()); + double dy = 111.3 * (from.Latitude() - to.Latitude()); + return sqrt(dx * dx + dy * dy); +} diff --git a/src/DistanceCalculator.h b/src/DistanceCalculator.h new file mode 100644 index 0000000..a21f531 --- /dev/null +++ b/src/DistanceCalculator.h @@ -0,0 +1,26 @@ +#ifndef DISTANCECALCULATOR_H +#define DISTANCECALCULATOR_H + +#include "Point.h" +#include + +class DistanceCalculator { + public: + static DistanceCalculator *GetInstance(); + + double Distance(const Point &from, const Point &to) const; + + private: + DistanceCalculator() = default; + ~DistanceCalculator() = default; + + private: + DistanceCalculator(const DistanceCalculator &) = delete; + DistanceCalculator &operator=(const DistanceCalculator &) = delete; + + private: + static double GetRadians(double degrees) { return degrees * M_PI / 180.0; } + static DistanceCalculator *m_pTheInstance; +}; + +#endif // DISTANCECALCULATOR_H diff --git a/src/Fahrzeug.cpp b/src/Fahrzeug.cpp deleted file mode 100644 index 5315e97..0000000 --- a/src/Fahrzeug.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "Fahrzeug.h" -#include - -void Fahrzeug::Print() { - std::cout << "Hersteller: " << hersteller << std::endl - << "Modell: " << modell << std::endl - << "Kennzeichen: " << kennzeichen << std::endl - << "Reichweite in km: " << reichweiteInKm << std::endl - << "Ladung in Prozent: " << ladungInProzent << std::endl; -} - -Fahrzeug::Fahrzeug(std::string hersteller, std::string modell, - std::string kennzeichen, unsigned int reichweiteInKm, - unsigned int ladungInProzent) { - this->hersteller = hersteller; - this->modell = modell; - this->kennzeichen = kennzeichen; - this->reichweiteInKm = reichweiteInKm; - this->ladungInProzent = ladungInProzent; -} - -Fahrzeug::Fahrzeug() = default; diff --git a/src/Fahrzeug.h b/src/Fahrzeug.h deleted file mode 100644 index c9abea7..0000000 --- a/src/Fahrzeug.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef FAHRZEUG_H -#define FAHRZEUG_H - -#include - -class Fahrzeug { -public: - void Print(); - -public: - Fahrzeug(std::string hersteller, std::string modell, std::string kennzeichen, - unsigned int reichweiteInKm, unsigned int ladungInProzent); - Fahrzeug(); - -protected: - std::string hersteller; - std::string modell; - std::string kennzeichen; - unsigned int reichweiteInKm; - unsigned int ladungInProzent; -}; - -#endif // FAHRZEUG_H diff --git a/src/Fuhrpark_1.cpp b/src/Fuhrpark_1.cpp deleted file mode 100644 index 1749fa0..0000000 --- a/src/Fuhrpark_1.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "Fuhrpark_1.h" -#include "Fahrzeug.h" -#include -#include - -void Fuhrpark_1::Add(const Fahrzeug &arg) { - if (anzElem < Max_Elems) { - pFuhrpark[anzElem] = new Fahrzeug(arg); - ++anzElem; - } else { - throw new std::out_of_range("Fuhrpark_1 instalce is full"); - } -} - -void Fuhrpark_1::Print() const { - std::cout << "Fuhrpark 1:" << std::endl; - for (size_t i = 0; i < anzElem; ++i) { - std::cout << "Fahrzeug " << i + 1 << ":" << std::endl; - pFuhrpark[i]->Print(); - } - std::cout << std::endl; -} diff --git a/src/Fuhrpark_1.h b/src/Fuhrpark_1.h deleted file mode 100644 index 5d67a37..0000000 --- a/src/Fuhrpark_1.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef FUHRPARK1_H -#define FUHRPARK1_H - -#include "Fahrzeug.h" - -class Fuhrpark_1 { -public: - void Add(const Fahrzeug &arg); - void Print() const; - -private: - static const size_t Max_Elems = 100; - size_t anzElem = 0; - Fahrzeug *pFuhrpark[Max_Elems]; -}; - -#endif // !FUHRPARK1_H diff --git a/src/Fuhrpark_2.cpp b/src/Fuhrpark_2.cpp deleted file mode 100644 index 7139a38..0000000 --- a/src/Fuhrpark_2.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "Fuhrpark_2.h" -#include "Fahrzeug.h" -#include - -void Fuhrpark_2::Add(const Fahrzeug &arg) { - pFuhrpark->push_back(new Fahrzeug(arg)); -} - -void Fuhrpark_2::Print() const { - std::cout << "Fuhrpark 2:" << std::endl; - for (size_t i = 0; i < pFuhrpark->size(); ++i) { - std::cout << "Fahrzeug " << i + 1 << ":" << std::endl; - (*pFuhrpark)[i]->Print(); - } - std::cout << std::endl; -} diff --git a/src/Fuhrpark_2.h b/src/Fuhrpark_2.h deleted file mode 100644 index 25e6b77..0000000 --- a/src/Fuhrpark_2.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef FUHRPARK2_H -#define FUHRPARK2_H - -#include "Fahrzeug.h" -#include - -class Fuhrpark_2 { -public: - void Add(const Fahrzeug &arg); - void Print() const; - -private: - std::vector *pFuhrpark = new std::vector; -}; - -#endif // !FUHRPARK2_H diff --git a/src/Point.cpp b/src/Point.cpp new file mode 100644 index 0000000..38381f9 --- /dev/null +++ b/src/Point.cpp @@ -0,0 +1,14 @@ +#include "Point.h" + +void Point::Set(double latitude, double longitude, const std::string &name) { + m_latitude = latitude; + m_longitude = longitude; + if (!name.empty()) { + m_name = name; + } +} + +void Point::Print() const { + std::cout << "Name: " << m_name << ", Latitude: " << m_latitude + << ", Longitude: " << m_longitude << std::endl; +} diff --git a/src/Point.h b/src/Point.h new file mode 100644 index 0000000..66eb0e7 --- /dev/null +++ b/src/Point.h @@ -0,0 +1,27 @@ +#ifndef POINT_H +#define POINT_H + +#include +#include + +class Point { + public: + Point(double latitude = 0.0, double longitude = 0.0, const std::string &name = "") + : m_latitude(latitude), m_longitude(longitude), m_name(name) {} + + public: + double Latitude() const { return m_latitude; } + double Longitude() const { return m_longitude; } + const std::string &Name() const { return m_name; } + + void Set(double latitude, double longitude, const std::string &name = ""); + + void Print() const; + + private: + double m_latitude; + double m_longitude; + std::string m_name; +}; + +#endif // POINT_H diff --git a/src/PointList.cpp b/src/PointList.cpp new file mode 100644 index 0000000..54fc38b --- /dev/null +++ b/src/PointList.cpp @@ -0,0 +1,50 @@ +#include "PointList.h" +#include "DistanceCalculator.h" + +PointList::PointList(const PointList& other) { + DeepCopy(other); +} + +PointList& PointList::operator=(const PointList& other) { + if (this != &other) { + for (auto point : m_points) { + delete point; + } + m_points.clear(); + DeepCopy(other); + } + return *this; +} + +PointList::~PointList() { + for (auto point : m_points) { + delete point; + } +} + +void PointList::Add(Point& arg) { + m_points.push_back(new Point(arg)); +} + +void PointList::Print() const { + for (const auto point : m_points) { + point->Print(); + } +} + +double PointList::GetDistance() const { + double totalDistance = 0.0; + if (m_points.size() < 2) return totalDistance; + + DistanceCalculator* calculator = DistanceCalculator::GetInstance(); + for (size_t i = 0; i < m_points.size() - 1; ++i) { + totalDistance += calculator->Distance(*m_points[i], *m_points[i + 1]); + } + return totalDistance; +} + +void PointList::DeepCopy(const PointList& other) { + for (const auto point : other.m_points) { + m_points.push_back(new Point(*point)); + } +} diff --git a/src/PointList.h b/src/PointList.h new file mode 100644 index 0000000..ca9ea34 --- /dev/null +++ b/src/PointList.h @@ -0,0 +1,24 @@ +#ifndef POINTLIST_H +#define POINTLIST_H + +#include +#include "Point.h" + +class PointList { + public: + void Add(Point& arg); + void Print() const; + double GetDistance() const; + + public: + PointList() = default; + PointList(const PointList& other); + PointList& operator=(const PointList& other); + ~PointList(); + + private: + std::vector m_points; + void DeepCopy(const PointList& other); +}; + +#endif // POINTLIST_H diff --git a/src/main.cpp b/src/main.cpp index 04499af..557e12e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,26 @@ -#include "Fahrzeug.h" -#include "Fuhrpark_1.h" -#include "Fuhrpark_2.h" +#include "Point.h" +#include "PointList.h" +#include int main() { - Fuhrpark_1 f1; - Fuhrpark_2 f2; + // Erstellen der PointList + PointList* pWayPoints = new PointList(); - Fahrzeug ts("Tesla", "Model S", "M-1234", 500, 80); - f1.Add(ts); - f2.Add(ts); + // Hinzufügen von Wegepunkten + pWayPoints->Add(*(new Point(49.12192452821553, 9.211100802045872, "74081 Heilbronn, Max-Planck-Straße 39"))); + pWayPoints->Add(*(new Point(49.12048223396379, 9.20712620461664, "74081 Heilbronn, Jörg-Ratgeb-Platz 1"))); + pWayPoints->Add(*(new Point(49.15188792098829, 9.234639505466209, "74076 Heilbronn, Im Breitenloch / Ecke, Kübelstraße"))); - Fahrzeug pt("Porsche", "Taycan", "M-5678", 400, 90); - f1.Add(pt); - f2.Add(pt); + // Ausgabe der Wegepunkte + std::cout << "Wegepunkte:" << std::endl; + pWayPoints->Print(); - f1.Print(); - f2.Print(); + // Berechnung und Ausgabe der Gesamtdistanz + double totalDistance = pWayPoints->GetDistance(); + std::cout << std::endl << "Gesamtdistanz: " << totalDistance << " km" << std::endl; - return 0; + // Speicher freigeben + delete pWayPoints; + + return 0; }