2014-01-10 30 views
0

嗨,大家好和SRY我的英文不好^^ ......我想異或一些char*陣列,如:異或的char *數組

"hello"^"moin"^"servus" = xorUnit 

和異或回原來的char*陣列狀:

"hello"^"moin"^xorUnit = "servus" 

但我得到的成果是將「serv」,而不是「SERVUS」,好像與長度的問題,希望你能幫助我:)

#include <iostream> 
#include <iomanip> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <vector> 

using namespace std; 

struct UNIT 
{ 
    int size; 
    char *content; 
}; 

UNIT XOR(vector<UNIT> list) 
{ 
    char* unit; 
    char* temp; 

    UNIT xorUnit; 
    int unitLength = 0; 
    int maxLength = 0; 
    int minLength = 0; 
    int bigger = 0; 

    for(int i = 0; i < list.size(); i++) 
    { 
    unitLength = list[i].size; 

    if(minLength == 0 && maxLength == 0) 
    { 
     maxLength = unitLength; 
    } 
    else if(unitLength > maxLength) 
    { 
     minLength = maxLength; 
     maxLength = unitLength; 
     bigger = 1; 
    } 
    else if(unitLength < maxLength) 
    { 
     minLength = unitLength; 
     bigger = 0; 
    } 
    printf("bigger: %d\n", bigger); 
    printf("MaxLänge: %d\n", maxLength); 
    printf("MinLänge: %d\n", minLength); 

    temp = new char[maxLength]; 

    for(int j = 0; j < minLength; j++) 
    { 
     temp[j] = list[i].content[j]^unit[j]; 
    } 
    for(int j = minLength; j < maxLength; j++) 
    { 
     temp[j] = list[i].content[j]; 
    } 
    unit = temp; 
    } 

    xorUnit.content = unit; 
    if(bigger == 0) { 
    xorUnit.size = minLength; 
    } else { 
    xorUnit.size = maxLength; 
    } 
    bigger = 0; 

    return xorUnit; 
} 


int main(int argc,char **argv) {  
    int i; 

    vector<UNIT> list; 
    vector<UNIT> backupList; 

    //Eingabeunits 
    UNIT input1; 
    UNIT input2; 
    UNIT input3; 

    UNIT xorUnit; 
    UNIT output; 

    input1.size = 5; 
    input1.content = "hallo";  
    input2.size = 6; 
    input2.content = "servus"; 
    input3.size = 4; 
    input3.content = "moin"; 

    list.push_back(input1); 
    list.push_back(input2); 
    list.push_back(input3); 

    for(i = 0; i < list.size(); i++) 
    { 
     output = list[i]; 
     printf("Vector(%d): %s %d\n", i, output.content, output.size); 
    } 

    xorUnit = XOR(list); 
    printf("XOR: %s %d\n\n\n", xorUnit.content, xorUnit.size); 

    backupList.push_back(input3); 
    backupList.push_back(input1); 
    backupList.push_back(xorUnit); 

    for(i = 0; i < backupList.size(); i++) 
    { 
     output = backupList[i]; 
     printf("Vector(%d): %s %d\n", i, output.content, output.size); 
    } 

    xorUnit = XOR(backupList); 
    printf("XOR: %s %d\n", xorUnit.content, xorUnit.size); 

    return 0; 
} 

回答

2

所以,我完全不清楚爲什麼你的異或函數是如此複雜。如果我沒有理解你的問題,XOR功能可以簡單地寫爲:

UNIT XOR(vector<UNIT> list) 
{ 
    // Find the length of the longest unit 
    int maxLength = 0; 
    for (int i=0; i<list.size(); ++i) 
     maxLength = std::max(maxLength, list[i].size); 

    // Allocate space for the new unit. 
    // Note that calloc will zero out any allocated space. 
    UNIT xorUnit; 
    xorUnit.size = maxLength; 
    xorUnit.content = (char *) calloc(maxLength + 1, sizeof(char)); 

    // xor each member of the list with xorUnit 
    for (int i = 0; i<list.size(); ++i) 
     for (int j=0; j<list[i].size; ++j) 
      xorUnit.content[j] ^= list[i].content[j]; 

    return xorUnit; 
} 

這將產生的輸出:

Vector(0): hallo 5 
Vector(1): servus 6 
Vector(2): moin 4 
XOR: vkwts 6 


Vector(0): moin 4 
Vector(1): hallo 5 
Vector(2): vkwts 6 
XOR: servus 6 

注意:你是這個例子,你得到相當幸運可打印的字符回來。一般來說,這種類型的代碼會產生很多字符,其中而不是被認爲是可打印的字符。例如,"Hi"^"Ho" => "\0\9",它們都不是可打印的字符。

+0

非常感謝你:) – user3183364