- 07_STD_MP: mehr Punkte vergeben

- aufgeräumt
This commit is contained in:
Johannes Theiner 2018-08-31 18:26:38 +02:00
parent 39efce229f
commit 08760f4afc
15 changed files with 477 additions and 793 deletions

View File

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

View File

@ -1,29 +0,0 @@
#include <iostream>
#include <cmath>
#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;
}

View File

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

View File

@ -1,105 +0,0 @@
#include <iostream>
#include <typeinfo>
//======================================
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;
}

View File

@ -1,217 +0,0 @@
// file main_mp4_OO_b.cpp
#include <iostream>
#include <cassert>
//=======================================
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;
}

View File

@ -1,11 +1,6 @@
#include <utility>
#include <utility>
#include <iostream>
#include "../../helpers/AnsiConsole.h"
#include <cmath>
#include <vector>
#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<Shape*> shapes;

View File

@ -1,29 +0,0 @@
#include <iostream>
#include <cmath>
#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;
}

View File

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

View File

@ -1,105 +0,0 @@
#include <iostream>
#include <typeinfo>
//======================================
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;
}

View File

@ -1,217 +0,0 @@
// file main_mp4_OO_b.cpp
#include <iostream>
#include <cassert>
//=======================================
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;
}

View File

@ -1,46 +1,59 @@
#include <iostream>
#include <cmath>
#include "../../helpers/AnsiConsole.h"
#include <cmath>
#include <vector>
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<int>(position.y + h), "#",
color);
ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(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<Shape*> shapes;
public:
explicit Scene(std::vector<Shape*> shapes);
void draw();
~Scene();
};
Scene::Scene(std::vector<Shape*> 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<Shape*> 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;
}

160
06_POLY/MP/shapes_main.cpp Normal file
View File

@ -0,0 +1,160 @@
#include "../../helpers/AnsiConsole.h"
#include <cmath>
#include <vector>
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<int>(position.y + h), "#",
color);
ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(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<Shape*> shapes;
public:
explicit Scene(std::vector<Shape*> shapes);
void draw();
~Scene();
};
Scene::Scene(std::vector<Shape*> 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<Shape*> 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;
}

View File

@ -0,0 +1,160 @@
#include "../../helpers/AnsiConsole.h"
#include <cmath>
#include <vector>
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<int>(position.y + h), "#",
color);
ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(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<Shape*> shapes;
public:
explicit Scene(std::vector<Shape*> shapes);
void draw();
~Scene();
};
Scene::Scene(std::vector<Shape*> 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<Shape*> 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;
}

View File

@ -3,6 +3,8 @@
#include <iostream>
#include <ctime>
#include <random>
#include <list>
#include <algorithm>
#include <unordered_map>
#include "grundgeruest.hpp"
@ -39,26 +41,37 @@ int stringSimilarity(std::string string1, std::string string2) {
//Anzahl der verschiedenen Zeichen zählen
std::unordered_map<char, int> string1map;
std::unordered_map<char, int> string2map;
std::unordered_map<char, int> 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<char, int> count : string1map) {
if(string2map.find(count.first) != string2map.end()) equalChars++;
if(string2map[count.first] == count.second) equalCharCount++;
int equalChars = 0;
for(std::pair<char, int> 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;
}

View File

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