Encoder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shing
    New Member
    • Mar 2007
    • 58

    Encoder

    I made this python script with another guy a year ago

    Code:
    ### This is a dictionary...
    MyDict={'a':'$','b':'i','c':'q','d':'s','e':'w','f':'~','g':'d','h':'2',
            'i':'5','j':'6','k':'a','l':'g','m':'-','n':'j','o':'z','p':'%',
            'q':'h','r':'9','s':'f','t':'7','u':'+','v':'c','w':'0','x':'8',
            'y':'/','z':'x',' ':'*'}
    
    ## This is just to show the mapping of the 'encoding'
    for k,v in MyDict.iteritems():
        print k,' will be encoded to.. ',v
        
    ## Here we ask for the word to be encoded
    ## Notice that i use the '.lower' method in case
    ##    the user inserts any uppercase letters
        
    Word=raw_input("What is the word you want to encode?").lower()
    
    ## This is an empty string to begin with
    Encoded_Word=''
    
    ## Now we iterate over the inserted word to check each letter
    for letter in Word:
        if MyDict.has_key(letter):
            ## Insert to the string the coresponding character..
            Encoded_Word=Encoded_Word+MyDict.get(letter)
    
    ## Print the encoded word
    print Encoded_Word
    How do you make a dictionary in C++? And how do I make this script in the same format for a C++ program? I've tried in a very unethical and unlogical manner :P

    Code:
    //Encoder
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        
      int A1;  
      int a;
      int b;
      int c;
      int d;
      int e;
      int f;
      int g;
      int h;
      int i;
      int j;
      int k;
      int l;
      int m;
      int n;
      int o;
      int p;
      int q;
      int r;
      int s;
      int t;
      int u;
      int v;
      int w;
      int x;
      int y;
      int z;
      int A3;
    
      system("cls");
      cout << "--------------------------------------------------------------------------------";
      cout << "Encoder";
      cout << "\n--------------------------------------------------------------------------------";
      
      \u0020 = "*"
      a = "$"
      b = "i";
      c = "q";
      d = "s";
      e = "w";
      f = "~";
      g = "d";
      h = "2";
      i = '5";
      j = "6";
      k = "a";
      l = "g";
      m = "-";
      n = "j";
      o = "z";
      p = "%";
      q = "h";
      r = "9";
      s = "f";
      t = "7";
      u = "+";
      v = "c";
      w = "0";
      x = "8";
      y = "/";
      z = "x";
     
          
      cout << "What is the word you want to encode?";
      cin >> A1
      
      A3 = A1 +
      
      cout << A1;
         
      cout << "\n--------------------------------------------------------------------------------";
      cout << "Bye";
      cout << "\n--------------------------------------------------------------------------------";
            
      return 0;
    
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    While I would recommend using a data structure from the standard template library, I think the most equivalent structure would be a hash map.

    However, depending on exactly what you are doing, that may not be the best choice for you. But it looks like the encoding is a static 1 to 1 replacement cypher, so it should work.

    Comment

    • shing
      New Member
      • Mar 2007
      • 58

      #3
      Originally posted by sicarie
      While I would recommend using a data structure from the standard template library, I think the most equivalent structure would be a hash map.

      However, depending on exactly what you are doing, that may not be the best choice for you. But it looks like the encoding is a static 1 to 1 replacement cypher, so it should work.
      I don't quite understand the Standard template library. :(

      This is what I have currently:

      Code:
      #include <iostream>
      
      using namespace namespace_name std::map;
      {
        int <key_type, data_type, [comparison_function]>;
        int <string, char> encoder;
        
      }
      
      std::map <key_type, data_type, [comparison_function]>;
      
      std::map <string, char> encoder;
      
      encoder["a"] = '*',
      encoder["b"] = 'f';
      encoder["c"] = '[';
      encoder["d"] = 'p';
      encoder["e"] = ']';
      encoder["f"] = '\';
      encoder["g"] = 'm';
      encoder["h"] = 'z';
      encoder["i"] = 'n';
      encoder["j"] = '-';
      encoder["k"] = '_';
      encoder["l"] = ',';
      encoder["m"] = 'W';
      encoder["n"] = '`';
      encoder["o"] = '~';
      encoder["p"] = '&';
      encoder["q"] = 'i';
      encoder["r"] = 'x';
      encoder["s"] = '^';
      encoder["t"] = '+';
      encoder["u"] = 'j';
      encoder["v"] = 'q';
      encoder["w"] = '9';
      encoder["x"] = '4';
      encoder["y"] = '.';
      encoder["z"] = '=';
      
      int main ()
      
      {
               int a;
               
               cout << "Encoded word?";
               cin >> a;
               
               cout << "Encoded word is " << a;
               
               return 0;
               
      }

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by shing
        I don't quite understand the Standard template library. :(
        Our resident oracle, weaknessforcats , just posted something that may help you in this thread.

        Edit- This is assuming that you are understanding the language after your jump from Python. If you are looking for a more language-specific, C++ for programmer's type reference, post again and I'm sure we (by which I mean weaknessforcats as I have yet to see the limits of his knowledge) can come up with something.
        Last edited by sicarie; Mar 11 '08, 05:33 PM. Reason: Edit

        Comment

        Working...