07_STD MP Grundgerüst stringSimilarity angefangen
Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
8177214669
commit
4b7bdc4160
|
@ -10,48 +10,42 @@ struct PascalString{
|
|||
char characters[256]; // chars of some character string
|
||||
};
|
||||
|
||||
void printPascalString(PascalString string) {
|
||||
for(int i = 0; i <= string.length; ++i) {
|
||||
print(string.characters[i]);
|
||||
void printPascalString(PascalString s) {
|
||||
for(int i = 0; i <= s.length; ++i) {
|
||||
print(s.characters[i]);
|
||||
}
|
||||
println("");
|
||||
|
||||
}
|
||||
|
||||
PascalString dualOr(PascalString a, PascalString b) {
|
||||
PascalString longString = a.length >= b.length ? a : b;
|
||||
PascalString shortString = a.length >= b.length ? b : a;
|
||||
|
||||
PascalString bitwiseDualOr(PascalString a, PascalString b) {
|
||||
PascalString shortString = a.length <= b.length ? a : b;
|
||||
PascalString longString = a.length <= b.length ? b : a;
|
||||
|
||||
PascalString result = {longString.length};
|
||||
|
||||
/*
|
||||
for(int i = longString.length; i >= 0; --i) {
|
||||
if(i >= shortString.length) result.characters[i] = '0';
|
||||
else result.characters[i] = shortString.characters[i];
|
||||
for(int i = 1; i <= shortString.length; ++i) {
|
||||
result.characters[result.length -i] = longString.characters[longString.length -i] | shortString.characters[shortString.length -i];
|
||||
//println(result.length -i, ": ",result.characters[result.length -i]," l=",longString.length -i, ":",longString.characters[longString.length -i]," s=",shortString.length -i,":", shortString.characters[shortString.length -i]);
|
||||
}
|
||||
*/
|
||||
for(int i = 0; i < longString.length; i++) {
|
||||
if(i <= shortString.length) {
|
||||
for(int i = (longString.length - shortString.length)-1; i >= 0 ; --i) {
|
||||
result.characters[i] = longString.characters[i];
|
||||
//println("lang ", longString.characters[i]);
|
||||
//println(i, ": ", result.characters[i]);
|
||||
}
|
||||
else{
|
||||
result.characters[i] = longString.characters[i] | shortString.characters[i];
|
||||
//println("beides ", shortString.characters[i] | longString.characters[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
printPascalString(dualOr({2, '0', '1'}, {2, '1', '0'}));
|
||||
printPascalString(dualOr({2, '0', '1'}, {3, '1', '1', '1'}));
|
||||
printPascalString(dualOr({3, '1', '1', '1'}, {4, '1', '1', '0', '1'}));
|
||||
printPascalString(dualOr({3, '0', '0', '0'}, {5, '1', '1', '0', '1', '0'}));
|
||||
printPascalString(dualOr({3, '0', '1', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||
printPascalString(dualOr({2, '0', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||
printPascalString(dualOr({2, '1', '1'}, {6, '1', '0', '0', '1', '0', '0'}));
|
||||
|
||||
printPascalString(bitwiseDualOr({2, '0', '1'}, {2, '1', '0'}));
|
||||
printPascalString(bitwiseDualOr({2, '0', '1'}, {3, '1', '1', '1'}));
|
||||
printPascalString(bitwiseDualOr({3, '1', '1', '1'}, {4, '1', '1', '0', '1'}));
|
||||
printPascalString(bitwiseDualOr({3, '0', '0', '0'}, {5, '1', '1', '0', '1', '0'}));
|
||||
printPascalString(bitwiseDualOr({3, '0', '1', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||
printPascalString(bitwiseDualOr({2, '0', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||
printPascalString(bitwiseDualOr({2, '1', '1'}, {6, '1', '0', '0', '1', '0', '0'}));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
rm *.out
|
||||
|
||||
clang++-6.0 -std=c++14 main_02_MENT.cpp
|
||||
|
||||
./a.out
|
|
@ -1,51 +0,0 @@
|
|||
// file: main_02_MENT.cpp
|
||||
// THIS IS C++, use clang++
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "../../helpers/println.hpp"
|
||||
|
||||
struct PascalString{
|
||||
int length; // number of chars used
|
||||
char characters[256]; // chars of some character string
|
||||
};
|
||||
|
||||
void printPascalString(PascalString s) {
|
||||
for(int i = 0; i <= s.length; ++i) {
|
||||
print(s.characters[i]);
|
||||
}
|
||||
println("");
|
||||
|
||||
}
|
||||
|
||||
PascalString bitwiseDualOr(PascalString a, PascalString b) {
|
||||
PascalString shortString = a.length <= b.length ? a : b;
|
||||
PascalString longString = a.length <= b.length ? b : a;
|
||||
|
||||
PascalString result = {longString.length};
|
||||
|
||||
for(int i = 1; i <= shortString.length; ++i) {
|
||||
result.characters[result.length -i] = longString.characters[longString.length -i] | shortString.characters[shortString.length -i];
|
||||
//println(result.length -i, ": ",result.characters[result.length -i]," l=",longString.length -i, ":",longString.characters[longString.length -i]," s=",shortString.length -i,":", shortString.characters[shortString.length -i]);
|
||||
}
|
||||
for(int i = (longString.length - shortString.length)-1; i >= 0 ; --i) {
|
||||
result.characters[i] = longString.characters[i];
|
||||
//println(i, ": ", result.characters[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
|
||||
printPascalString(bitwiseDualOr({2, '0', '1'}, {2, '1', '0'}));
|
||||
printPascalString(bitwiseDualOr({2, '0', '1'}, {3, '1', '1', '1'}));
|
||||
printPascalString(bitwiseDualOr({3, '1', '1', '1'}, {4, '1', '1', '0', '1'}));
|
||||
printPascalString(bitwiseDualOr({3, '0', '0', '0'}, {5, '1', '1', '0', '1', '0'}));
|
||||
printPascalString(bitwiseDualOr({3, '0', '1', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||
printPascalString(bitwiseDualOr({2, '0', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||
printPascalString(bitwiseDualOr({2, '1', '1'}, {6, '1', '0', '0', '1', '0', '0'}));
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#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;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
#!/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
|
|
@ -0,0 +1,105 @@
|
|||
#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;
|
||||
}
|
|
@ -0,0 +1,217 @@
|
|||
// 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;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,113 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include "../../helpers/AnsiConsole.h"
|
||||
|
||||
struct Position {
|
||||
int x;
|
||||
int y;
|
||||
|
||||
Position(int x_ = 0, int y_ = 0) {
|
||||
x = x_;
|
||||
y = y_;
|
||||
}
|
||||
};
|
||||
|
||||
class Point {
|
||||
protected:
|
||||
Position _position;
|
||||
public:
|
||||
Point(int x = 0, int y = 0);
|
||||
|
||||
void draw();
|
||||
};
|
||||
|
||||
Point::Point(int x, int y) {
|
||||
_position = Position(x, y);
|
||||
}
|
||||
|
||||
void Point::draw() {
|
||||
ansiConsole.printText(_position.x, _position.y, "*", Colors::RED);
|
||||
}
|
||||
|
||||
class Circle {
|
||||
protected:
|
||||
Position _position;
|
||||
int _radius;
|
||||
public:
|
||||
Circle(int x = 0, int y = 0, int radius = 0);
|
||||
|
||||
void draw();
|
||||
};
|
||||
|
||||
Circle::Circle(int x, int y, int radius) {
|
||||
_position = Position(x, y);
|
||||
_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,
|
||||
_position.y + h, "#",
|
||||
Colors::GREEN);
|
||||
ansiConsole.printText(_position.x + int(x_relative) - _radius / 2,
|
||||
_position.y - h, "#",
|
||||
Colors::GREEN);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class Rectangle {
|
||||
protected:
|
||||
Position position;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
public:
|
||||
Rectangle(int x = 0, int y = 0, int width = 0, int height = 0);
|
||||
void draw();
|
||||
};
|
||||
|
||||
Rectangle::Rectangle(int x, int y, int width, int height) {
|
||||
position = Position(x, y);
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
}
|
||||
|
||||
void Rectangle::draw() {
|
||||
|
||||
}
|
||||
|
||||
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!");
|
||||
|
||||
Point *p = new Point(10, 10);
|
||||
p->draw();
|
||||
|
||||
Point *p2 = new Point(2, 10);
|
||||
p2->draw();
|
||||
|
||||
|
||||
Circle *c = new Circle(30, 15, 10);
|
||||
c->draw();
|
||||
|
||||
Point *p3 = new Point(30, 15);
|
||||
p3->draw();
|
||||
|
||||
|
||||
delete p;
|
||||
delete p2;
|
||||
delete p3;
|
||||
delete c;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,281 @@
|
|||
// file main_mp5_POLY.cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class Base {
|
||||
public:
|
||||
void overriddenMethod();
|
||||
void overriddenMethod(int);
|
||||
void overriddenMethod(std::string);
|
||||
void nonVirtualMethod(void);
|
||||
virtual void virtualMethod(void);
|
||||
/*
|
||||
virtual void virtualAndOverridden(void);
|
||||
virtual void virtualAndOverridden(int);
|
||||
virtual void virtualAndOverridden(std::string);*/
|
||||
};
|
||||
|
||||
class Derived_1 : public Base {
|
||||
public:
|
||||
Derived_1();
|
||||
~Derived_1();
|
||||
void nonVirtualMethod(void);
|
||||
virtual void virtualMethod(void);
|
||||
|
||||
/* virtual void virtualAndOverridden(void);
|
||||
virtual void virtualAndOverridden(int);
|
||||
virtual void virtualAndOverridden(std::string);*/
|
||||
};
|
||||
|
||||
class Derived_2 : public Derived_1 {
|
||||
public:
|
||||
void nonVirtualMethod(void);
|
||||
virtual void virtualMethod(void);
|
||||
};
|
||||
|
||||
// see https://isocpp.org/wiki/faq/strange-inheritance#calling-virtuals-from-ctors
|
||||
Derived_1::Derived_1(){
|
||||
virtualMethod(); // 6a
|
||||
}
|
||||
|
||||
Derived_1::~Derived_1(){
|
||||
virtualMethod(); // 6b
|
||||
}
|
||||
|
||||
// -------------------
|
||||
void Base::overriddenMethod(void){
|
||||
std::cout << "Base::overriddenMethod(void)" << std::endl;
|
||||
}
|
||||
|
||||
void Base::overriddenMethod(int){
|
||||
std::cout << "Base::overriddenMethod(int)" << std::endl;
|
||||
}
|
||||
|
||||
void Base::overriddenMethod(std::string){
|
||||
std::cout << "Base::overriddenMethod(std::string)" << std::endl;
|
||||
}
|
||||
|
||||
void Base::nonVirtualMethod(void){
|
||||
std::cout << "Base::nonVirtualMethod(void)" << std::endl;
|
||||
}
|
||||
|
||||
void Base::virtualMethod(void){
|
||||
std::cout << "Base::virtualMethod" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
void Base::virtualAndOverridden(void){
|
||||
std::cout << "Base::virtualAndOverridden(void)" << std::endl;
|
||||
}
|
||||
|
||||
void Base::virtualAndOverridden(int){
|
||||
std::cout << "Base::virtualAndOverridden(int)" << std::endl;
|
||||
}
|
||||
|
||||
void Base::virtualAndOverridden(std::string){
|
||||
std::cout << "Base::virtualAndOverridden(std::string)" << std::endl;
|
||||
}
|
||||
*/
|
||||
|
||||
// -------------------
|
||||
void Derived_1::nonVirtualMethod(void){
|
||||
std::cout << "Derived_1::nonVirtualMethod" << std::endl;
|
||||
}
|
||||
void Derived_1::virtualMethod(void){
|
||||
std::cout << "Derived_1::virtualMethod" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
void Derived_1::virtualAndOverridden(void){
|
||||
std::cout << "Derived_1::virtualAndOverridden(void)" << std::endl;
|
||||
}
|
||||
|
||||
void Derived_1::virtualAndOverridden(int){
|
||||
std::cout << "Derived_1::virtualAndOverridden(int)" << std::endl;
|
||||
}
|
||||
|
||||
void Derived_1::virtualAndOverridden(std::string){
|
||||
std::cout << "Derived_1::virtualAndOverridden(std::string)" << std::endl;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
// -------------------
|
||||
void Derived_2::nonVirtualMethod(void){
|
||||
std::cout << "Derived_2::nonVirtualMethod" << std::endl;
|
||||
}
|
||||
void Derived_2::virtualMethod(void){
|
||||
std::cout << "Derived_2::virtualMethod" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
illustrates binding of method invocations
|
||||
compile time vs. run time binding
|
||||
*/
|
||||
|
||||
void foobar(){
|
||||
|
||||
std::cout << "6a) ";
|
||||
Derived_2* pR = new Derived_2();
|
||||
|
||||
Base* pBase = pR;
|
||||
Derived_1* pDerived_1 = pR;
|
||||
Derived_2* pDerived_2 = pR;
|
||||
|
||||
std::cout << "1) ";
|
||||
pBase->virtualMethod(); // 1)
|
||||
|
||||
std::cout << "2) ";
|
||||
pBase->nonVirtualMethod(); // 2)
|
||||
|
||||
std::cout << "3) ";
|
||||
pDerived_2->virtualMethod(); // 3)
|
||||
|
||||
std::cout << "4) ";
|
||||
pDerived_2->nonVirtualMethod(); // 4)
|
||||
|
||||
std::cout << "5) ";
|
||||
static_cast<Base*>(pDerived_2)->nonVirtualMethod(); // 5)
|
||||
|
||||
std::cout << "7) ";
|
||||
static_cast<Derived_1*>(pDerived_2)->nonVirtualMethod(); // 7)
|
||||
|
||||
std::cout << "8) ";
|
||||
pDerived_1->nonVirtualMethod(); // 8)
|
||||
|
||||
std::cout << "9) ";
|
||||
pDerived_1->virtualMethod(); // 9)
|
||||
|
||||
std::cout << "10a) ";
|
||||
pBase->Base::virtualMethod(); // 10a)
|
||||
|
||||
std::cout << "10b) ";
|
||||
pDerived_2->Derived_1::virtualMethod(); // 10b)
|
||||
|
||||
std::cout << "11 ";
|
||||
pDerived_2->overriddenMethod(17); // 11)
|
||||
|
||||
std::cout << "12 ";
|
||||
pDerived_2->overriddenMethod(); // 12)
|
||||
|
||||
std::cout << "13 ";
|
||||
pDerived_2->overriddenMethod(std::string("x")); // 13)
|
||||
|
||||
std::cout << "14 ";
|
||||
dynamic_cast<Derived_2*>(pBase)->virtualMethod(); // 14)
|
||||
|
||||
std::cout << "15 ";
|
||||
Base* baseObject = new Base();
|
||||
Derived_2* d2Object = dynamic_cast<Derived_2*>(baseObject);
|
||||
if(d2Object){ // 15)
|
||||
std::cout << "+" << std::endl;
|
||||
}else{
|
||||
std::cout << "-" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
std::cout << "16 ";
|
||||
pDerived_2->virtualAndOverridden(17); // 16)
|
||||
|
||||
std::cout << "17 ";
|
||||
pDerived_2->virtualAndOverridden(); // 17)
|
||||
|
||||
std::cout << "18 ";
|
||||
pDerived_2->virtualAndOverridden(std::string("x")); // 18)
|
||||
*/
|
||||
|
||||
|
||||
std::cout << "6b) ";
|
||||
delete pR;
|
||||
}
|
||||
|
||||
/*
|
||||
void foo(){
|
||||
for(int i=0; i<LOOPS;i++){
|
||||
Base* ps = new Base();
|
||||
ps->nonVirtualMethod2();
|
||||
delete ps;
|
||||
}
|
||||
}
|
||||
|
||||
void bar(int i){
|
||||
Base s;
|
||||
s.nonVirtualMethod2();
|
||||
if(i>0){
|
||||
bar(i-1);
|
||||
}
|
||||
}*/
|
||||
|
||||
typedef void (*funcPointer_void_void)(void);
|
||||
|
||||
void sing_gingle(void){
|
||||
std::cout << "gingle ";
|
||||
}
|
||||
|
||||
void sing_bells(void){
|
||||
std::cout << "bells ";
|
||||
}
|
||||
|
||||
void sing_surf(void){
|
||||
std::cout << "surf ";
|
||||
}
|
||||
|
||||
void sing_safari(void){
|
||||
std::cout << "surfin safari ";
|
||||
}
|
||||
|
||||
std::vector<funcPointer_void_void> songOfSeason(int currentTemperatureCelsius) {
|
||||
std::vector<funcPointer_void_void> result;
|
||||
|
||||
if(currentTemperatureCelsius <= 0){
|
||||
result.push_back(sing_gingle);
|
||||
result.push_back(sing_bells);
|
||||
result.push_back(sing_gingle);
|
||||
result.push_back(sing_bells);
|
||||
}else{
|
||||
result.push_back(sing_surf);
|
||||
result.push_back(sing_surf);
|
||||
result.push_back(sing_safari);
|
||||
result.push_back(sing_surf);
|
||||
result.push_back(sing_surf);
|
||||
result.push_back(sing_safari);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void sing(int currentTemperatureCelsius){
|
||||
std::vector<funcPointer_void_void> song = songOfSeason(currentTemperatureCelsius);
|
||||
|
||||
// traditional C-style for-loop
|
||||
for(int i=0; i<song.size(); ++i){
|
||||
song[i](); // invoke funtion (pointed to by function pointer at song[i])
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// C++11 range-based for-loop looks better:
|
||||
for(auto func: song){
|
||||
func();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
|
||||
foobar();
|
||||
|
||||
sing(-17);
|
||||
|
||||
/*
|
||||
std::cout << "foo " << std::endl;
|
||||
foo();
|
||||
|
||||
std::cout << "bar " << std::endl;
|
||||
bar(LOOPS);
|
||||
*/
|
||||
return 0;
|
||||
}
|
|
@ -2,16 +2,70 @@
|
|||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <random>
|
||||
#include <unordered_map>
|
||||
#include "grundgeruest.hpp"
|
||||
|
||||
struct BinaryOctet {
|
||||
bool evenParity;
|
||||
char bitsAsDigits[bitsPerOctet];
|
||||
char bitsAsDigits[8];
|
||||
|
||||
BinaryOctet(int i) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::srand(std::time(0)); // use current time as seed for random generator
|
||||
int random_variable = std::rand();
|
||||
std::cout << "Random value on [0 " << RAND_MAX << "]: "
|
||||
<< random_variable << '\n';
|
||||
int main() {
|
||||
int array[41];
|
||||
|
||||
std::default_random_engine generator;
|
||||
std::uniform_int_distribution<int> distribution(0, 23);
|
||||
|
||||
for(int i = 0; i <= 41; i++) {
|
||||
array[i] = distribution(generator);
|
||||
}
|
||||
|
||||
unittest_stringSimilarity();
|
||||
}
|
||||
|
||||
int stringSimilarity(std::string string1, std::string string2) {
|
||||
//wenn gleicher String, gleich 100 Punkte geben, spart den Rest
|
||||
if(string1 == string2) return 100;
|
||||
int points = 0;
|
||||
|
||||
//gleiche Länge +10 Punkte
|
||||
if(string1.length() == string2.length()) points += 10;
|
||||
|
||||
//Anzahl der verschiedenen Zeichen zählen
|
||||
std::unordered_map<char, int> string1map;
|
||||
std::unordered_map<char, int> string2map;
|
||||
|
||||
for(char c : string1) {
|
||||
string1map[c]++;
|
||||
}
|
||||
for(char c : string2) {
|
||||
string2map[c]++;
|
||||
}
|
||||
|
||||
//gleiche Anzahl an verschiedenen Zeichen +10 Punkte
|
||||
if(string1map.size() == string2map.size()) points += 10;
|
||||
|
||||
//auf gleiche Zeichenanzahl testen
|
||||
int equalChars = 0;
|
||||
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++;
|
||||
}
|
||||
|
||||
//TODO: Pnukte für gleiche Buchstaben und gleiche Buchstaben Anzahl vergeben
|
||||
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
void unittest_stringSimilarity() {
|
||||
std::cout << "Hallo Welt | Hallo Welt ---> " << stringSimilarity("Hallo Welt", "Hallo Welt") << std::endl;
|
||||
std::cout << "Hello World | Hallo Welt ---> " << stringSimilarity("Hello World", "Hallo Welt") << std::endl;
|
||||
std::cout << "bla | ping ---> " << stringSimilarity("bla", "ping") << std::endl;
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
int stringSimilarity(std::string string1, std::string string2);
|
||||
void unittest_stringSimilarity();
|
|
@ -0,0 +1,17 @@
|
|||
// based on example from http://en.cppreference.com/w/cpp/numeric/random/rand
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
struct BinaryOctet {
|
||||
bool evenParity;
|
||||
char bitsAsDigits[bitsPerOctet];
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::srand(std::time(0)); // use current time as seed for random generator
|
||||
int random_variable = std::rand();
|
||||
std::cout << "Random value on [0 " << RAND_MAX << "]: "
|
||||
<< random_variable << '\n';
|
||||
}
|
|
@ -7,10 +7,8 @@ add_executable(01_ENV_MP 01_ENV/MP/main.c 01_ENV/MP/func1.c)
|
|||
target_link_libraries(01_ENV_MP m)
|
||||
add_executable(01_ENV_Testat 01_ENV/Testat/main.c 01_ENV/Testat/func1.c)
|
||||
|
||||
|
||||
add_executable(02_MENT_MP 02_MENT/MP/main_02_MENT.cpp)
|
||||
add_executable(02_MENT_Testat_1 02_MENT/Testat/main_02_MENT.cpp)
|
||||
add_executable(02_MENT_Testat_2 02_MENT/Testat2/main_02_MENT.cpp)
|
||||
add_executable(02_MENT_Testat 02_MENT/Testat/main_02_MENT.cpp)
|
||||
|
||||
add_executable(03_FLOW_MP 03_FLOW_a/MP/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp)
|
||||
add_executable(03_FLOW_Testat 03_FLOW_a/Testat/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp)
|
||||
|
@ -19,13 +17,14 @@ add_executable(04_UDEF_MP_1 04_UDEF/MP/main_04_UDEF_e.cpp)
|
|||
add_executable(04_UDEF_MP_2 04_UDEF/MP/main_04_UDEF_a.cpp)
|
||||
add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp)
|
||||
|
||||
add_executable(05_OO_MP 05_OO/MP/main_mp4_OO_a_vehicles.cpp)
|
||||
#add_executable(OO_5_Testat 05_OO/Testat/)
|
||||
#add_executable(OO_B 05_OO/main_mp4_OO_b.cpp)
|
||||
|
||||
#add_executable(OO_Shapes 05_OO/shapes_main.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(07_STD_MP 07_STD/MP/grundgeruest.cpp)
|
||||
add_executable(07_STD_Testat 07_STD/Testat/grundgeruest.cpp)
|
||||
|
||||
#add_executable(SequenceDiagram 11_PUTT/05_OO_b/SequenceDiagramCreator.cpp 11_PUTT/05_OO_b/main.cpp)
|
||||
|
||||
|
@ -35,4 +34,4 @@ add_executable(06_POLY_MP 06_POLY/MP/main_mp5_POLY.cpp)
|
|||
|
||||
#add_executable(Banking 10_PITF/MP/banking_base_rawptr.cpp)
|
||||
|
||||
#add_executable(Test 11_PUTT/Test.cpp)
|
||||
add_executable(Test 11_PUTT/Test.cpp)
|
Loading…
Reference in New Issue