diff --git a/04_UDEF/MP/main_04_UDEF_e.cpp b/04_UDEF/MP/main_04_UDEF_e.cpp index 55e2e7f..ad43ea3 100644 --- a/04_UDEF/MP/main_04_UDEF_e.cpp +++ b/04_UDEF/MP/main_04_UDEF_e.cpp @@ -9,31 +9,28 @@ public: bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise char bitsAsDigits[bitsPerOctet]{}; // bit values as chars BinaryOctet(int x = 0); - BinaryOctet& operator=(int const &a); BinaryOctet(const BinaryOctet &) = default; -private: - BinaryOctet init(int x); + BinaryOctet& operator=(int const &a); + const BinaryOctet operator--(int); }; -BinaryOctet BinaryOctet::init(int x) { +BinaryOctet::BinaryOctet(int x) { + //Alles mit 0 füllen + for (char &bitsAsDigit : bitsAsDigits) { + bitsAsDigit = '0'; + } int length = static_cast(std::to_string(x).length() * 4); - while (x >= 1) { - bitsAsDigits[length] = (x % 2 == 0) ? '1' : '0'; + while (length >= 0) { + bitsAsDigits[length-1] = (x % 2 == 0) ? '0' : '1'; + std::cout << x << " : " << length << " : " << bitsAsDigits[length-1] << std::endl; x = x / 2; length--; } - int i = 0; for (char c : bitsAsDigits) { if (c == '1') i++; } evenParity = i % 2 == 0; - - return *this; -} - -BinaryOctet::BinaryOctet(int x) { - init(x); } bool operator!=(BinaryOctet a, BinaryOctet b) { @@ -44,10 +41,10 @@ bool operator!=(BinaryOctet a, BinaryOctet b) { return true; } -const BinaryOctet operator--(BinaryOctet &id, int) { +const BinaryOctet BinaryOctet::operator--(const int) { - return id; + return *this; } BinaryOctet operator+(BinaryOctet a, BinaryOctet b) { @@ -61,8 +58,13 @@ BinaryOctet operator+(BinaryOctet a, BinaryOctet b) { return result; } +BinaryOctet operator+(BinaryOctet a, int b) { + +} + BinaryOctet operator/(BinaryOctet a, BinaryOctet b) { + return a; } BinaryOctet& BinaryOctet::operator=(int const &a) { @@ -103,14 +105,13 @@ std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){ int main(int argc, char **argv) { BinaryOctet a = 0b00001111; - BinaryOctet b = 0b00000110; std::cout << a << std::endl; - BinaryOctet c = 00001111; - std::cout << c << std::endl; + BinaryOctet b = 0b00000110; + std::cout << b << std::endl; std::cout << new BinaryOctet(5) << std::endl; println(a + b); std::cout << a + b << std::endl; //println("result = ", doCalculation(a,b)); - //std::cout << "result = " << doCalculation(a,b) << std::endl; + std::cout << "result = " << doCalculation(a,b) << std::endl; return 0; } diff --git a/05_OO/MP/AnsiConsoleDemo.cpp b/05_OO/MP/AnsiConsoleDemo.cpp deleted file mode 100644 index f149f11..0000000 --- a/05_OO/MP/AnsiConsoleDemo.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include "../../helpers/AnsiConsole.h" - -/* - see - https://en.wikipedia.org/wiki/ANSI_escape_code - https://gist.github.com/vratiu/9780109 - https://gist.github.com/RobinMalfait/7881398 - */ - - -int main(int argc, char **argv) -{ - ansiConsole.clearScreen(); - // x=1 and y=1 is the upper left corner - // x and y are more like column and row - ansiConsole.printText(4,4,"Hello, World!"); - - // draw a pixel, sort of - ansiConsole.printText(3,3,"*"); - // draw a line - for(int i=0;i<10;i++){ - ansiConsole.printText(2+i,2,"-", Colors::GREEN); - } - ansiConsole.printText(5,15,"end."); - - return 0; -} diff --git a/05_OO/MP/build.sh b/05_OO/MP/build.sh deleted file mode 100644 index 4237857..0000000 --- a/05_OO/MP/build.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -clang++ -std=c++14 -o shape_main.out -I ../helpers/ shapes_main.cpp ../helpers/AnsiConsole.cpp -clang++ -std=c++14 -o ansiConsole.out -I ../helpers/ AnsiConsoleDemo.cpp ../helpers/AnsiConsole.cpp \ No newline at end of file diff --git a/05_OO/MP/main_mp4_OO_a_vehicles.cpp b/05_OO/MP/main_mp4_OO_a_vehicles.cpp deleted file mode 100644 index 9eeac3d..0000000 --- a/05_OO/MP/main_mp4_OO_a_vehicles.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include -#include - - -//====================================== - -class Vehicle { -protected: - int _numSeats; -public: - Vehicle(int numSeats=0);// may serve as default ctor (i.e. no arguments) - virtual ~Vehicle(); - virtual int payload() = 0; - int numSeats(); // a 'getter' method to get a value; no 'setter' here -}; - -//====================================== - -class Car : public Vehicle { -protected: - int _maxWeight; // german: zulässiges Gesamtgewicht -public: - Car(int numSeats, int maxWeight); - virtual int payload(); - -}; - -//====================================== - -class Truck : public Vehicle { -protected: - int _payload; -public: - Truck(int numSeats, int payload); - virtual int payload(); -}; - -//====================================== - -Vehicle::Vehicle(int numSeats){ - _numSeats = numSeats; -} - -Vehicle::~Vehicle(){ - std::cout << "destroying a Vehicle" << std::endl; -} - -int Vehicle::numSeats(){ - return _numSeats; -} - -//====================================== - -Car::Car(int numSeats, int maxWeight){ - _numSeats = numSeats; - _maxWeight = maxWeight; -} - -int Car::payload(){ - return _maxWeight - (_numSeats*75); -} - -//====================================== - -Truck::Truck(int numSeats, int payload){ - _numSeats = numSeats; - _payload = payload; -} - -int Truck::payload(){ - return _payload; -} - -//====================================== - - -void printVehicleInfo(Vehicle* v){ - std::cout << "typeid=`" << typeid(*v).name() << "`" - << " numSeats=" << v->numSeats() - << " payload=" << v->payload() << std::endl; -} - -int main(int argc, const char * argv[]) { - - Car* c = new Car(5, 1000); // create a new object of class Car in free store - Truck* t = new Truck(3, 7500); - - std::cout << "1" << std::endl; - std::cout << "c: numSeats=" << c->numSeats() << " payload=" << c->payload() << std::endl; - std::cout << "t: numSeats=" << t->numSeats() << " payload=" << t->payload() << std::endl; - - std::cout << std::endl << "2" << std::endl; - Vehicle* v = c; // a Car `is a` Vehicle => implicitly convertible - printVehicleInfo(v); - v = t; // a Truck `is a` Vehicle => implicitly convertible - printVehicleInfo(v); - - - // release memory occupied by t,c for use by future objects created by `new` - // do NOT release v. it is only an alias - delete t; - delete c; - - return 0; -} diff --git a/05_OO/MP/main_mp4_OO_b.cpp b/05_OO/MP/main_mp4_OO_b.cpp deleted file mode 100644 index 2e6ee06..0000000 --- a/05_OO/MP/main_mp4_OO_b.cpp +++ /dev/null @@ -1,217 +0,0 @@ -// file main_mp4_OO_b.cpp -#include -#include - -//======================================= - -struct FooVal { - FooVal(int initialValue=0); - ~FooVal(); -private: - int _someValue; -}; - -//======================================= - -class FooBase { - FooVal _value; -public: - FooBase(); - FooBase(int initialValue); - virtual ~FooBase(); -}; - -//======================================= - -class FooDerived : public FooBase { - int _ival; -public: - FooDerived(int initialValue); - ~FooDerived(); -}; - -//======================================= - -class Bar { - FooBase *_helperObject; -public: - Bar(); - ~Bar(); -}; - -//======================================= - -class Base { - int a; -public: - Base(int a) { - this.a = a; - } -}; - -class Derived : public Base{ - int b; -public: - Derived(int a, int b) { - Base::Base(a); - this.b = b; - } -}; - -//======================================= - -FooVal::FooVal(int initialValue) -: _someValue(initialValue) -{ - std::cout << "FooVal::FooVal()" << std::endl; -} - -FooVal::~FooVal(){ - std::cout << "FooVal::~FooVal()" << std::endl; -} - -FooBase::FooBase(){ - std::cout << "FooBase::FooBase()" << std::endl; -} - -FooBase::FooBase(int initialValue) -: _value(initialValue) -{ - std::cout << "FooBase::FooBase(int)" << std::endl; -} - -FooBase::~FooBase(){ - std::cout << "FooBase::~FooBase(" << std::endl; -} - -FooDerived::FooDerived(int initialValue) -: FooBase(initialValue), _ival(0) -{ - std::cout << "FooDerived::FooDerived()" << std::endl; -} - -FooDerived::~FooDerived(){ - std::cout << "FooDerived::~FooDerived()" << std::endl; -} - -Bar::Bar(){ - _helperObject = new FooDerived(0); -} - -Bar::~Bar(){ - delete _helperObject; -} - - -struct StackObject { -private: - void* operator new(size_t size) noexcept { - bool noStackObjectOnHeap = false; - assert(noStackObjectOnHeap); - return nullptr; - } -}; - - -struct A : public StackObject { - A(){std::cout << "+A ";} - //A(const A&){std::cout << "+A";} - ~A(){std::cout << "-A ";} -}; - -struct B : public StackObject { - B(){std::cout << "+B ";} - //B(const B&){std::cout << "+B";} - ~B(){std::cout << "-B ";} -}; - - -struct C : public StackObject { - C(){std::cout << "+C ";} - //C(const C&){std::cout << "+C";} - ~C(){std::cout << "-C ";} -}; - -class HeapObject{ -public: - void* operator new (size_t size); - HeapObject(); - virtual ~HeapObject(); - static bool assertionsHold(); -protected: -private: - static int ctorCount; - static int dtorCount; - static int newCount; - // static void remove(HeapObject *); - HeapObject(const HeapObject&) = delete; - HeapObject& operator=(const HeapObject&) = delete; -}; - -int HeapObject::ctorCount = 0; -int HeapObject::dtorCount = 0; -int HeapObject::newCount = 0; - -void* HeapObject::operator new (size_t size){ - newCount++; - return new char[size]; -} - -HeapObject::HeapObject(){ - ctorCount++; -} - -HeapObject::~HeapObject(){ - dtorCount++; -} - -bool HeapObject::assertionsHold(){ - assert(ctorCount == newCount); // all objects have been allocated on heap - assert(ctorCount == dtorCount); // all objects have been deleted - return true; -} - - -class K : public HeapObject -{ -public: - K(){std::cout << "+K ";} - ~K(){std::cout << "-K ";} -}; - -class L { -public: - L(){std::cout << "+L ";} - ~L(){std::cout << "-L ";} -}; - - -class M { -public: - M(){std::cout << "+M ";} - ~M(){std::cout << "-M ";} -}; - - - -int main(int argc, const char * argv[]) { - Bar * bar = new Bar(); - // ... - delete bar; // implies "delete _helperObject"; - - FooBase *obj = new FooDerived(7); - delete obj; - - { - C c; - K* k = new K(); - delete k; - } - - HeapObject::assertionsHold(); - std::cout << " ENDE" << std::endl; - return 0; -} - - - diff --git a/05_OO/MP/shapes_main.cpp b/05_OO/MP/shapes_main.cpp index 62d3c50..f09dcb8 100644 --- a/05_OO/MP/shapes_main.cpp +++ b/05_OO/MP/shapes_main.cpp @@ -1,11 +1,6 @@ -#include - -#include - -#include +#include "../../helpers/AnsiConsole.h" #include #include -#include "../../helpers/AnsiConsole.h" struct Position { int x; @@ -34,7 +29,6 @@ Shape::Shape(Position position, Colors color) { Shape::~Shape() = default; - class Point : public Shape{ public: @@ -141,10 +135,6 @@ Scene::~Scene() { } } - - - - int main(int argc, char **argv) { std::vector shapes; diff --git a/05_OO/Testat/AnsiConsoleDemo.cpp b/05_OO/Testat/AnsiConsoleDemo.cpp deleted file mode 100644 index 136daf0..0000000 --- a/05_OO/Testat/AnsiConsoleDemo.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include "AnsiConsole.h" - -/* - see - https://en.wikipedia.org/wiki/ANSI_escape_code - https://gist.github.com/vratiu/9780109 - https://gist.github.com/RobinMalfait/7881398 - */ - - -int main(int argc, char **argv) -{ - ansiConsole.clearScreen(); - // x=1 and y=1 is the upper left corner - // x and y are more like column and row - ansiConsole.printText(4,4,"Hello, World!"); - - // draw a pixel, sort of - ansiConsole.printText(3,3,"*"); - // draw a line - for(int i=0;i<10;i++){ - ansiConsole.printText(2+i,2,"-", Colors::GREEN); - } - ansiConsole.printText(5,15,"end."); - - return 0; -} diff --git a/05_OO/Testat/build.sh b/05_OO/Testat/build.sh deleted file mode 100644 index 4237857..0000000 --- a/05_OO/Testat/build.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -clang++ -std=c++14 -o shape_main.out -I ../helpers/ shapes_main.cpp ../helpers/AnsiConsole.cpp -clang++ -std=c++14 -o ansiConsole.out -I ../helpers/ AnsiConsoleDemo.cpp ../helpers/AnsiConsole.cpp \ No newline at end of file diff --git a/05_OO/Testat/main_mp4_OO_a_vehicles.cpp b/05_OO/Testat/main_mp4_OO_a_vehicles.cpp deleted file mode 100644 index 9eeac3d..0000000 --- a/05_OO/Testat/main_mp4_OO_a_vehicles.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include -#include - - -//====================================== - -class Vehicle { -protected: - int _numSeats; -public: - Vehicle(int numSeats=0);// may serve as default ctor (i.e. no arguments) - virtual ~Vehicle(); - virtual int payload() = 0; - int numSeats(); // a 'getter' method to get a value; no 'setter' here -}; - -//====================================== - -class Car : public Vehicle { -protected: - int _maxWeight; // german: zulässiges Gesamtgewicht -public: - Car(int numSeats, int maxWeight); - virtual int payload(); - -}; - -//====================================== - -class Truck : public Vehicle { -protected: - int _payload; -public: - Truck(int numSeats, int payload); - virtual int payload(); -}; - -//====================================== - -Vehicle::Vehicle(int numSeats){ - _numSeats = numSeats; -} - -Vehicle::~Vehicle(){ - std::cout << "destroying a Vehicle" << std::endl; -} - -int Vehicle::numSeats(){ - return _numSeats; -} - -//====================================== - -Car::Car(int numSeats, int maxWeight){ - _numSeats = numSeats; - _maxWeight = maxWeight; -} - -int Car::payload(){ - return _maxWeight - (_numSeats*75); -} - -//====================================== - -Truck::Truck(int numSeats, int payload){ - _numSeats = numSeats; - _payload = payload; -} - -int Truck::payload(){ - return _payload; -} - -//====================================== - - -void printVehicleInfo(Vehicle* v){ - std::cout << "typeid=`" << typeid(*v).name() << "`" - << " numSeats=" << v->numSeats() - << " payload=" << v->payload() << std::endl; -} - -int main(int argc, const char * argv[]) { - - Car* c = new Car(5, 1000); // create a new object of class Car in free store - Truck* t = new Truck(3, 7500); - - std::cout << "1" << std::endl; - std::cout << "c: numSeats=" << c->numSeats() << " payload=" << c->payload() << std::endl; - std::cout << "t: numSeats=" << t->numSeats() << " payload=" << t->payload() << std::endl; - - std::cout << std::endl << "2" << std::endl; - Vehicle* v = c; // a Car `is a` Vehicle => implicitly convertible - printVehicleInfo(v); - v = t; // a Truck `is a` Vehicle => implicitly convertible - printVehicleInfo(v); - - - // release memory occupied by t,c for use by future objects created by `new` - // do NOT release v. it is only an alias - delete t; - delete c; - - return 0; -} diff --git a/05_OO/Testat/main_mp4_OO_b.cpp b/05_OO/Testat/main_mp4_OO_b.cpp deleted file mode 100644 index 2e6ee06..0000000 --- a/05_OO/Testat/main_mp4_OO_b.cpp +++ /dev/null @@ -1,217 +0,0 @@ -// file main_mp4_OO_b.cpp -#include -#include - -//======================================= - -struct FooVal { - FooVal(int initialValue=0); - ~FooVal(); -private: - int _someValue; -}; - -//======================================= - -class FooBase { - FooVal _value; -public: - FooBase(); - FooBase(int initialValue); - virtual ~FooBase(); -}; - -//======================================= - -class FooDerived : public FooBase { - int _ival; -public: - FooDerived(int initialValue); - ~FooDerived(); -}; - -//======================================= - -class Bar { - FooBase *_helperObject; -public: - Bar(); - ~Bar(); -}; - -//======================================= - -class Base { - int a; -public: - Base(int a) { - this.a = a; - } -}; - -class Derived : public Base{ - int b; -public: - Derived(int a, int b) { - Base::Base(a); - this.b = b; - } -}; - -//======================================= - -FooVal::FooVal(int initialValue) -: _someValue(initialValue) -{ - std::cout << "FooVal::FooVal()" << std::endl; -} - -FooVal::~FooVal(){ - std::cout << "FooVal::~FooVal()" << std::endl; -} - -FooBase::FooBase(){ - std::cout << "FooBase::FooBase()" << std::endl; -} - -FooBase::FooBase(int initialValue) -: _value(initialValue) -{ - std::cout << "FooBase::FooBase(int)" << std::endl; -} - -FooBase::~FooBase(){ - std::cout << "FooBase::~FooBase(" << std::endl; -} - -FooDerived::FooDerived(int initialValue) -: FooBase(initialValue), _ival(0) -{ - std::cout << "FooDerived::FooDerived()" << std::endl; -} - -FooDerived::~FooDerived(){ - std::cout << "FooDerived::~FooDerived()" << std::endl; -} - -Bar::Bar(){ - _helperObject = new FooDerived(0); -} - -Bar::~Bar(){ - delete _helperObject; -} - - -struct StackObject { -private: - void* operator new(size_t size) noexcept { - bool noStackObjectOnHeap = false; - assert(noStackObjectOnHeap); - return nullptr; - } -}; - - -struct A : public StackObject { - A(){std::cout << "+A ";} - //A(const A&){std::cout << "+A";} - ~A(){std::cout << "-A ";} -}; - -struct B : public StackObject { - B(){std::cout << "+B ";} - //B(const B&){std::cout << "+B";} - ~B(){std::cout << "-B ";} -}; - - -struct C : public StackObject { - C(){std::cout << "+C ";} - //C(const C&){std::cout << "+C";} - ~C(){std::cout << "-C ";} -}; - -class HeapObject{ -public: - void* operator new (size_t size); - HeapObject(); - virtual ~HeapObject(); - static bool assertionsHold(); -protected: -private: - static int ctorCount; - static int dtorCount; - static int newCount; - // static void remove(HeapObject *); - HeapObject(const HeapObject&) = delete; - HeapObject& operator=(const HeapObject&) = delete; -}; - -int HeapObject::ctorCount = 0; -int HeapObject::dtorCount = 0; -int HeapObject::newCount = 0; - -void* HeapObject::operator new (size_t size){ - newCount++; - return new char[size]; -} - -HeapObject::HeapObject(){ - ctorCount++; -} - -HeapObject::~HeapObject(){ - dtorCount++; -} - -bool HeapObject::assertionsHold(){ - assert(ctorCount == newCount); // all objects have been allocated on heap - assert(ctorCount == dtorCount); // all objects have been deleted - return true; -} - - -class K : public HeapObject -{ -public: - K(){std::cout << "+K ";} - ~K(){std::cout << "-K ";} -}; - -class L { -public: - L(){std::cout << "+L ";} - ~L(){std::cout << "-L ";} -}; - - -class M { -public: - M(){std::cout << "+M ";} - ~M(){std::cout << "-M ";} -}; - - - -int main(int argc, const char * argv[]) { - Bar * bar = new Bar(); - // ... - delete bar; // implies "delete _helperObject"; - - FooBase *obj = new FooDerived(7); - delete obj; - - { - C c; - K* k = new K(); - delete k; - } - - HeapObject::assertionsHold(); - std::cout << " ENDE" << std::endl; - return 0; -} - - - diff --git a/05_OO/Testat/shapes_main.cpp b/05_OO/Testat/shapes_main.cpp index a2e7433..f09dcb8 100644 --- a/05_OO/Testat/shapes_main.cpp +++ b/05_OO/Testat/shapes_main.cpp @@ -1,46 +1,59 @@ -#include -#include #include "../../helpers/AnsiConsole.h" +#include +#include struct Position { int x; int y; - Position(int x_ = 0, int y_ = 0) { + explicit Position(int x_ = 0, int y_ = 0) { x = x_; y = y_; } }; -class Point { +class Shape { protected: - Position _position; + Position position; + Colors color; public: - Point(int x = 0, int y = 0); - - void draw(); + Shape(Position position, Colors color); + virtual ~Shape(); + virtual void draw() = 0; }; -Point::Point(int x, int y) { - _position = Position(x, y); +Shape::Shape(Position position, Colors color) { + this->position = position; + this->color = color; +} + +Shape::~Shape() = default; + +class Point : public Shape{ + +public: + explicit Point(int x, int y, Colors color); + void draw() override; +}; + +Point::Point(int x, int y, Colors color) : Shape(Position(x, y), color) { + } void Point::draw() { - ansiConsole.printText(_position.x, _position.y, "*", Colors::RED); + ansiConsole.printText(position.x, position.y, "*", color); } -class Circle { +class Circle : public Shape{ protected: - Position _position; int _radius; public: - Circle(int x = 0, int y = 0, int radius = 0); + explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN); - void draw(); + void draw() override; }; -Circle::Circle(int x, int y, int radius) { - _position = Position(x, y); +Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) { _radius = radius; } @@ -48,66 +61,100 @@ void Circle::draw() { /* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie * Höhensatz des Euklid * */ - int x_start = _position.x - _radius / 2; - int x_stop = _position.x + _radius / 2; + int x_start = position.x - _radius / 2; + int x_stop = position.x + _radius / 2; for (int i = x_start; i <= x_stop; i++) { double x_relative = double(i) - double(x_start); double h = sqrt(x_relative * (x_stop - x_start - x_relative)); - ansiConsole.printText(_position.x + int(x_relative) - _radius / 2, - _position.y + h, "#", - Colors::GREEN); - ansiConsole.printText(_position.x + int(x_relative) - _radius / 2, - _position.y - h, "#", - Colors::GREEN); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y + h), "#", + color); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y - h), "#", + color); } } -class Rectangle { +class Rectangle : public Shape{ protected: - Position position; int width; int height; public: - Rectangle(int x = 0, int y = 0, int width = 0, int height = 0); - void draw(); + explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE); + void draw() override; }; -Rectangle::Rectangle(int x, int y, int width, int height) { - position = Position(x, y); +Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) { this->width = width; this->height = height; } void Rectangle::draw() { + int x_start = position.x - (width / 2); + int x_stop = position.x + (width / 2); + int y_start = position.y - (height / 2); + int y_stop = position.y + (height / 2); + + for(int i = x_start; i <= x_stop; i++) { + ansiConsole.printText(i, position.y - (height / 2), "#", color); + ansiConsole.printText(i, position.y + (height / 2), "#", color); + } + for(int i = y_start; i< y_stop; i++) { + ansiConsole.printText(position.x + (width / 2), i, "#", color); + ansiConsole.printText(position.x - (width / 2), i, "#", color); + } +} + +class Scene { +private: + std::vector shapes; +public: + explicit Scene(std::vector shapes); + void draw(); + ~Scene(); +}; + +Scene::Scene(std::vector shapes) : shapes(std::move(shapes)) { + +} + +void Scene::draw() { + ansiConsole.clearScreen(); + for(Shape* shape : shapes) { + shape->draw(); + } +} + +Scene::~Scene() { + for(Shape* shape : shapes) { + delete shape; + } } int main(int argc, char **argv) { - // x=1 and y=1 is the upper left corner - // x and y are more like column and row - ansiConsole.printText(5, 5, "Hello, World!"); + std::vector shapes; - Point *p = new Point(10, 10); - p->draw(); - - Point *p2 = new Point(2, 10); - p2->draw(); + shapes.push_back(new Point(30, 10, Colors::RED)); + shapes.push_back(new Point(28, 8, Colors::BLUE)); + shapes.push_back(new Point(32, 8, Colors::BLUE)); - Circle *c = new Circle(30, 15, 10); - c->draw(); - - Point *p3 = new Point(30, 15); - p3->draw(); + shapes.push_back(new Circle(30, 10, 10, Colors::WHITE)); + shapes.push_back(new Circle(30, 20, 15, Colors::GREEN)); - delete p; - delete p2; - delete p3; - delete c; + shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA)); + + + auto *s = new Scene(shapes); + s->draw(); + + delete s; + return 0; } diff --git a/06_POLY/MP/shapes_main.cpp b/06_POLY/MP/shapes_main.cpp new file mode 100644 index 0000000..f09dcb8 --- /dev/null +++ b/06_POLY/MP/shapes_main.cpp @@ -0,0 +1,160 @@ +#include "../../helpers/AnsiConsole.h" +#include +#include + +struct Position { + int x; + int y; + + explicit Position(int x_ = 0, int y_ = 0) { + x = x_; + y = y_; + } +}; + +class Shape { +protected: + Position position; + Colors color; +public: + Shape(Position position, Colors color); + virtual ~Shape(); + virtual void draw() = 0; +}; + +Shape::Shape(Position position, Colors color) { + this->position = position; + this->color = color; +} + +Shape::~Shape() = default; + +class Point : public Shape{ + +public: + explicit Point(int x, int y, Colors color); + void draw() override; +}; + +Point::Point(int x, int y, Colors color) : Shape(Position(x, y), color) { + +} + +void Point::draw() { + ansiConsole.printText(position.x, position.y, "*", color); +} + +class Circle : public Shape{ +protected: + int _radius; +public: + explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN); + + void draw() override; +}; + +Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) { + _radius = radius; +} + +void Circle::draw() { + /* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie + * Höhensatz des Euklid + * */ + int x_start = position.x - _radius / 2; + int x_stop = position.x + _radius / 2; + + for (int i = x_start; i <= x_stop; i++) { + double x_relative = double(i) - double(x_start); + double h = sqrt(x_relative * (x_stop - x_start - x_relative)); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y + h), "#", + color); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y - h), "#", + color); + + } +} + +class Rectangle : public Shape{ +protected: + int width; + int height; + +public: + explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE); + void draw() override; +}; + +Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) { + this->width = width; + this->height = height; +} + +void Rectangle::draw() { + int x_start = position.x - (width / 2); + int x_stop = position.x + (width / 2); + + int y_start = position.y - (height / 2); + int y_stop = position.y + (height / 2); + + for(int i = x_start; i <= x_stop; i++) { + ansiConsole.printText(i, position.y - (height / 2), "#", color); + ansiConsole.printText(i, position.y + (height / 2), "#", color); + } + for(int i = y_start; i< y_stop; i++) { + ansiConsole.printText(position.x + (width / 2), i, "#", color); + ansiConsole.printText(position.x - (width / 2), i, "#", color); + } +} + +class Scene { +private: + std::vector shapes; +public: + explicit Scene(std::vector shapes); + void draw(); + ~Scene(); +}; + +Scene::Scene(std::vector shapes) : shapes(std::move(shapes)) { + +} + +void Scene::draw() { + ansiConsole.clearScreen(); + for(Shape* shape : shapes) { + shape->draw(); + } +} + +Scene::~Scene() { + for(Shape* shape : shapes) { + delete shape; + } +} + +int main(int argc, char **argv) { + std::vector shapes; + + shapes.push_back(new Point(30, 10, Colors::RED)); + shapes.push_back(new Point(28, 8, Colors::BLUE)); + shapes.push_back(new Point(32, 8, Colors::BLUE)); + + + shapes.push_back(new Circle(30, 10, 10, Colors::WHITE)); + shapes.push_back(new Circle(30, 20, 15, Colors::GREEN)); + + + shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA)); + + + auto *s = new Scene(shapes); + s->draw(); + + delete s; + + + return 0; +} diff --git a/06_POLY/Testat/shapes_main.cpp b/06_POLY/Testat/shapes_main.cpp new file mode 100644 index 0000000..f09dcb8 --- /dev/null +++ b/06_POLY/Testat/shapes_main.cpp @@ -0,0 +1,160 @@ +#include "../../helpers/AnsiConsole.h" +#include +#include + +struct Position { + int x; + int y; + + explicit Position(int x_ = 0, int y_ = 0) { + x = x_; + y = y_; + } +}; + +class Shape { +protected: + Position position; + Colors color; +public: + Shape(Position position, Colors color); + virtual ~Shape(); + virtual void draw() = 0; +}; + +Shape::Shape(Position position, Colors color) { + this->position = position; + this->color = color; +} + +Shape::~Shape() = default; + +class Point : public Shape{ + +public: + explicit Point(int x, int y, Colors color); + void draw() override; +}; + +Point::Point(int x, int y, Colors color) : Shape(Position(x, y), color) { + +} + +void Point::draw() { + ansiConsole.printText(position.x, position.y, "*", color); +} + +class Circle : public Shape{ +protected: + int _radius; +public: + explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN); + + void draw() override; +}; + +Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) { + _radius = radius; +} + +void Circle::draw() { + /* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie + * Höhensatz des Euklid + * */ + int x_start = position.x - _radius / 2; + int x_stop = position.x + _radius / 2; + + for (int i = x_start; i <= x_stop; i++) { + double x_relative = double(i) - double(x_start); + double h = sqrt(x_relative * (x_stop - x_start - x_relative)); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y + h), "#", + color); + ansiConsole.printText(position.x + int(x_relative) - _radius / 2, + static_cast(position.y - h), "#", + color); + + } +} + +class Rectangle : public Shape{ +protected: + int width; + int height; + +public: + explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE); + void draw() override; +}; + +Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) { + this->width = width; + this->height = height; +} + +void Rectangle::draw() { + int x_start = position.x - (width / 2); + int x_stop = position.x + (width / 2); + + int y_start = position.y - (height / 2); + int y_stop = position.y + (height / 2); + + for(int i = x_start; i <= x_stop; i++) { + ansiConsole.printText(i, position.y - (height / 2), "#", color); + ansiConsole.printText(i, position.y + (height / 2), "#", color); + } + for(int i = y_start; i< y_stop; i++) { + ansiConsole.printText(position.x + (width / 2), i, "#", color); + ansiConsole.printText(position.x - (width / 2), i, "#", color); + } +} + +class Scene { +private: + std::vector shapes; +public: + explicit Scene(std::vector shapes); + void draw(); + ~Scene(); +}; + +Scene::Scene(std::vector shapes) : shapes(std::move(shapes)) { + +} + +void Scene::draw() { + ansiConsole.clearScreen(); + for(Shape* shape : shapes) { + shape->draw(); + } +} + +Scene::~Scene() { + for(Shape* shape : shapes) { + delete shape; + } +} + +int main(int argc, char **argv) { + std::vector shapes; + + shapes.push_back(new Point(30, 10, Colors::RED)); + shapes.push_back(new Point(28, 8, Colors::BLUE)); + shapes.push_back(new Point(32, 8, Colors::BLUE)); + + + shapes.push_back(new Circle(30, 10, 10, Colors::WHITE)); + shapes.push_back(new Circle(30, 20, 15, Colors::GREEN)); + + + shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA)); + + + auto *s = new Scene(shapes); + s->draw(); + + delete s; + + + return 0; +} diff --git a/07_STD/MP/grundgeruest.cpp b/07_STD/MP/grundgeruest.cpp index 3b33a04..f242ce5 100644 --- a/07_STD/MP/grundgeruest.cpp +++ b/07_STD/MP/grundgeruest.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include "grundgeruest.hpp" @@ -39,26 +41,37 @@ int stringSimilarity(std::string string1, std::string string2) { //Anzahl der verschiedenen Zeichen zählen std::unordered_map string1map; std::unordered_map string2map; + std::unordered_map characterCount; for(char c : string1) { string1map[c]++; + characterCount[c]++; } for(char c : string2) { string2map[c]++; + characterCount[c]++; } //gleiche Anzahl an verschiedenen Zeichen +10 Punkte if(string1map.size() == string2map.size()) points += 10; + //auf gleiche Zeichenanzahl testen - int equalChars = 0; + //gleiche Anzahl an gleichen Zeichen int equalCharCount = 0; - for(std::pair count : string1map) { - if(string2map.find(count.first) != string2map.end()) equalChars++; - if(string2map[count.first] == count.second) equalCharCount++; + int equalChars = 0; + for(std::pair count : characterCount) { + if(string1map[count.first] == string2map[count.second]) equalCharCount++; + if(string1map[count.first] != 0 && string2map[count.first] != 0) equalChars++; + } + if(equalChars > 0) { + points += characterCount.size() / equalChars * 10; + } + if(equalCharCount > 0) { + points += characterCount.size() / equalCharCount; } - //TODO: Pnukte für gleiche Buchstaben und gleiche Buchstaben Anzahl vergeben + //TODO: Punkte für gleiche Buchstaben und gleiche Buchstaben Anzahl vergeben return points; @@ -66,6 +79,26 @@ int stringSimilarity(std::string string1, std::string string2) { void unittest_stringSimilarity() { std::cout << "Hallo Welt | Hallo Welt ---> " << stringSimilarity("Hallo Welt", "Hallo Welt") << std::endl; + std::cout << "Hallo Welt | Hallo Baum --->" << stringSimilarity("Halllo Welt", "Hallo Baum") << std::endl; std::cout << "Hello World | Hallo Welt ---> " << stringSimilarity("Hello World", "Hallo Welt") << std::endl; std::cout << "bla | ping ---> " << stringSimilarity("bla", "ping") << std::endl; + std::cout << "Lorem ipsum..... | Lorem ipsum ---> " << stringSimilarity("Lorem ipsum dolor sit amet, " + "consetetur sadipscing elitr, " + "sed diam nonumy eirmod tempor" + "invidunt ut labore et dolore " + "magna aliquyam erat, sed diam voluptua. " + "At vero eos et accusam et justo duo dolores " + "et ea rebum. Stet clita kasd gubergren, " + "no sea takimata sanctus est Lorem " + "ipsum dolor sit amet. " + "Lorem ipsum dolor sit amet, " + "consetetur sadipscing elitr, " + "sed diam nonumy eirmod tempor " + "invidunt ut labore et dolore magna " + "aliquyam erat, sed diam voluptua. " + "At vero eos et accusam et justo duo dolores " + "et ea rebum. Stet clita kasd gubergren, " + "no sea takimata sanctus est Lorem ipsum " + "dolor sit amet.", + "Lorem ipsum") << std::endl; } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index fd3be0c..7969eb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.3) project(C/C++) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +SET(CMAKE_BUILD_TYPE Debug) # These are the corresponding output paths set (EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin) @@ -23,8 +24,8 @@ add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp) add_executable(05_OO_MP 05_OO/MP/shapes_main.cpp helpers/AnsiConsole.cpp) add_executable(05_OO_Testat 05_OO/Testat/shapes_main.cpp helpers/AnsiConsole.cpp) -add_executable(06_POLY_MP 06_POLY/MP/main_mp5_POLY.cpp) -#add_executable(06_POLY_Testat 06_POLY/Testat/main_mp5_POLY.cpp) +add_executable(06_POLY_MP 06_POLY/MP/shapes_main.cpp helpers/AnsiConsole.cpp) +add_executable(06_POLY_Testat 06_POLY/Testat/shapes_main.cpp helpers/AnsiConsole.cpp) add_executable(07_STD_MP 07_STD/MP/grundgeruest.cpp) add_executable(07_STD_Testat 07_STD/Testat/grundgeruest.cpp)