Advertisement
asqapro

Serialized

Aug 1st, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. struct bin{
  2.     map<string, int>::iterator it;
  3.     int modify_string;
  4.     vector<string> uncrypt_vec;
  5.     map<string, int> encryption;
  6.     void set_up_code(){
  7.         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"};
  8.         int letter_num=1;
  9.         for(int i = 0; i < 27; ++i){
  10.             string letter = alphabet[i]; //Assign each letter to a number
  11.             encryption.insert(pair<string,int>(letter,letter_num));
  12.             letter_num++;
  13.         }
  14.         encryption.insert(pair<string,int>(" ", -1));
  15.     }
  16.     int encrypt_vector(vector<string>& crypt){
  17.         stringstream ssv;
  18.         for(unsigned int i = 0; i < crypt.size(); ++i){
  19.             string word = crypt[i]; //Grab each string
  20.             int mod_word[word.size()];
  21.             for(unsigned int x = 0; x < word.size(); ++x){
  22.                 stringstream ssl;
  23.                 string letter;
  24.                 char let = word[x]; //Convert each letter of the string into an integer
  25.                 ssl << let;
  26.                 ssl >> letter;
  27.                 int num = encryption[letter];
  28.                 mod_word[x] = num;
  29.             }
  30.             mod_word[word.size()] = 0; //Include a zero for each word
  31.             copy(mod_word, mod_word+word.size()+1, ostream_iterator<int>(ssv));
  32.         }
  33.         ssv >> modify_string; //Put into int modify_string and return
  34.         return modify_string;
  35.     }
  36.     vector<string> decrypt_vector(int& uncrypt){
  37.         string num;
  38.         stringstream ssn;
  39.         ssn << uncrypt; //Store the numbers in a string
  40.         ssn >> num;
  41.         string mod_word;
  42.         string word;
  43.         for(unsigned int i=0;i<num.size();++i){
  44.             int number;
  45.             char check_num = num[i];
  46.             stringstream ssctn;
  47.             ssctn << check_num; //Kinda stupid way of doing it
  48.             ssctn >> number; //Pulls int from num[i], then checks it against 0
  49.             if(number==0){ //If break point
  50.                 uncrypt_vec.push_back(mod_word); //Push back the word
  51.                 mod_word.clear();
  52.             }
  53.             int letter;
  54.             stringstream sscn;
  55.             char let = num[i];
  56.             sscn << let; //Pull each letter from uncrypt, change back to letter based on encryption
  57.             sscn >> letter;
  58.             for(it = encryption.begin(); it != encryption.end(); ++it){
  59.                 if(it->second == letter){
  60.                     //cout << it->first << endl;
  61.                     mod_word.append(it->first);
  62.                     break;
  63.                 }
  64.             }
  65.         }
  66.         cout << "Done" << endl;
  67.         return uncrypt_vec;
  68.     }
  69. }binary;
  70.  
  71. struct player{
  72.     vector<string> inventory;
  73.     int inventory_en; //en for encrypted
  74.     void save(){
  75.         inventory_en = binary.encrypt_vector(inventory);
  76.         cout << inventory_en;
  77.     }
  78.  
  79.     void load(){
  80.         vector<string> inventory; //Must recreate, type is lost otherwise, crashes
  81.         inventory = binary.decrypt_vector(inventory_en);
  82.         vector<string>::iterator i;
  83.         for(i=inventory.begin();i!=inventory.end();++i){
  84.             cout << *i << endl;
  85.         }
  86.     }
  87.  
  88. }character;
  89.  
  90. int main(){
  91.     character.inventory.push_back("abc");
  92.     character.inventory.push_back("edf");
  93.     binary.set_up_code();
  94.     character.save();
  95.     character.load();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement