将数组中的元素移动一定数量

时间:2016-10-14 18:38:26

标签: c++ arrays encryption shift

所以我有一个读取一定数量键的程序。一个例子是

5 1 10 21 9 6 21 11 13 16 20

文本文件示例为

Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2

我希望我的程序从Java开始,并且取决于第一个键,在这种情况下是5,将移动超过5个元素,导致“红色”。等等等等。但是,我已经设法读入所有数据并将它们放入数组中,我只是无法弄清楚如何在数组中移动指针。

代码:

#include <iostream>
#include <fstream>

using namespace std;

struct pieces {

char word[5];
int jump;
} ;

 // Main Function
int main ()
{
// declare variables
int wordCount[2];
int keyCount[2];
int numKeys, numKeys2;
int numWords;
int keyAmount = 1;
int wordAmount = 23;
int keyarr[11];
pieces cypher[wordAmount];
char filename[10];
ifstream inData;
int temp[10];

  //prompt user for input file

cout << " Enter file name: ";
cin >> filename;

inData.open(filename);

if(inData.is_open());
{

    // read in data

for ( numKeys = numWords = 0; numKeys < keyAmount; numKeys++){

// read in num of words and keys
inData >> wordCount[numKeys] >> keyCount[numKeys];

        //read in words followed by jump key
        for( numWords = 0; numWords < wordAmount; numWords++){

        inData >> cypher[numWords].word >> cypher[numWords].jump;

        }

    // read in the actual keys
    for( numKeys2 = 0; numKeys2 < wordCount[numKeys]; numKeys2++){

    inData >> keyarr[numKeys2];

    }

}


//print out data

for( int j = 0; j < numKeys; j++){

cout << wordCount[j] << "\n";
cout << keyCount[j] << "\n";

}

cout << "\n";

for ( int i = 0; i < wordAmount; ++i){
cout << cypher[i].word << " ";
cout << cypher[i].jump << " ";

}

cout << "\nKeys: " << "\n";

for(int k = 0; k < 11; k++){

cout << keyarr[k] << " ";

}
cout << "\n";

}

inData.close();

  return 0;
}

0 个答案:

没有答案