diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-10-03 10:34:53 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-10-03 10:34:53 +0200 |
commit | 6cda6a35647b5faad8914b66f277e5bf645bbfd2 (patch) | |
tree | 7adb1dd155bc6c1ac09016a637ffa1fcaae27bf7 | |
parent | 5c8d2ae4da91571f92f26d6e8352538c043e6b56 (diff) |
week 6 huiswerk toegevoegd
-rw-r--r-- | week-6/Punt.cpp | 17 | ||||
-rw-r--r-- | week-6/Punt.h | 17 | ||||
-rw-r--r-- | week-6/PuntContainer.cpp | 69 | ||||
-rw-r--r-- | week-6/PuntContainer.h | 35 | ||||
-rw-r--r-- | week-6/main.cpp | 23 | ||||
-rw-r--r-- | week-6/makefile | 24 |
6 files changed, 185 insertions, 0 deletions
diff --git a/week-6/Punt.cpp b/week-6/Punt.cpp new file mode 100644 index 0000000..707047e --- /dev/null +++ b/week-6/Punt.cpp @@ -0,0 +1,17 @@ +#include "Punt.h" + +Punt::Punt() { + this->x = this->y = this->z = 0; +} + +Punt::Punt(int x, int y, int z) { + this->x = x; + this->y = y; + this->z = z; +} + +std::ostream& operator << ( std::ostream& output, const Punt& punt ) { + output << "(" << punt.x << "," << punt.y << "," << punt.z << ")"; + return output; +} + diff --git a/week-6/Punt.h b/week-6/Punt.h new file mode 100644 index 0000000..b56e798 --- /dev/null +++ b/week-6/Punt.h @@ -0,0 +1,17 @@ +#pragma once + +#include <iostream> + +class Punt +{ +private: + Punt(); + Punt( int x, int y, int z ); + + friend std::ostream& operator << ( std::ostream& output, const Punt& punt ); + +private: + int x, y, z; + + friend class PuntContainer; +}; diff --git a/week-6/PuntContainer.cpp b/week-6/PuntContainer.cpp new file mode 100644 index 0000000..59b5fd7 --- /dev/null +++ b/week-6/PuntContainer.cpp @@ -0,0 +1,69 @@ +#include "PuntContainer.h" +#include "Punt.h" + +int PuntContainer::checkX(const Punt& punt1, const Punt& punt2) { + return punt1.x - punt2.x; +} + +int PuntContainer::checkY(const Punt& punt1, const Punt& punt2) { + return punt1.y - punt2.y; +} + +int PuntContainer::checkZ(const Punt& punt1, const Punt& punt2) { + return punt1.z - punt2.z; +} + +int PuntContainer::checkX2(const Punt& punt1, const Punt& punt2) const { + return punt1.x - punt2.x; +} + +int PuntContainer::checkY2(const Punt& punt1, const Punt& punt2) const { + return punt1.y - punt2.y; +} + +int PuntContainer::checkZ2(const Punt& punt1, const Punt& punt2) const { + return punt1.z - punt2.z; +} + +PuntContainer::PuntContainer() { + this->puntRij = (Punt*) malloc(sizeof(Punt) * 10); +} + +PuntContainer::~PuntContainer() { + free(this->puntRij); +} + +void PuntContainer::add(int x, int y, int z) { + puntRij[grootte].x = x; + puntRij[grootte].y = y; + puntRij[grootte].z = z; + grootte++; +} + +void PuntContainer::sort1(int (*fnCompare)(const Punt&, const Punt&)) { + for ( uint32_t outer = grootte-1; outer > 0; outer-- ) + for ( uint32_t inner=0; inner < outer; inner++ ) + if ( (*fnCompare)(puntRij[inner],puntRij[inner+1]) > 0 ) { + Punt temp = puntRij[inner]; + puntRij[inner] = puntRij[inner+1]; + puntRij[inner+1] = temp; + } +} + +void PuntContainer::sort2(int (PuntContainer::*fnCompare)(const Punt&, const Punt&) const) { + for ( uint32_t outer = grootte-1; outer > 0; outer-- ) + for ( uint32_t inner=0; inner < outer; inner++ ) { + if ( (*this.*fnCompare)(puntRij[inner],puntRij[inner+1]) > 0 ) { + Punt temp = puntRij[inner]; + puntRij[inner] = puntRij[inner+1]; + puntRij[inner+1] = temp; + } + } +} + +void PuntContainer::showAll() { + for (uint32_t i = 0; i < this->grootte; i++) + std::cout << this->puntRij[i] << std::endl; + std::cout << std::endl; +} + diff --git a/week-6/PuntContainer.h b/week-6/PuntContainer.h new file mode 100644 index 0000000..58c7875 --- /dev/null +++ b/week-6/PuntContainer.h @@ -0,0 +1,35 @@ +#pragma once + +class Punt; + +#include <cstdint> + +class PuntContainer +{ +public: + PuntContainer(); + virtual ~PuntContainer(); + +public: + virtual void add( int x, int y, int z ); + + virtual void sort1( int (*)( const Punt&, const Punt&) ); + virtual void sort2( int (PuntContainer::*)( const Punt&, const Punt&) const ); + + virtual void showAll(); + +public: + static int checkX( const Punt& punt1, const Punt& punt2 ); + static int checkY( const Punt& punt1, const Punt& punt2 ); + static int checkZ( const Punt& punt1, const Punt& punt2 ); + +public: + virtual int checkX2( const Punt& punt1, const Punt& punt2 ) const; + virtual int checkY2( const Punt& punt1, const Punt& punt2 ) const; + virtual int checkZ2( const Punt& punt1, const Punt& punt2 ) const; + +private: + Punt* puntRij; + uint32_t grootte; +}; + diff --git a/week-6/main.cpp b/week-6/main.cpp new file mode 100644 index 0000000..701ac33 --- /dev/null +++ b/week-6/main.cpp @@ -0,0 +1,23 @@ +#include "PuntContainer.h" + +#include <iostream> + +int main() +{ + PuntContainer container; + + container.add( 1, 3, 4 ); + container.add( 3, 2, 1 ); + container.add( 2, 4, 2 ); + + container.sort2( &PuntContainer::checkZ2 ); + container.showAll(); + + container.sort2( &PuntContainer::checkY2 ); + container.showAll(); + + container.sort2( &PuntContainer::checkX2 ); + container.showAll(); + + return 0; +} diff --git a/week-6/makefile b/week-6/makefile new file mode 100644 index 0000000..3b71b5b --- /dev/null +++ b/week-6/makefile @@ -0,0 +1,24 @@ +CC = gcc +LD = gcc +RM = rm -f +TARGET = main + +LFLAGS += -lstdc++ + +SRCS := $(wildcard *.cpp) +OBJS := $(patsubst %.cpp,%.o, $(SRCS)) + +all: main + +%.o: %.cpp + $(CC) -c $(CFLAGS) $< -o $@ + +$(TARGET): $(OBJS) + $(LD) $^ $(LFLAGS) -o $@ + +clean: + $(RM) $(TARGET) $(OBJS) + +compile_commands: clean + compiledb make + |