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;
}
非常感謝你:) – user3183364