Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cctype>
- #include <iostream>
- #include <fstream>
- #include <map>
- #include <stdexcept>
- #include <string>
- #include <sstream>
- #include <utility>
- #include <vector>
- //Builds alphabet_to_morse (II parameter) & morse_to_alphabet (III parameter) from file (I parameter)
- //Throws std::runtime_error if file is not found
- void build_tables(std::string, std::map<std::string, std::string> &, std::map<std::string, std::string> &);
- //Used when converting from alphabet to morse for proper formatting for convert function
- //Chages "abc Def" to "A B C / D E F"
- std::string format(const std::string &);
- //Used for converting from one system to another
- //Keys from original (I parameter) are replaced with values from map (II parameter)
- std::string convert(const std::string &, const std::map<std::string, std::string> &);
- //Used when converting from morse to alphabet to get readable output
- //Changes "A B C / D E F" to "ABC DEF"
- std::string clear_formatting(const std::string &);
- int main(int argc, char *argv[])
- {
- //Read mode & name of the conversion chart from arguments
- std::string table = "morse.txt";
- int mode = 0;
- switch (argc)
- {
- case 3:
- table = argv[2];
- case 2:
- mode = atoi(argv[1]);
- default:
- break;
- }
- //Create conversion maps
- std::map<std::string, std::string> alphabet_to_morse;
- std::map<std::string, std::string> morse_to_alphabet;
- try
- {
- build_tables(table, alphabet_to_morse, morse_to_alphabet);
- if (mode != 0 && mode != 1)
- throw std::runtime_error("Invalid mode");
- }
- catch (std::runtime_error &e)
- {
- std::cerr << e.what() << std::endl;
- exit(-1);
- }
- //Read messages
- std::vector<std::string> original_message;
- std::string line;
- while (getline(std::cin, line))
- {
- original_message.push_back(line);
- }
- if (original_message.empty() == true)
- {
- std::cerr << "No data!" << std::endl;
- exit(-1);
- }
- //Convert messages
- std::vector<std::string> converted_message;
- for (auto i : original_message)
- {
- std::string temp;
- //Converting from alphabet to morse
- if (mode == 1)
- temp = convert(format(i), alphabet_to_morse);
- //Converting from morse to alphabet
- else
- temp = clear_formatting(convert(i, morse_to_alphabet));
- converted_message.push_back(temp);
- }
- for (auto i : converted_message)
- std::cout << i << std::endl;
- system("PAUSE");
- return 0;
- }
- void build_tables(std::string file, std::map<std::string, std::string> &alphabet_to_morse, std::map<std::string, std::string> &morse_to_alphabet)
- {
- std::ifstream table(file);
- if (table.is_open() == false)
- throw std::runtime_error("Alphabet - Morse conversion chart not found");
- std::string line;
- while (getline(table, line))
- {
- //Separate letter and morse code
- std::istringstream alphabet_pair(line);
- std::string alphabet_letter;
- std::string morse_letter;
- alphabet_pair >> alphabet_letter >> morse_letter;
- //Add to maps
- alphabet_to_morse.insert(make_pair(alphabet_letter, morse_letter));
- morse_to_alphabet.insert(make_pair(morse_letter, alphabet_letter));
- }
- }
- std::string format(const std::string &input)
- {
- std::string output;
- for (auto i : input)
- {
- if (isalpha(i))
- {
- output += toupper(i);
- output += " ";
- }
- else if (isspace(i))
- output += " / ";
- else
- output += i;
- }
- return output;
- }
- std::string convert(const std::string &input, const std::map<std::string, std::string> &table)
- {
- std::string output;
- std::istringstream line(input);
- std::string temp;
- while (line >> temp)
- {
- //Check if the key exists in the table
- if (table.find(temp) != table.end())
- output += table.at(temp);
- else
- output += temp;
- output += " ";
- }
- return output;
- }
- std::string clear_formatting(const std::string &input)
- {
- std::string output;
- for (auto i : input)
- {
- if (isspace(i))
- //Ignore spaces
- ;
- else if (isalpha(i))
- output += i;
- else if (i == '/')
- //Replace '/' with ' '
- output += " ";
- else
- output += i;
- }
- return output;
- }
- //Morse.txt
- 0 -----
- 1 .----
- 2 ..---
- 3 ...--
- 4 ....-
- 5 .....
- 6 -....
- 7 --...
- 8 ---..
- 9 ----.
- 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 --..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement