Advertisement
Kimossab

BFEE - Morse Code to Location

Feb 25th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //file delimiter of name and morse code
  8. const string file_delimiter = "     :   ";
  9.  
  10. //structures
  11. typedef struct
  12. {
  13.     string location;
  14.     string morse;
  15. }MorseInfo;
  16.  
  17. typedef struct morse_node
  18. {
  19.     MorseInfo *info;
  20.     struct morse_node *next;
  21. }MorseNode;
  22.  
  23. //functions
  24. //Function to remove all spaces of a string str
  25. string RemoveSpaces(string str)
  26. {
  27.     int pos =0;
  28.     string val = str;
  29.     while((pos = val.find(" ", pos)) != string::npos)
  30.     {
  31.         val.replace(pos, 1, "");
  32.     }
  33.     return val;
  34. }
  35.  
  36. //Function to remove all slashes "/" from a string str
  37. string RemoveSlashes(string str)
  38. {
  39.     int pos =0;
  40.     string val = str;
  41.     while((pos = val.find("/", pos)) != string::npos)
  42.     {
  43.         val.replace(pos, 1, "");
  44.     }
  45.     return val;
  46. }
  47.  
  48. void main()
  49. {
  50.     //array of a list of morse codes (a list per stage, 8 stages)
  51.     MorseNode **ListNodes = new MorseNode*[8];
  52.     //initialization
  53.     for(int i=0; i < 8; i++)
  54.     {
  55.         ListNodes[i] = nullptr;
  56.     }
  57.  
  58.     //open and loading file
  59.     ifstream file;
  60.     file.open("morse.txt");
  61.     string line;
  62.     int pos;
  63.     int stagenum;
  64.     if(file.is_open())
  65.     {
  66.         while(getline(file, line))
  67.         {
  68.             if(line.find("STAGE") != string::npos)
  69.             {
  70.                 //-1 because first stage is index 0, 2 is index 1, etc.
  71.                 stagenum = stoi(line.substr(6, 1)) - 1;
  72.             }
  73.             else if(line != "")
  74.             {
  75.                 pos = line.find(file_delimiter);
  76.                 string location = line.substr(0, pos);
  77.                 string val = line.substr(pos + file_delimiter.size(), line.size() - 1);
  78.  
  79.                 val = RemoveSpaces(val);
  80.                 val = RemoveSlashes(val);
  81.  
  82.                 MorseInfo *info = new MorseInfo;
  83.                 info->location = location;
  84.                 info->morse = val;
  85.  
  86.                 //always insert in the beginning to make it easier to do
  87.                 MorseNode *n = new MorseNode;
  88.                 n->info = info;
  89.                 n->next = ListNodes[stagenum];
  90.                 ListNodes[stagenum] = n;
  91.             }
  92.         }
  93.  
  94.         file.close();
  95.     }
  96.  
  97.     //credits
  98.     cout << "BFEE - Morse to Location\n";
  99.     cout << "Credits:\n";
  100.     cout << "Oetjel - Idea and Morse Code list\n";
  101.     cout << "Kimossab - Programmer\n";
  102.     cout << "Rest of Room40 and BFEE discord server\n\n\n";
  103.  
  104.     int select=0;
  105.     string newmorse;
  106.     cout << "Select a stage:\n(SELECT AN INVALID STAGE TO LEAVE)\nWARNING: DO NOT CLOSE THE PROGRAM OR ALT+F4, PLEASE USE AN INVALID STAGE TO SECURELY CLOSE THE PROGRAM\n";
  107.     cin >> select;
  108.     while(select > 0 && select < 9)
  109.     {
  110.         cout << "\nPlease insert the morse code:\n";
  111.         cin.ignore();
  112.         getline(cin, newmorse, '\n');
  113.  
  114.         newmorse = RemoveSpaces(newmorse);
  115.         newmorse = RemoveSlashes(newmorse);
  116.  
  117.         MorseNode *n = ListNodes[select - 1];
  118.         cout << "\n\n";
  119.         while(n)
  120.         {
  121.             if(n->info->morse.find(newmorse) != string::npos)
  122.                 cout << n->info->location << " : " << n->info->morse << "\n";
  123.             n = n->next;
  124.         }
  125.  
  126.         select = 0;
  127.         cout << "Select a stage:\n(SELECT AN INVALID STAGE TO LEAVE)\n";
  128.         cin >> select;
  129.         cin.ignore();
  130.     }
  131.  
  132.  
  133.     //memory cleaning
  134.     for(int i=0; i < 8; i++)
  135.     {
  136.         MorseNode *n = ListNodes[i];
  137.         MorseNode *aux = nullptr;
  138.         while(n)
  139.         {
  140.             delete n->info;
  141.             if(aux)
  142.                 delete aux;
  143.             aux = n;
  144.             n = n->next;
  145.         }
  146.     }
  147.     delete[] ListNodes;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement