Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- //file delimiter of name and morse code
- const string file_delimiter = " : ";
- //structures
- typedef struct
- {
- string location;
- string morse;
- }MorseInfo;
- typedef struct morse_node
- {
- MorseInfo *info;
- struct morse_node *next;
- }MorseNode;
- //functions
- //Function to remove all spaces of a string str
- string RemoveSpaces(string str)
- {
- int pos =0;
- string val = str;
- while((pos = val.find(" ", pos)) != string::npos)
- {
- val.replace(pos, 1, "");
- }
- return val;
- }
- //Function to remove all slashes "/" from a string str
- string RemoveSlashes(string str)
- {
- int pos =0;
- string val = str;
- while((pos = val.find("/", pos)) != string::npos)
- {
- val.replace(pos, 1, "");
- }
- return val;
- }
- void main()
- {
- //array of a list of morse codes (a list per stage, 8 stages)
- MorseNode **ListNodes = new MorseNode*[8];
- //initialization
- for(int i=0; i < 8; i++)
- {
- ListNodes[i] = nullptr;
- }
- //open and loading file
- ifstream file;
- file.open("morse.txt");
- string line;
- int pos;
- int stagenum;
- if(file.is_open())
- {
- while(getline(file, line))
- {
- if(line.find("STAGE") != string::npos)
- {
- //-1 because first stage is index 0, 2 is index 1, etc.
- stagenum = stoi(line.substr(6, 1)) - 1;
- }
- else if(line != "")
- {
- pos = line.find(file_delimiter);
- string location = line.substr(0, pos);
- string val = line.substr(pos + file_delimiter.size(), line.size() - 1);
- val = RemoveSpaces(val);
- val = RemoveSlashes(val);
- MorseInfo *info = new MorseInfo;
- info->location = location;
- info->morse = val;
- //always insert in the beginning to make it easier to do
- MorseNode *n = new MorseNode;
- n->info = info;
- n->next = ListNodes[stagenum];
- ListNodes[stagenum] = n;
- }
- }
- file.close();
- }
- //credits
- cout << "BFEE - Morse to Location\n";
- cout << "Credits:\n";
- cout << "Oetjel - Idea and Morse Code list\n";
- cout << "Kimossab - Programmer\n";
- cout << "Rest of Room40 and BFEE discord server\n\n\n";
- int select=0;
- string newmorse;
- 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";
- cin >> select;
- while(select > 0 && select < 9)
- {
- cout << "\nPlease insert the morse code:\n";
- cin.ignore();
- getline(cin, newmorse, '\n');
- newmorse = RemoveSpaces(newmorse);
- newmorse = RemoveSlashes(newmorse);
- MorseNode *n = ListNodes[select - 1];
- cout << "\n\n";
- while(n)
- {
- if(n->info->morse.find(newmorse) != string::npos)
- cout << n->info->location << " : " << n->info->morse << "\n";
- n = n->next;
- }
- select = 0;
- cout << "Select a stage:\n(SELECT AN INVALID STAGE TO LEAVE)\n";
- cin >> select;
- cin.ignore();
- }
- //memory cleaning
- for(int i=0; i < 8; i++)
- {
- MorseNode *n = ListNodes[i];
- MorseNode *aux = nullptr;
- while(n)
- {
- delete n->info;
- if(aux)
- delete aux;
- aux = n;
- n = n->next;
- }
- }
- delete[] ListNodes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement