diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-03-05 20:43:24 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-03-05 20:43:24 +0100 |
commit | 01bd4791cbeabcf85ea72f91c1edd0af8ac17b1f (patch) | |
tree | 5c24611842cfefd1866a1e915c5d55279aae0234 | |
parent | bdf6588d9154889d897d6b102fc32f49bf92fa7b (diff) |
-rw-r--r-- | algo1w4d2/BracketCheck.cpp | 68 | ||||
-rw-r--r-- | algo1w4d2/BracketCheck.h | 20 | ||||
-rw-r--r-- | algo1w4d2/Kassa.cpp | 23 | ||||
-rw-r--r-- | algo1w4d2/Kassa.h | 22 | ||||
-rw-r--r-- | algo1w4d2/Klant.cpp | 4 | ||||
-rw-r--r-- | algo1w4d2/Klant.h | 8 | ||||
-rw-r--r-- | algo1w4d2/Queue.cpp | 27 | ||||
-rw-r--r-- | algo1w4d2/Queue.h | 21 | ||||
l--------- | algo1w4d2/Stack.cpp | 1 | ||||
l--------- | algo1w4d2/Stack.h | 1 | ||||
-rw-r--r-- | algo1w4d2/WinkelSim.cpp | 27 | ||||
-rw-r--r-- | algo1w4d2/WinkelSim.h | 24 | ||||
-rw-r--r-- | algo1w4d2/consts.h | 7 | ||||
-rw-r--r-- | algo1w4d2/input.txt | 7 | ||||
-rw-r--r-- | algo1w4d2/main.cpp | 38 | ||||
l--------- | algo1w4d2/makefile | 1 | ||||
-rw-r--r-- | algo1w4d2/readme.md | 55 | ||||
-rw-r--r-- | week.mk | 2 |
18 files changed, 355 insertions, 1 deletions
diff --git a/algo1w4d2/BracketCheck.cpp b/algo1w4d2/BracketCheck.cpp new file mode 100644 index 0000000..2584b75 --- /dev/null +++ b/algo1w4d2/BracketCheck.cpp @@ -0,0 +1,68 @@ +#include "BracketCheck.h" +#include "Stack.h" + +BracketCheck::BracketCheck() { _stack = new Stack(); } +BracketCheck::~BracketCheck() { delete _stack; } + +BracketCheck::BracketCheck(const std::string& s) : BracketCheck() { + for (char x : s) parse(std::string(1, x)); +} + +void BracketCheck::parse(const std::string& input) { + if (!_valid) return; + // match brackets + switch(input[0]) { + case '{': case '(': case '[': { + _stack->push(input); + break; + } + case '}': case ')': case ']': { + char open_bracket = _stack->pop()[0]; + if (open_bracket == '{' && input[0] == '}') { + // _stack->pop(); // } is used to close blocks + break; + } + if (open_bracket == '(' && input[0] == ')') break; + if (open_bracket == '[' && input[0] == ']') break; + _valid = false; + break; + } + } + + // check block ordering + switch(input[0]) { + case 'I': { + break; + } + case 'E': { + if (_stack->size() == 0) { _valid = false; break; } + if (_stack->peek()[0] != 'I') _valid = false; + break; + } + case 'D': { + break; + } + case 'W': { + if (_stack->size() > 0 && _stack->peek()[0] == 'D') { _stack->pop(); } + break; + } + } + + // add blocks to stack + switch(input[0]) { + case 'I': case 'E': case 'D': case 'W': { + _stack->push(input); + } + } +} + +bool BracketCheck::input_valid() { + while (_stack->size() > 0) { + char remainder = _stack->pop()[0]; + switch(remainder) { + case '{': case '(': case '[': case 'D': + return false; + } + } + return _valid; +} diff --git a/algo1w4d2/BracketCheck.h b/algo1w4d2/BracketCheck.h new file mode 100644 index 0000000..2f379a7 --- /dev/null +++ b/algo1w4d2/BracketCheck.h @@ -0,0 +1,20 @@ +#pragma once + +#include <string> + +class Stack; + +class BracketCheck { +public: + void parse(const std::string&); + bool input_valid(); + +public: + BracketCheck(); + BracketCheck(const std::string&); + virtual ~BracketCheck(); + +private: + Stack* _stack; + bool _valid = true; +}; diff --git a/algo1w4d2/Kassa.cpp b/algo1w4d2/Kassa.cpp new file mode 100644 index 0000000..e1ee46d --- /dev/null +++ b/algo1w4d2/Kassa.cpp @@ -0,0 +1,23 @@ +#include "Kassa.h" + +#include "consts.h" + +Kassa::Kassa() { _queue = new Queue(); } +Kassa::~Kassa() { delete _queue; } + +std::ostream& operator << (std::ostream& output, const Kassa& k) { + for (unsigned i = 0; i < k._queue->size(); i++) + output << "* "; + return output; +} + +void Kassa::add_customer(const Klant& k) { + _queue->insert(k); +} + +void Kassa::step() { + for (unsigned i = 0; i < MAX_TRANSACTION_PER_CHECKOUT; i++) { + if (_queue->size() == 0) break; + delete _queue->remove().release(); + } +} diff --git a/algo1w4d2/Kassa.h b/algo1w4d2/Kassa.h new file mode 100644 index 0000000..511015b --- /dev/null +++ b/algo1w4d2/Kassa.h @@ -0,0 +1,22 @@ +#pragma once + +#include <iostream> + +#include "Klant.h" +#include "Queue.h" + +class Kassa { +public: + void step(); /** @brief handle MAX_TRANSACTION_PER_CHECKOUT amount of customers */ + void add_customer(const Klant&); /** @brief add customer to checkout queue */ + +public: + Kassa(); + virtual ~Kassa(); + +public: + friend std::ostream& operator << (std::ostream& output, const Kassa& kassa); + +private: + Queue* _queue; +}; diff --git a/algo1w4d2/Klant.cpp b/algo1w4d2/Klant.cpp new file mode 100644 index 0000000..9b59724 --- /dev/null +++ b/algo1w4d2/Klant.cpp @@ -0,0 +1,4 @@ +#include "Klant.h" + +Klant::Klant() {} +Klant::~Klant() {} diff --git a/algo1w4d2/Klant.h b/algo1w4d2/Klant.h new file mode 100644 index 0000000..3626cf3 --- /dev/null +++ b/algo1w4d2/Klant.h @@ -0,0 +1,8 @@ +#pragma once + +/** @brief Dummy class */ +class Klant { +public: + Klant(); + virtual ~Klant(); +}; diff --git a/algo1w4d2/Queue.cpp b/algo1w4d2/Queue.cpp new file mode 100644 index 0000000..6fd2959 --- /dev/null +++ b/algo1w4d2/Queue.cpp @@ -0,0 +1,27 @@ +#include "Queue.h" +#include "Klant.h" + +Queue::Queue() {} +Queue::~Queue() { + while (size() != 0) + delete remove().release(); +} + +void Queue::insert(const Klant& k) { + _queue.push_back(std::unique_ptr<Klant>(new Klant(k))); +} + +std::unique_ptr<Klant> Queue::remove() { + std::unique_ptr<Klant> s = std::move(_queue.at(0)); + _queue.erase(_queue.begin()); + return s; +} + +Klant* Queue::peek() { + return _queue.at(0).get(); +} + +unsigned Queue::size() { + return _queue.size(); +} + diff --git a/algo1w4d2/Queue.h b/algo1w4d2/Queue.h new file mode 100644 index 0000000..cf52b12 --- /dev/null +++ b/algo1w4d2/Queue.h @@ -0,0 +1,21 @@ +#pragma once + +class Klant; + +#include <vector> +#include <memory> + +class Queue { +public: + void insert(const Klant&); /** @brief append to queue */ + std::unique_ptr<Klant> remove(); /** @brief remove and return last queue element */ + Klant* peek(); /** @brief return but keep last queue element */ + unsigned size(); /** @brief get size of queue */ + +public: + Queue(); + virtual ~Queue(); + +private: + std::vector<std::unique_ptr<Klant>> _queue; +}; diff --git a/algo1w4d2/Stack.cpp b/algo1w4d2/Stack.cpp new file mode 120000 index 0000000..34bfb6f --- /dev/null +++ b/algo1w4d2/Stack.cpp @@ -0,0 +1 @@ +../algo1w4d1/Stack.cpp
\ No newline at end of file diff --git a/algo1w4d2/Stack.h b/algo1w4d2/Stack.h new file mode 120000 index 0000000..9b6502a --- /dev/null +++ b/algo1w4d2/Stack.h @@ -0,0 +1 @@ +../algo1w4d1/Stack.h
\ No newline at end of file diff --git a/algo1w4d2/WinkelSim.cpp b/algo1w4d2/WinkelSim.cpp new file mode 100644 index 0000000..1e7ad88 --- /dev/null +++ b/algo1w4d2/WinkelSim.cpp @@ -0,0 +1,27 @@ +#include "WinkelSim.h" +#include "Kassa.h" + +WinkelSim::WinkelSim() { + srand(time(NULL)); + for (size_t i = 0; i < CHECKOUT_COUNT; i++) + _checkouts[i] = new Kassa(); +} + +WinkelSim::~WinkelSim() { + for (size_t i = 0; i < CHECKOUT_COUNT; i++) + delete _checkouts[i]; +} + +std::ostream& operator << (std::ostream& output, const WinkelSim& w) { + for (size_t i = 0; i < CHECKOUT_COUNT; i++) + output << "\tkassa " << i+1 << ": " << *w._checkouts[i] << std::endl; + return output; +} + +void WinkelSim::step() { + unsigned new_customer_count = std::rand() % MAX_NEW_CUSTOMERS_PER_TIMESTEP; + for (unsigned i = 0; i < new_customer_count; i++) + _checkouts[std::rand() % CHECKOUT_COUNT]->add_customer({}); + for (size_t i = 0; i < CHECKOUT_COUNT; i++) + _checkouts[i]->step(); +} diff --git a/algo1w4d2/WinkelSim.h b/algo1w4d2/WinkelSim.h new file mode 100644 index 0000000..d2152a8 --- /dev/null +++ b/algo1w4d2/WinkelSim.h @@ -0,0 +1,24 @@ +#pragma once + +#include <iostream> +#include <array> + +#include "Kassa.h" + +#include "consts.h" + +class WinkelSim { +public: + void step(); /** @brief distribute MAX_NEW_CUSTOMERS_PER_TIMESTEP and call Kassa.step() */ + +public: + WinkelSim(); + virtual ~WinkelSim(); + +public: + friend std::ostream& operator << (std::ostream& output, const WinkelSim& sim); + +private: + std::array<Kassa*, CHECKOUT_COUNT> _checkouts = { nullptr }; +}; + diff --git a/algo1w4d2/consts.h b/algo1w4d2/consts.h new file mode 100644 index 0000000..656d9ad --- /dev/null +++ b/algo1w4d2/consts.h @@ -0,0 +1,7 @@ +#pragma once + +#define CHECKOUT_COUNT 3 +#define MAX_NEW_CUSTOMERS_PER_TIMESTEP 21 +#define MAX_TRANSACTION_PER_CHECKOUT 3 +#define MAX_TIME_STEPS 20 + diff --git a/algo1w4d2/input.txt b/algo1w4d2/input.txt new file mode 100644 index 0000000..6799292 --- /dev/null +++ b/algo1w4d2/input.txt @@ -0,0 +1,7 @@ +I(){}E{}[] +D{I(){}}W([]) +D{I(){}I(){}I(){}}W([]) +E{I(){}}W([]) + +I{[}] +D{}I(){} diff --git a/algo1w4d2/main.cpp b/algo1w4d2/main.cpp new file mode 100644 index 0000000..1892fbc --- /dev/null +++ b/algo1w4d2/main.cpp @@ -0,0 +1,38 @@ +#include <iostream> + +#define BRACKET_CHECK + +#ifdef WINKEL_SIM +#include "WinkelSim.h" + +int main() { + WinkelSim sim; + + for (unsigned t = 0; t < MAX_TIME_STEPS; t++) { + sim.step(); + std::cout << "tijd: " << t << std::endl << sim << std::endl; + } + + return 0; +} +#endif + +#ifdef BRACKET_CHECK +#include "BracketCheck.h" + +int main() { + unsigned i = 0; + std::string input; + + while(getline(std::cin, input)) { + i++; + if (input.size() == 0) { + std::cout << std::endl; + continue; + } + BracketCheck parser(input); + std::cout << "line " << i << " is " << (parser.input_valid() ? "valid" : "invalid") << std::endl; + } + return 0; +} +#endif diff --git a/algo1w4d2/makefile b/algo1w4d2/makefile new file mode 120000 index 0000000..a4e84c6 --- /dev/null +++ b/algo1w4d2/makefile @@ -0,0 +1 @@ +../week.mk
\ No newline at end of file diff --git a/algo1w4d2/readme.md b/algo1w4d2/readme.md new file mode 100644 index 0000000..04d37ea --- /dev/null +++ b/algo1w4d2/readme.md @@ -0,0 +1,55 @@ +# week 4 deel 2 + +## opdracht 1 + +output: +```bash +$ ./main +tijd: 0 + kassa 1: + kassa 2: + kassa 3: + +tijd: 1 + kassa 1: + kassa 2: + kassa 3: * * + +tijd: 2 + kassa 1: * * + kassa 2: * * + kassa 3: * * * * * * + +tijd: 3 + kassa 1: * * * * * * * * * * + kassa 2: * + kassa 3: * * * * * * * + +tijd: 4 + kassa 1: * * * * * * * * * * * * * * * + kassa 2: * * + kassa 3: * * * * * * * * * * * * +``` +etc. + +## opdracht 2 + +```bash +$ cat input.txt +I(){}E{}[] +D{I(){}}W([]) +D{I(){}I(){}I(){}}W([]) +E{I(){}}W([]) + +I{[}] +D{}I(){} +$ ./main < input.txt +line 1 is valid +line 2 is invalid +line 3 is invalid +line 4 is invalid + +line 6 is invalid +line 7 is invalid +$ +``` @@ -33,5 +33,5 @@ compile_commands: clean compiledb make -Bn zip: all - zip -q $(OUTPUT_ZIP) makefile $(wildcard *.cpp) $(wildcard *.h) $(wildcard *.hpp) $(wildcard *.c) $(wildcard *.svg) $(wildcard *.xml) + zip -q $(OUTPUT_ZIP) $(shell git ls-files) |