2012-06-17 113 views
1

使用C++,我試圖編寫一個加密字符串的程序。我必須將字母表中的每個字母映射到一個數字值:例如,a = 0,b = 1,c = 2等等。到目前爲止,我已經創建了一個void函數,它將一個字符串作爲參數並使用switch語句輸出值。問題是,這些值是字符,而不是整數,我不能使用數學運算符來修改它們。 我的源代碼低於:將數字映射到字符

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <cstring> 
#include <string> 
#include <cctype> 

using namespace std; 

void mapped_string(string in) 
{ string mapped_string; 
    int length = in.length(); 

    for (int i = 0; i < length; i++) 
    { 
     switch (in[i]) 
     { 
     case 'a': 
      cout<<"0"; 
      break; 
     case 'b': 
      cout<<"1"; 
      break; 
     case 'c': 
      cout<<"2"; 
      break; 
     case 'd': 
      cout<<"3"; 
      break; 
     case 'e': 
      cout<<"4"; 
      break; 
     case 'f': 
      cout<<"5"; 
      break; 
     case 'g': 
      cout<<"6"; 
      break; 
     case 'h': 
      cout<<"7"; 
      break; 
     case 'i': 
      cout<<"8"; 
      break; 
     case 'j': 
      cout<<"9"; 
      break; 
     case 'k': 
      cout<<"10"; 
      break; 
     case 'l': 
      cout<<"11"; 
      break; 
     case 'm': 
      cout<<"12"; 
      break; 
     case 'n': 
      cout<<"13"; 
      break; 
     case 'o': 
      cout<<"14"; 
      break; 
     case 'p': 
      cout<<"15"; 
      break; 
     case 'q': 
      cout<<"16"; 
      break; 
     case 'r': 
      cout<<"17"; 
      break; 
     case 's': 
      cout<<"18"; 
      break; 
     case 't': 
      cout<<"19"; 
      break; 

     default: 
      cout<< in[i]; 
     } 
    } 
} 

int main() 
{ string str1 = "Hello"; 

    mapped_string(str1); 



     cout << "Press any key to exit." << endl; 
     cin.ignore(2); 
     return 0; 
} 

我需要這個功能映射字符串到int值中的每個字符和整型值存儲在一個名爲mapped_string新的字符串。

回答

0
//stringstream is used as string 'builder'. 
std::stringstream storedstr; 

for (int i = 0; i < length; i++) 
{ 
    //As op's code but without the lengthy switch statement. Just cast to get an int. 
    int intEquiv = (int)(in[i]); 
    if (in[i] > 'z' || in[i] < 'a') intEquiv = (int)(in[i]) - 'a' 
    storedstr << intEquiv; 

    //Show what we are adding, like in original loop; 
    std::stringstream ss; 
    ss << intEquiv; 
    std::cout << ss.str(); 
} 
mapped_string = storedstr.str(); 
0
字符數

在C/C++有數值,ASCII格式, 'A' 是65, 'B' 是66 ... 'a' 爲97, 'b' 爲98等,作爲這樣你可以應用它們的數學運算,它可能更容易簡單地粘到衆所周知的ASCII格式,而不是創建自己的,http://en.wikipedia.org/wiki/ASCII

string str1 = "Hello"; 
for (int index = 0; index < str1.length(); index++) 
    cout << "char: " << str1[index] << " ascii numeric values: " << (int)(str1[index]) << endl; 

打印:

char: H ascii numeric values: 72 
char: e ascii numeric values: 101 
char: l ascii numeric values: 108 
char: l ascii numeric values: 108 
char: o ascii numeric values: 111 

簡單/幼稚加密計劃將是簡單地轉換字符b y一定的數量(移位密碼),但在使用加密方案時,根據您正在實施的方案,您應該始終注意溢出。

string str1 = "Hello", cipher = "";  
    for (int index = 0; index < str1.length(); index++) 
     cipher += (char)(str1[index] + 10); 
    cout << "original: " << str1 << " cipher " << cipher << endl; 

打印:

original: Hello cipher Rovvy 

找回原來只是減去偏移。

0

用C++ 11:

#include <iostream> 
#include <string> 
#include <algorithm> 
using namespace std; 

string mapped_string(string in, char password) 
{ 
    transform(in.begin(), in.end(), in.begin(), [password](char& x){ return x^password; }); 
    return in; 
} 

int main(int argc, char* argv[]) 
{ 
    char password = '1'; 
    string str1 = "Hello"; 
    string str2 = mapped_string(str1, password); // encrypt str1 
    string str3 = mapped_string(str2, password); // decrypt str2 
    cout << "Original: " << str1 << endl; 
    cout << "Encrypt: " << str2 << endl; 
    cout << "Decrypt: " << str3 << endl; 
    cin.ignore(); 
    return 0; 
} 

輸出:
原件:你好
加密:YT]]
解密:你好