#Text - Morse Code translation machine (console)

61 messages · Page 1 of 1 (latest)

fervent flax
#
    char alphabet;
    const char* morse
}
const MorseUnit morseList[] = {
    { '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', "--.." },
    { '1', ".----" },{ '2', "..---" },{ '3', "...--" },{ '4', "....-" },
    { '5', "....." },{ '6', "-...." },{ '7', "--..." },{ '8', "---.." },
    { '9', "----." },{ '0', "-----" },{ ' ', "/" }
};
const int Morsesize = sizeof(morseList) / sizeof(MorseUnit); // the same as const int MORSE_SIZE = sizeof(morseList) / sizeof(morseList[0]);
void MTT(std::string &text, std::string &morse) {
    text = '';
    size_t start = 0; // size_t helps store large amount of DATA, instead of int we use size_t for storing non-negative values
    size_t end = 0; 
    getline(cin, morse);
    if ((!text.empty() || text.size() != 0) && (morse.size() != 0)) {
        for (int i = 0; i < Morsesize; i++) {
            for (int j = 0; j < Morsesize; j++)
            std::cout << morse[i] << std::endl;
        }
    }
}

how can i let compiler know which morse character is what the english text in the given struct array, and make compiler translates it

tranquil novaBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

novel fulcrum
fervent flax
#

what are your thoughts

#

btw i cannot think any easier and simpler way

novel fulcrum
#
for (int i = 0; i < Morsesize; ++i) {
        if (morseList[i].alphabet == c) {
            return morseList[i].morse;
        }
    }

you can do something like that and call this in another function iterating all the characters of the text u want to translate

novel fulcrum
#

but I am just a beginner so take my advices with a grain of salt

fervent flax
#

it's ok, maybe you will have better ideas than the expert , who knows Clueless

tall kite
#

you can also make use of the fact that the ascii values aren't arbitrary: B has a value one higher than A, and so on until Z.
Which means you can get the index for an upper-case letter c as c - 'A'

#

it's the same for the digits

#

(technically the C++ standard doesn't require that characters are encoded as ascii, but that's probably not an issue for you)

#

so you could write something like ```c++
const char* to_morse(char c) {
if (c >= 'A' && c <= 'Z') { return morseList[c - 'A'].morse; }
// this requires you to move the entry for '0` in front of '1' in the list
else if (c >= '0' && c <= '9') { return morseList[26 + c - '0'].morse; }
else if (c == ' ') { return morseList[Morsesize - 1].morse; }
else { /* print an error message or something */ }
}

fervent flax
#

i mean we can use toupper()?

#

it automatically turns non capital into capital

tall kite
#

yeah you can use that

tall kite
fervent flax
#

but thank u for that idea man, when i finished this i might try without toupper()

tall kite
#

I mean, you can use this and toupper

novel fulcrum
fervent flax
#

do you*

tall kite
novel fulcrum
tall kite
#

also faster than a std::unordered_map or similar

#

this is essentially a map, if you think about it

novel fulcrum
#

why though

#

like asm level

tall kite
#

do you know how a hashmap is implemented?

novel fulcrum
#

i have no idea

fervent flax
#

i havent use hashmap for complex projects yet, just one time solving a simple problem on leetcode

#

so i might don't really know much abt it

tall kite
# novel fulcrum i have no idea

there are a couple of ways to implement a hashmap, but the reason an array is faster boils down to the fact that a hashmap allows you to look up an arbitrary element
But for an array, the index is always between 0 and the size

fervent flax
tall kite
#

so one of the fast ways to implement a hashmap is to first hash the key, then translate this hash into an index, then use that to index an array, then do additional stuff to get the actual element
So clearly this isn't faster than just using an array directly

fervent flax
#

i only know it can stores unlimited variable

tall kite
#

in fact, one of the ways your code could be improved would be to not use a c style array

#

instead, you could use a std::array

fervent flax
#

sometimes i use pairs or tuples, to form datas in group

novel fulcrum
tall kite
#

which makes your code more readable

tall kite
#

btw what I've written is just one way to do it

novel fulcrum
#

nooo data science

tall kite
#

another way to improve this would be to use std::string_view instead of const char*

novel fulcrum
#

its not to iterate its calculating the indexes im so dumb

tall kite
#

there are a couple more things that could be improved for this code

#

it's always a good idea to have the input and output (i.e., std::getline and std::cout) be separate from the actual logic

#

that makes your code more reusable and testable

#

so the general approach would be:

  • Get the input text
  • Call a function that takes this text (probably as a std::string_view) and returns the equivalent morse text
  • Output that result
#

and to implement this function you would loop over the characters in the string and call a function that converts each char to its morse code representation

#

which is the function I've implemented above

#

but yeah, using a std::unordered_map instead of an array would also be fine, it'd be a bit slower but if this is for competetive programming it would still be fasat enough

tranquil novaBOT
#

@fervent flax Has your question been resolved? If so, type !solved :)

hard dawn
#

!solved

tranquil novaBOT