This commit is contained in:
Borean 2025-01-12 15:22:13 +01:00
parent d04ebdcad6
commit cff475a854
5 changed files with 86 additions and 0 deletions

5
morse/Makefile Normal file
View file

@ -0,0 +1,5 @@
# Only define if needed:
# TARGET = main.elf
# SRCS = main.cc
include ../config.mk

12
morse/main.cc Normal file
View file

@ -0,0 +1,12 @@
#include "morse_code.h"
#include <iostream>
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";
//
}

26
morse/morse.def Normal file
View file

@ -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 --..

29
morse/morse_code.cc Normal file
View file

@ -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) {
// }

14
morse/morse_code.h Normal file
View file

@ -0,0 +1,14 @@
#pragma once
#include <string>
#include <map>
#include <fstream>
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<char, std::string> morse_map;
};