From fba03cf4e13783d2d0c355c1a7d7c0799762aea5 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 4 Feb 2023 16:15:14 +0100 Subject: week 1 deel 1 (initial commit) --- .gitignore | 6 + algo1w1/NAW.cpp | 24 ++ algo1w1/NAW.h | 30 +++ algo1w1/NAWArray.cpp | 121 +++++++++ algo1w1/NAWArray.h | 31 +++ algo1w1/class-diagram.svg | 626 ++++++++++++++++++++++++++++++++++++++++++++++ algo1w1/main.cpp | 41 +++ algo1w1/makefile | 1 + makefile | 11 + week.mk | 30 +++ 10 files changed, 921 insertions(+) create mode 100644 .gitignore create mode 100644 algo1w1/NAW.cpp create mode 100644 algo1w1/NAW.h create mode 100644 algo1w1/NAWArray.cpp create mode 100644 algo1w1/NAWArray.h create mode 100644 algo1w1/class-diagram.svg create mode 100644 algo1w1/main.cpp create mode 120000 algo1w1/makefile create mode 100644 makefile create mode 100644 week.mk diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4cf3fc4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +**/*.o +**/*.pdf +**/main +**/compile_commands.json +**/.cache + diff --git a/algo1w1/NAW.cpp b/algo1w1/NAW.cpp new file mode 100644 index 0000000..72b1b29 --- /dev/null +++ b/algo1w1/NAW.cpp @@ -0,0 +1,24 @@ +#include "NAW.h" +#include + +NAW::NAW() { } + +NAW::NAW(const std::string& naam, const std::string& adres, const std::string& woonplaats) { + setNaam(naam); + setAdres(adres); + setPlaats(woonplaats); +} + +NAW::~NAW() { } + +const std::string& NAW::getNaam() const { return _naam; } +const std::string& NAW::getAdres() const { return _adres; } +const std::string& NAW::getPlaats() const { return _woonplaats; } + +void NAW::setNaam(const std::string& naam) { _naam = naam; } +void NAW::setAdres(const std::string& adres) { _adres = adres; } +void NAW::setPlaats(const std::string& woonplaats) { _woonplaats = woonplaats; } + +bool NAW::heeftNaam(const std::string& naam) const { return getNaam().compare(naam) == 0; } +bool NAW::heeftAdres(const std::string& adres) const { return getAdres().compare(adres) == 0; } +bool NAW::heeftPlaats(const std::string& woonplaats) const { return getPlaats().compare(woonplaats) == 0; } diff --git a/algo1w1/NAW.h b/algo1w1/NAW.h new file mode 100644 index 0000000..ad38133 --- /dev/null +++ b/algo1w1/NAW.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +class NAW { +public: + NAW(); + NAW(const std::string&, const std::string&, const std::string&); + virtual ~NAW(); + +public: + virtual const std::string& getNaam() const; + virtual const std::string& getAdres() const; + virtual const std::string& getPlaats() const; + +public: + virtual void setNaam(const std::string&); + virtual void setAdres(const std::string&); + virtual void setPlaats(const std::string&); + +public: + virtual bool heeftNaam(const std::string&) const; + virtual bool heeftAdres(const std::string&) const; + virtual bool heeftPlaats(const std::string&) const; + +private: + std::string _naam, _adres, _woonplaats; +}; + diff --git a/algo1w1/NAWArray.cpp b/algo1w1/NAWArray.cpp new file mode 100644 index 0000000..51742f1 --- /dev/null +++ b/algo1w1/NAWArray.cpp @@ -0,0 +1,121 @@ +#include "NAWArray.h" +#include "NAW.h" + +#include +#include + +NAWArray::NAWArray() { } +NAWArray::~NAWArray() { } + +void NAWArray::add(const NAW& newNAW) { + _nawCollection[_nawCollectionSize++] = new NAW(newNAW); +} + +int NAWArray::zoekOpEersteNaam(const std::string& naam) const { + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (!_nawCollection[i]->heeftNaam(naam)) continue; + return i; + } + return -1; +} + +int NAWArray::zoekOpEersteAdres(const std::string& adres) const { + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (!_nawCollection[i]->heeftAdres(adres)) continue; + return i; + } + return -1; +} + +int NAWArray::zoekOpEerstePlaats(const std::string& plaats) const { + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (!_nawCollection[i]->heeftPlaats(plaats)) continue; + return i; + } + return -1; +} + +int NAWArray::zoekOpEersteAdresEnPlaats(const std::string& adres, const std::string& plaats) const { + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (!_nawCollection[i]->heeftAdres(adres)) continue; + if (!_nawCollection[i]->heeftPlaats(plaats)) continue; + return i; + } + return -1; +} + +int NAWArray::verwijderEersteMetNaam(const std::string& naam) { + int x = -1; // deleted element index + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (x == -1) { + if (!_nawCollection[i]->heeftNaam(naam)) continue; + x = i; // set deleted element index + _nawCollectionSize--; // decrease container size + i++; // skip deleted element + } + if (i == 0) continue; // check if i-1 >= 0 + _nawCollection[i-1] = _nawCollection[i]; // shift elements back + } + return x; +} + +int NAWArray::verwijderLaatsteMetNaam(const std::string& naam) { + int x = -1; // deleted element index + for (unsigned _i = 0; _i < _nawCollectionSize; _i++) { + unsigned i = _nawCollectionSize - (_i + 1); // calculate index from back + if (x == -1) { + if (!_nawCollection[i]->heeftNaam(naam)) continue; + x = i; // set deleted element index + _nawCollectionSize--; // decrease container size + _i = i; // set loop index to deleted element + i++; // skip deleted element + } + i = _i; // move forward through array shifting elements back + if (i == 0) continue; // check if i-1 >= 0 + _nawCollection[i-1] = _nawCollection[i]; // shift elements back + } + return x; +} + +int NAWArray::verwijderAllenMetNaam(const std::string& naam) { + int c = 0; // deleted element count + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (_nawCollection[i]->heeftNaam(naam)) { + c++; // pun unintended + _nawCollectionSize--; // decrease container size + } + if (i + c >= _nawCollectionSize) break; + _nawCollection[i] = _nawCollection[i + c]; // shift elements back + } + return c; +} + +int NAWArray::verwijderEersteMetAdresEnPlaats(const std::string& adres, const std::string& plaats) { + int x = -1; // deleted element index + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (x == -1) { + if (!_nawCollection[i]->heeftAdres(adres)) continue; + if (!_nawCollection[i]->heeftPlaats(plaats)) continue; + x = i; // set deleted element index + _nawCollectionSize--; // decrease container size + i++; // skip deleted element + } + if (i == 0) continue; // check if i-1 >= 0 + _nawCollection[i-1] = _nawCollection[i]; // shift elements back + } + return x; +} + +int NAWArray::verwijderAllenMetAdresEnPlaats(const std::string& adres, const std::string& plaats) { + int c = 0; // deleted element count + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (_nawCollection[i]->heeftAdres(adres) && + _nawCollection[i]->heeftPlaats(plaats)) { + c++; // pun unintended + _nawCollectionSize--; // decrease container size + } + if (i + c >= _nawCollectionSize) break; + _nawCollection[i] = _nawCollection[i + c]; // shift elements back + } + return c; +} diff --git a/algo1w1/NAWArray.h b/algo1w1/NAWArray.h new file mode 100644 index 0000000..2f98885 --- /dev/null +++ b/algo1w1/NAWArray.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +class NAW; + +class NAWArray { +public: + NAWArray(); + virtual ~NAWArray(); + +public: + virtual void add(const NAW&); + +public: + virtual int zoekOpEersteNaam(const std::string&) const; + virtual int zoekOpEersteAdres(const std::string&) const; + virtual int zoekOpEerstePlaats(const std::string&) const; + virtual int zoekOpEersteAdresEnPlaats(const std::string&, const std::string&) const; + +public: + virtual int verwijderEersteMetNaam(const std::string&); + virtual int verwijderLaatsteMetNaam(const std::string&); + virtual int verwijderAllenMetNaam(const std::string&); + virtual int verwijderEersteMetAdresEnPlaats(const std::string&, const std::string&); + virtual int verwijderAllenMetAdresEnPlaats(const std::string&, const std::string&); + +private: + const NAW* _nawCollection[20] = { nullptr }; + unsigned _nawCollectionSize = 0; +}; diff --git a/algo1w1/class-diagram.svg b/algo1w1/class-diagram.svg new file mode 100644 index 0000000..0d0758a --- /dev/null +++ b/algo1w1/class-diagram.svg @@ -0,0 +1,626 @@ + + +Qt SVG Document +Generated with Qt + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0..20 + + + + + + + + + + + + + + + + + + + + + + +-_nawCollection + + + + + + + + + + + + + + + + +NAWArray + + + + + + + + + + + + + +- _nawCollection : const NAW*[20] + + + + + + +- _nawCollectionSize : unsigned + + + + + + + + + + + + + + + + + + + ++ NAWArray() «constructor» + + + + + + ++ ~NAWArray() «destructor» + + + + + + ++ add( : const NAW&) + + + + + + ++ zoekOpEersteNaam( : const std::string&) : int + + + + + + ++ zoekOpEersteAdres( : const std::string&) : int + + + + + + ++ zoekOpEerstePlaats( : const std::string&) : int + + + + + + ++ zoekOpEersteAdresEnPlaats( : const std::string&, : const std::string&) : int + + + + + + ++ verwijderEersteMetNaam( : const std::string&) : int + + + + + + ++ verwijderLaatsteMetNaam( : const std::string&) : int + + + + + + ++ verwijderAllenMetNaam( : const std::string&) : int + + + + + + ++ verwijderEersteMetAdresEnPlaats( : const std::string&, : const std::string&) : int + + + + + + ++ verwijderAllenMetAdresEnPlaats( : const std::string&, : const std::string&) : int + + + + + + + + + + + + + + + + + + + + + + + + + +NAW + + + + + + + + + + + + + +- _naam : string + + + + + + +- _adres : string + + + + + + +- _woonplaats : string + + + + + + + + + + + + + + + + + + + ++ NAW() «constructor» + + + + + + ++ NAW( : const std::string&, : const std::string&, : const std::string&) «constructor» + + + + + + ++ ~NAW() «destructor» + + + + + + ++ getNaam() : const std::string& + + + + + + ++ getAdres() : const std::string& + + + + + + ++ getPlaats() : const std::string& + + + + + + ++ setNaam( : const std::string&) + + + + + + ++ setAdres( : const std::string&) + + + + + + ++ setPlaats( : const std::string&) + + + + + + ++ heeftNaam( : const std::string&) : bool + + + + + + ++ heeftAdres( : const std::string&) : bool + + + + + + ++ heeftPlaats( : const std::string&) : bool + + + + + + + + + + + + + + + + + + diff --git a/algo1w1/main.cpp b/algo1w1/main.cpp new file mode 100644 index 0000000..9b737e5 --- /dev/null +++ b/algo1w1/main.cpp @@ -0,0 +1,41 @@ +#include "NAWArray.h" +#include "NAW.h" + +#include +#include + +int main() { + NAWArray array; + + for (int n = 0; n < 10; n++) { + std::stringstream naam, adres, plaats; + + naam << "avans " << n + 1; + adres << "onderwijsboulevard " << n + 1; + plaats << "den bosch " << n + 1; + + array.add({ naam.str(), adres.str(), plaats.str() }); + } + + std::cout << array.zoekOpEersteNaam("avans 4") << std::endl; + std::cout << array.zoekOpEersteNaam("avans 34") << std::endl; + std::cout << array.zoekOpEersteAdres("onderwijsboulevard 7") << std::endl; + std::cout << array.zoekOpEersteAdres("onderwijsboulevard 37") << std::endl; + std::cout << array.zoekOpEerstePlaats("den bosch 2") << std::endl; + std::cout << array.zoekOpEerstePlaats("den bosch 83") << std::endl; + std::cout << array.zoekOpEersteAdresEnPlaats("onderwijsboulevard 7", "den bosch 7") << std::endl; + std::cout << array.zoekOpEersteAdresEnPlaats("onderwijsboulevard 7", "den bosch 8") << std::endl; + + std::cout << array.verwijderEersteMetNaam("avans 4") << std::endl; + std::cout << array.verwijderEersteMetNaam("avans 34") << std::endl; + std::cout << array.verwijderLaatsteMetNaam("avans 5") << std::endl; + std::cout << array.verwijderLaatsteMetNaam("avans 34") << std::endl; + std::cout << array.verwijderAllenMetNaam("avans 5") << std::endl; + std::cout << array.verwijderAllenMetNaam("avans 34") << std::endl; + std::cout << array.verwijderEersteMetAdresEnPlaats("onderwijsboulevard 7", "den bosch 7") << std::endl; + std::cout << array.verwijderEersteMetAdresEnPlaats("onderwijsboulevard 7", "den bosch 8") << std::endl; + std::cout << array.verwijderAllenMetAdresEnPlaats("onderwijsboulevard 8", "den bosch 8") << std::endl; + std::cout << array.verwijderAllenMetAdresEnPlaats("onderwijsboulevard 9", "den bosch 8") << std::endl; + + return 0; +} diff --git a/algo1w1/makefile b/algo1w1/makefile new file mode 120000 index 0000000..a4e84c6 --- /dev/null +++ b/algo1w1/makefile @@ -0,0 +1 @@ +../week.mk \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..acb6c56 --- /dev/null +++ b/makefile @@ -0,0 +1,11 @@ +MK = make -j +WEKEN := $(wildcard oop*w*) +TARGETS := all clean compile_commands zip + +_: all + +.PHONY: $(TARGETS) $(WEKEN) +$(TARGETS): $(WEKEN) +$(WEKEN): + $(MK) -C $@ $(MAKECMDGOALS) + diff --git a/week.mk b/week.mk new file mode 100644 index 0000000..7a58f4d --- /dev/null +++ b/week.mk @@ -0,0 +1,30 @@ +CC = g++ +LD = g++ +RM = rm -f +TARGET = main +OUTPUT_ZIP = Huiswerk_2180996.zip + +LFLAGS += -lstdc++ + +SRCS := $(wildcard *.cpp) +OBJS := $(patsubst %.cpp,%.o, $(SRCS)) + +.PHONY: clean compile_commands zip + +all: $(TARGET) + +%.o: %.cpp + $(CC) -c $(CFLAGS) $< -o $@ + +$(TARGET): $(OBJS) + $(LD) $^ $(LFLAGS) -o $@ + +clean: + $(RM) $(TARGET) $(OBJS) $(OUTPUT_ZIP) + +compile_commands: clean + compiledb make -Bn + +zip: all + zip -q $(OUTPUT_ZIP) makefile $(wildcard *.cpp) $(wildcard *.h) $(wildcard *.hpp) + -- cgit v1.2.3