Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <windows.h>
- std::string engtomol (std::string, std::string[]);
- std::string moltoeng (std::string, std::string[]);
- int main ()
- {
- std::string morse[39] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
- ".---", "-.-", ".-..", "--.", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
- "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
- "-....", "--...", "---..", "----.", "-----", ".-.-.-", "--..--", "..--.."};
- std::string text, morsecode;
- char choice;
- char repeat='y';
- while (repeat=='y')
- {
- std::cout << " A = .- \t N = -. \t 1 = .---- \n";
- std::cout << " B = -... \t O = --- \t 2 = ..--- \n";
- std::cout << " C = -.-. \t P = .--. \t 3 = ...-- \n";
- std::cout << " D = -.. \t Q = --.- \t 4 = ....- \n";
- std::cout << " E = . \t\t R = .-. \tv 5 = ..... \n";
- std::cout << " F = ..-. \t S = ... \t 6 = -.... \n";
- std::cout << " G = --. \t T = - \t\t 7 = --... \n";
- std::cout << " H = .... \t U = ..- \t 8 = ---.. \n";
- std::cout << " I = .. \t V = ...- \t 9 = ----. \n";
- std::cout << " J = .--- \t W = .-- \t 0 = ----- \n";
- std::cout << " K = -.- \t X = -..- \t Stop = .-.-.- \n";
- std::cout << " L = .-.. \t Y = -.-- \t , = --..-- \n";
- std::cout << " M = -- \t Z = --.. \t ? = ..--.. \n";
- std::cout << "\n";
- std::cout << "Enter 1 to decode English to Morse\nEnter 2 to decode Morse code to English\n";
- std::cin >> choice;
- if (choice=='1')
- {
- std::cout << "Enter text to translate: ";
- std::cin.get();
- getline(std::cin,text);
- // cout << "TEXT: " << text << endl;
- std::cout << "MORSE CODE: " << engtomol(text, morse) << "\n";
- }
- else if (choice=='2')
- {
- std::cout << "Enter your morse code, space in between each letter.\n";
- std::cout << "If multiple words, add 3 spaces after each word: ";
- std::cin.get();
- getline(std::cin,morsecode);
- // cout << "MORSECODE: " << morsecode << endl;
- std::cout << "TEXT: " << moltoeng (morsecode, morse) << "\n";
- }
- std::cout << "Would you like to try again? Enter y to repeat. Enter any other key to exit. ";
- std::cin >> repeat;
- }
- return 0;
- }
- // this function converts from English to Morse
- std::string engtomol (std::string text, std::string morse[])
- {
- int textlength = text.length();
- std::string morsevalue;
- std::string spacesbtwletters= " ";
- std::string spacesbtwwords = " ";//2 spaces because i am going to add it with spacesbtwletters so that it will = 3
- for (int k=0; k<textlength; k++)
- {
- if (text[k]!= ' ') //if the word(s) did not encounter a space
- { text[k]=toupper(text[k]); //upper case letters and lower case letters are the same hence have the same appropriate morse code.
- morsevalue = spacesbtwletters+= morse[text[k]-'A']+" ";
- }
- if (text[k]==' ')
- {
- spacesbtwletters+=spacesbtwwords;//adds 3 spaces when there is a space between words
- }
- }
- return morsevalue;
- }
- std::string moltoeng(std::string morsecode, std::string morse[])
- {
- std::string english[39] = {"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", "Stop", ",", "?"};
- std::string output = ""; //output for function
- int index = 0;
- while(index < morsecode.size())
- {
- //find current morse code
- std::string letter = "";
- int x = 0;
- while(morsecode[index] == ' ')
- {
- x++;
- index++;
- if(x==3){
- output+= ' ';
- break;
- }
- }
- while(morsecode[index] != ' ')
- {
- letter += morsecode[index++];
- } //find this code in the morse alphabet
- int position = 0;
- for(position=0; position < 39; position++)
- {
- if(morse[position]==letter)
- {
- output+=english[position];
- ++index; //to get rid of the space character.
- break;
- }
- }
- }
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement