aufgabe 2

This commit is contained in:
Frank Mayer 2025-01-17 13:34:44 +01:00
parent 5dd8a33f1c
commit 509a9d9981
Signed by: tsukinoko-kun
GPG Key ID: 427B3E61E69C2E51
15 changed files with 196 additions and 130 deletions

17
.editorconfig Normal file
View File

@ -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

1
.gitignore vendored
View File

@ -21,3 +21,4 @@ my_project
*.dll
*.exe
*.zip
build/

View File

@ -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);
}

26
src/DistanceCalculator.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef DISTANCECALCULATOR_H
#define DISTANCECALCULATOR_H
#include "Point.h"
#include <math.h>
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

View File

@ -1,22 +0,0 @@
#include "Fahrzeug.h"
#include <iostream>
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;

View File

@ -1,23 +0,0 @@
#ifndef FAHRZEUG_H
#define FAHRZEUG_H
#include <string>
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

View File

@ -1,22 +0,0 @@
#include "Fuhrpark_1.h"
#include "Fahrzeug.h"
#include <iostream>
#include <stdexcept>
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;
}

View File

@ -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

View File

@ -1,16 +0,0 @@
#include "Fuhrpark_2.h"
#include "Fahrzeug.h"
#include <iostream>
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;
}

View File

@ -1,16 +0,0 @@
#ifndef FUHRPARK2_H
#define FUHRPARK2_H
#include "Fahrzeug.h"
#include <vector>
class Fuhrpark_2 {
public:
void Add(const Fahrzeug &arg);
void Print() const;
private:
std::vector<Fahrzeug *> *pFuhrpark = new std::vector<Fahrzeug *>;
};
#endif // !FUHRPARK2_H

14
src/Point.cpp Normal file
View File

@ -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;
}

27
src/Point.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <string>
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

50
src/PointList.cpp Normal file
View File

@ -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));
}
}

24
src/PointList.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef POINTLIST_H
#define POINTLIST_H
#include <vector>
#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<Point*> m_points;
void DeepCopy(const PointList& other);
};
#endif // POINTLIST_H

View File

@ -1,21 +1,26 @@
#include "Fahrzeug.h"
#include "Fuhrpark_1.h"
#include "Fuhrpark_2.h"
#include "Point.h"
#include "PointList.h"
#include <iostream>
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;
}