Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct bin{
- map<string, int>::iterator it;
- int modify_string;
- vector<string> uncrypt_vec;
- map<string, int> encryption;
- void set_up_code(){
- string alphabet[] = {"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"};
- int letter_num=1;
- for(int i = 0; i < 27; ++i){
- string letter = alphabet[i]; //Assign each letter to a number
- encryption.insert(pair<string,int>(letter,letter_num));
- letter_num++;
- }
- encryption.insert(pair<string,int>(" ", -1));
- }
- int encrypt_vector(vector<string>& crypt){
- stringstream ssv;
- for(unsigned int i = 0; i < crypt.size(); ++i){
- string word = crypt[i]; //Grab each string
- int mod_word[word.size()];
- for(unsigned int x = 0; x < word.size(); ++x){
- stringstream ssl;
- string letter;
- char let = word[x]; //Convert each letter of the string into an integer
- ssl << let;
- ssl >> letter;
- int num = encryption[letter];
- mod_word[x] = num;
- }
- mod_word[word.size()] = 0; //Include a zero for each word
- copy(mod_word, mod_word+word.size()+1, ostream_iterator<int>(ssv));
- }
- ssv >> modify_string; //Put into int modify_string and return
- return modify_string;
- }
- vector<string> decrypt_vector(int& uncrypt){
- string num;
- stringstream ssn;
- ssn << uncrypt; //Store the numbers in a string
- ssn >> num;
- string mod_word;
- string word;
- for(unsigned int i=0;i<num.size();++i){
- int number;
- char check_num = num[i];
- stringstream ssctn;
- ssctn << check_num; //Kinda stupid way of doing it
- ssctn >> number; //Pulls int from num[i], then checks it against 0
- if(number==0){ //If break point
- uncrypt_vec.push_back(mod_word); //Push back the word
- mod_word.clear();
- }
- int letter;
- stringstream sscn;
- char let = num[i];
- sscn << let; //Pull each letter from uncrypt, change back to letter based on encryption
- sscn >> letter;
- for(it = encryption.begin(); it != encryption.end(); ++it){
- if(it->second == letter){
- //cout << it->first << endl;
- mod_word.append(it->first);
- break;
- }
- }
- }
- cout << "Done" << endl;
- return uncrypt_vec;
- }
- }binary;
- struct player{
- vector<string> inventory;
- int inventory_en; //en for encrypted
- void save(){
- inventory_en = binary.encrypt_vector(inventory);
- cout << inventory_en;
- }
- void load(){
- vector<string> inventory; //Must recreate, type is lost otherwise, crashes
- inventory = binary.decrypt_vector(inventory_en);
- vector<string>::iterator i;
- for(i=inventory.begin();i!=inventory.end();++i){
- cout << *i << endl;
- }
- }
- }character;
- int main(){
- character.inventory.push_back("abc");
- character.inventory.push_back("edf");
- binary.set_up_code();
- character.save();
- character.load();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement