左移一个给定值的数组

时间:2017-10-04 19:54:58

标签: c++ arrays bit-shift shift

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void shiftLeft (char myarray[], int size, int shiftBy)
{
    if(shiftBy > size){
        shiftBy = shiftBy - size;
    }

    if(size == 1){
        //do nothing
    }
    else{
        char temp;
        //for loop to print the array with indexes moved up (to the left) <-- by 2
        for (int i=0; i < size-shiftBy; i++)
        {//EXAMPLE shift by 3  for a c-string of 5
            temp = myarray[i];//temp = myarray[0]
            myarray[i] = myarray[i + shiftBy];//myarray[0] == myarray[2]
            myarray[i + shiftBy] = temp;//myarray[2] = temp(value previously at index i)
        }

    }
}

int main ()
{
    char text[] = "OPAKMIGWPKBTWAQSZQABTAVWASZGETAATGCEWQEAVFZMHATXSOQLPAMOTZMIATXFWFKMQEMELWBHQZYPQJMFGEMLKTZQEOWAVARATXMXIABTKVAOUKDQFLTQZWDKMYMAFLPUFSULYGWMDLWELWBOQFZWGLIRSZYZWGKMZWIDTMFOMQFBTWEAGLESVPXZARMZDIWWBTWLMJSQKBQNMZAVSGNFZMKWIDZMSADQKPUKPMJVQKANWTXKIEZIWWBASAWANFZMDWQEKWYWUUKBMCMFZMAFTKGBTWZEGCZVAFZMEOMQHWRWIEQEUFLMFLPGEZQNXSSQLPQOWAVAMJMXGDQDGPSZWSVPVMQHJGLQTSDQHZAEQEWAFGSQWXMFLYATQKBAYWNWNAJMUKTQWXMFLYATQKBAYWNWNAJMUKTQWX" ;
    char textTwo[] = "OPAKMIGWPKBTWAQSZQABTAVWASZGETAATGCEWQEAVFZMHATXSOQLPAMOTZMIATXFWFKMQEMELWBHQZYPQJMFGEMLKTZQEOWAVARATXMXIABTKVAOUKDQFLTQZWDKMYMAFLPUFSULYGWMDLWELWBOQFZWGLIRSZYZWGKMZWIDTMFOMQFBTWEAGLESVPXZARMZDIWWBTWLMJSQKBQNMZAVSGNFZMKWIDZMSADQKPUKPMJVQKANWTXKIEZIWWBASAWANFZMDWQEKWYWUUKBMCMFZMAFTKGBTWZEGCZVAFZMEOMQHWRWIEQEUFLMFLPGEZQNXSSQLPQOWAVAMJMXGDQDGPSZWSVPVMQHJGLQTSDQHZAEQEWAFGSQWXMFLYATQKBAYWNWNAJMUKTQWXMFLYATQKBAYWNWNAJMUKTQWX";
    string OG="OPAKMIGWPKBTWAQSZQABTAVWASZGETAATGCEWQEAVFZMHATXSOQLPAMOTZMIATXFWFKMQEMELWBHQZYPQJMFGEMLKTZQEOWAVARATXMXIABTKVAOUKDQFLTQZWDKMYMAFLPUFSULYGWMDLWELWBOQFZWGLIRSZYZWGKMZWIDTMFOMQFBTWEAGLESVPXZARMZDIWWBTWLMJSQKBQNMZAVSGNFZMKWIDZMSADQKPUKPMJVQKANWTXKIEZIWWBASAWANFZMDWQEKWYWUUKBMCMFZMAFTKGBTWZEGCZVAFZMEOMQHWRWIEQEUFLMFLPGEZQNXSSQLPQOWAVAMJMXGDQDGPSZWSVPVMQHJGLQTSDQHZAEQEWAFGSQWXMFLYATQKBAYWNWNAJMUKTQWXMFLYATQKBAYWNWNAJMUKTQWX";
    int size =OG.length(); 
    int count = 0;
    ofstream outputFile("info.txt");
    for (int i =1; i<size+1;i++){
        shiftLeft(text,size,i);
        string name="OPAKMIGWPKBTWAQSZQABTAVWASZGETAATGCEWQEAVFZMHATXSOQLPAMOTZMIATXFWFKMQEMELWBHQZYPQJMFGEMLKTZQEOWAVARATXMXIABTKVAOUKDQFLTQZWDKMYMAFLPUFSULYGWMDLWELWBOQFZWGLIRSZYZWGKMZWIDTMFOMQFBTWEAGLESVPXZARMZDIWWBTWLMJSQKBQNMZAVSGNFZMKWIDZMSADQKPUKPMJVQKANWTXKIEZIWWBASAWANFZMDWQEKWYWUUKBMCMFZMAFTKGBTWZEGCZVAFZMEOMQHWRWIEQEUFLMFLPGEZQNXSSQLPQOWAVAMJMXGDQDGPSZWSVPVMQHJGLQTSDQHZAEQEWAFGSQWXMFLYATQKBAYWNWNAJMUKTQWXMFLYATQKBAYWNWNAJMUKTQWX";
        for(int j=0;j<size;j++){
            if(text[j]==textTwo[j])
            count++;
        }
        outputFile  <<"Shift right by: " << 422-i << " Coincidences: " << count <<"text:" <<text <<endl; 
        strcpy_s(text, name.c_str());
        count = 0;
    }
outputFile.close();
    return 0;
}

我的目标是破解The Vigen`ere Cipher,但是我需要执行以下操作,将从0开始的位移到文本大小,并检查移位和原始文本是否匹配,它们匹配的最大值给了我关键空间,但我的代码没有正常工作,向左移动似乎不起作用,但我测试了小输入它在小输入上工作正常..

1 个答案:

答案 0 :(得分:0)

可以通过移动元素或更改索引来移动数组。

通过移动元素移动 对每个插槽重复:

array[i] = array[i+1];
// or 
array[i+1] = array[i];

通过移动元素进行旋转

i = 0;
carry_value = array[i];
// Loop:
array[i] = array[i+1];
// end loop
array[last_slot] = carry_value;

Caesar Cipher通过更改索引值来工作:

new_value = array[(index + offset) % array_capacity];