diff --git a/morse/Makefile b/morse/Makefile new file mode 100644 index 0000000..60bee61 --- /dev/null +++ b/morse/Makefile @@ -0,0 +1,5 @@ +# Only define if needed: +# TARGET = main.elf +# SRCS = main.cc + +include ../config.mk diff --git a/morse/main.cc b/morse/main.cc new file mode 100644 index 0000000..db97661 --- /dev/null +++ b/morse/main.cc @@ -0,0 +1,12 @@ +#include "morse_code.h" +#include + +int main() { + Morse_code mc{"morse.def"}; + std::cout << mc.encode("Hello Morse") << "\n"; // .... . .-.. .-.. --- -- --- .-. ... . + +// std::cout << mc.decode("... --- ...") << "\n"; +// std::cout << mc.decode(".... ----") << "\n"; // ---- is not a valid code +// std::cout << mc.decode(mc.encode("loopback test")) << "\n"; +// +} diff --git a/morse/morse.def b/morse/morse.def new file mode 100644 index 0000000..ed3cac4 --- /dev/null +++ b/morse/morse.def @@ -0,0 +1,26 @@ +A .- +B -... +C -.-. +D -.. +E . +F ..-. +G --. +H .... +I .. +J .--- +K -.- +L .-.. +M -- +N -. +O --- +P .--. +Q --.- +R .-. +S ... +T - +U ..- +V ...- +W .-- +X -..- +Y -.-- +Z --.. \ No newline at end of file diff --git a/morse/morse_code.cc b/morse/morse_code.cc new file mode 100644 index 0000000..5c91d32 --- /dev/null +++ b/morse/morse_code.cc @@ -0,0 +1,29 @@ +#include "morse_code.h" + +Morse_code::Morse_code(std::string filename) { + std::ifstream in(filename); + + char c; + while (in >> c) { + std::string s; + in >> s; + morse_map.insert(std::make_pair(c, s)); // gay ass + } +} + +std::string Morse_code::encode(const std::string &str) { + + std::string res; + + for (auto &c : str) { + auto it = morse_map.find(c); + if (it != morse_map.end()) { + res += it->second + " "; + } + } + return res; +} + +// std::string Morse_code::decode(const std::string &str) { + +// } \ No newline at end of file diff --git a/morse/morse_code.h b/morse/morse_code.h new file mode 100644 index 0000000..0ea2e56 --- /dev/null +++ b/morse/morse_code.h @@ -0,0 +1,14 @@ +#pragma once +#include +#include +#include + +class Morse_code +{ +public: + Morse_code(std::string filename); + std::string encode(const std::string &str); + std::string decode(const std::string &str); +private: + std::map morse_map; +}; \ No newline at end of file