Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. #include <cctype>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <map>
  5. #include <stdexcept>
  6. #include <string>
  7. #include <sstream>
  8. #include <utility>
  9. #include <vector>
  10.  
  11. //Builds alphabet_to_morse (II parameter) & morse_to_alphabet (III parameter) from file (I parameter)
  12. //Throws std::runtime_error if file is not found
  13. void build_tables(std::string, std::map<std::string, std::string> &, std::map<std::string, std::string> &);
  14.  
  15. //Used when converting from alphabet to morse for proper formatting for convert function
  16. //Chages "abc Def" to "A B C / D E F"
  17. std::string format(const std::string &);
  18.  
  19. //Used for converting from one system to another
  20. //Keys from original (I parameter) are replaced with values from map (II parameter)
  21. std::string convert(const std::string &, const std::map<std::string, std::string> &);
  22.  
  23. //Used when converting from morse to alphabet to get readable output
  24. //Changes "A B C / D E F" to "ABC DEF"
  25. std::string clear_formatting(const std::string &);
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29.     //Read mode & name of the conversion chart from arguments
  30.     std::string table = "morse.txt";
  31.     int mode = 0;
  32.     switch (argc)
  33.     {
  34.     case 3:
  35.         table = argv[2];
  36.     case 2:
  37.         mode = atoi(argv[1]);
  38.     default:
  39.         break;
  40.     }
  41.  
  42.     //Create conversion maps
  43.     std::map<std::string, std::string> alphabet_to_morse;
  44.     std::map<std::string, std::string> morse_to_alphabet;
  45.     try
  46.     {
  47.         build_tables(table, alphabet_to_morse, morse_to_alphabet);
  48.         if (mode != 0 && mode != 1)
  49.             throw std::runtime_error("Invalid mode");
  50.     }
  51.     catch (std::runtime_error &e)
  52.     {
  53.         std::cerr << e.what() << std::endl;
  54.         exit(-1);
  55.     }
  56.  
  57.     //Read messages
  58.     std::vector<std::string> original_message;
  59.     std::string line;
  60.     while (getline(std::cin, line))
  61.     {
  62.         original_message.push_back(line);
  63.     }
  64.     if (original_message.empty() == true)
  65.     {
  66.         std::cerr << "No data!" << std::endl;
  67.         exit(-1);
  68.     }
  69.  
  70.     //Convert messages
  71.     std::vector<std::string> converted_message;
  72.     for (auto i : original_message)
  73.     {
  74.         std::string temp;
  75.         //Converting from alphabet to morse
  76.         if (mode == 1)
  77.             temp = convert(format(i), alphabet_to_morse);
  78.         //Converting from morse to alphabet
  79.         else
  80.             temp = clear_formatting(convert(i, morse_to_alphabet));
  81.         converted_message.push_back(temp);
  82.     }
  83.     for (auto i : converted_message)
  84.         std::cout << i << std::endl;
  85.     system("PAUSE");
  86.     return 0;
  87. }
  88.  
  89. void build_tables(std::string file, std::map<std::string, std::string> &alphabet_to_morse, std::map<std::string, std::string> &morse_to_alphabet)
  90. {
  91.     std::ifstream table(file);
  92.     if (table.is_open() == false)
  93.         throw std::runtime_error("Alphabet - Morse conversion chart not found");
  94.  
  95.     std::string line;
  96.     while (getline(table, line))
  97.     {
  98.         //Separate letter and morse code
  99.         std::istringstream alphabet_pair(line);
  100.         std::string alphabet_letter;
  101.         std::string morse_letter;
  102.         alphabet_pair >> alphabet_letter >> morse_letter;
  103.  
  104.         //Add to maps
  105.         alphabet_to_morse.insert(make_pair(alphabet_letter, morse_letter));
  106.         morse_to_alphabet.insert(make_pair(morse_letter, alphabet_letter));
  107.     }
  108. }
  109.  
  110. std::string format(const std::string &input)
  111. {
  112.     std::string output;
  113.     for (auto i : input)
  114.     {
  115.         if (isalpha(i))
  116.         {
  117.             output += toupper(i);
  118.             output += " ";
  119.         }
  120.         else if (isspace(i))
  121.             output += " / ";
  122.         else
  123.             output += i;
  124.     }
  125.     return output;
  126. }
  127.        
  128. std::string convert(const std::string &input, const std::map<std::string, std::string> &table)
  129. {
  130.     std::string output;
  131.     std::istringstream line(input);
  132.     std::string temp;
  133.     while (line >> temp)
  134.     {
  135.         //Check if the key exists in the table
  136.         if (table.find(temp) != table.end())
  137.             output += table.at(temp);
  138.         else
  139.             output += temp;
  140.         output += " ";
  141.     }
  142.     return output;
  143. }
  144.  
  145. std::string clear_formatting(const std::string &input)
  146. {
  147.     std::string output;
  148.     for (auto i : input)
  149.     {
  150.         if (isspace(i))
  151.             //Ignore spaces
  152.             ;
  153.         else if (isalpha(i))
  154.             output += i;
  155.         else if (i == '/')
  156.             //Replace '/' with ' '
  157.             output += " ";
  158.         else
  159.             output += i;
  160.     }
  161.     return output;
  162. }
  163.  
  164. //Morse.txt
  165. 0 -----
  166. 1 .----
  167. 2 ..---
  168. 3 ...--
  169. 4 ....-
  170. 5 .....
  171. 6 -....
  172. 7 --...
  173. 8 ---..
  174. 9 ----.
  175. A .-
  176. B -...
  177. C -.-.
  178. D -..
  179. E .
  180. F ..-.
  181. G --.
  182. H ....
  183. I ..
  184. J .---
  185. K -.-
  186. L .-..
  187. M --
  188. N -.
  189. O ---
  190. P .--.
  191. Q --.-
  192. R .-.
  193. S ...
  194. T -
  195. U ..-
  196. V ...-
  197. W .--
  198. X -..-
  199. Y -.--
  200. Z --..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement