2

VISUALC++の質問

やあ、

3つの要素の配列があり、その要素を右にシフトして、シフトされたインデックスセルを「SHIFTED」文字列に置き換えたいと思います。これは、すべてのセルが「SHIFTED」文字列になるまでループするはずです。


例えば:

int a[x]={0,1,2};

初期インデックスと要素順序:

[0]=0
[1]=1
[2]=2

次のようになる必要があります:

最初のループ:

 [0]=SHIFTED
 [1]=0
 [2]=1

2番目のループ:

 [0]=SHIFTED
 [1]=SHIFTED
 [2]=0

3番目のループ:

 [0]=SHIFTED
 [1]=SHIFTED
 [2]=SHIFTED

memmove()でそれができることは知っていますが、関数を使用したくありません。

手伝ってくれませんか。これが私の仕事です:

#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
using namespace std;

int const ARRAY_SIZE=3;

int main()
{
 int Array[ARRAY_SIZE];
 int iniPostion,newPostion;
 string x="SHIFTED";
 for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++)
 {
  Array[iniPostion] = iniPostion;
  cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
 }
 cout << endl;
 for(newPostion=0; newPostion<ARRAY_SIZE; newPostion++)
 {
  Array[newPostion]=newPostion;
  cout << "Cell [" << newPostion << "]     New Element is: (";
  if(Array[newPostion-1]<0)
  {
   cout << x << ")\n";
  }
  else
  {
   cout << Array[newPostion-1] << ")" << endl;
  }
 }
 return 0;
}
4

6 に答える 6

3

ピーターが答えで述べたように、文字列を int に割り当てることはできません。であると仮定SHIFTEDしました-1。したがって、シフトするたびに-1、作成されたギャップが生じます。

2 つのループが必要です。外側のループは (3) 回反復Nし、内側のループは配列の末尾から始まり、(n-1)th要素を次のnth位置にコピーします。

for(int count = 0;count < N;count++){
    for(newPostion=ARRAY_SIZE-1; newPostion > count;newPostion--)
        Array[newPostion]=Array[newPostion - 1]; // copy
    Array[newPostion]= -1; // fill the gap.

    // print.
    for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++) {
        cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
    }
    cout<<endl;
}

サンプルラン:

# g++ a.cpp && ./a.out
Cell [0] Initial Element is: (0)
Cell [1] Initial Element is: (1)
Cell [2] Initial Element is: (2)

Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (0)
Cell [2] Initial Element is: (1)

Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (-1)
Cell [2] Initial Element is: (0)

Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (-1)
Cell [2] Initial Element is: (-1)
于 2010-03-07T14:58:56.567 に答える
2

整数の配列に文字列 ("SHIFTED") が含まれていることをどのように期待できるかは、少し不明です。

ただし、この操作では、回転アルゴリズムを使用できます。

#include <iostream>
#include <algorithm>
#include <string>

int const ARRAY_SIZE=3;

void print(std::string* array) {
    for (int i = 0; i != ARRAY_SIZE; ++i) {
        std::cout << array[i] << ' ';
    }
    std::cout << '\n';
}

int main()
{
    std::string Array[ARRAY_SIZE] = {"0", "1", "2"};
    print(Array);

    //the last item ends up at the beginning of the array
    std::rotate(Array, Array + ARRAY_SIZE - 1, Array + ARRAY_SIZE);

    //now overwrite the item that used to be last
    Array[0] = "SHIFTED";
    print(Array);
    return 0;
}

std::dequeまたは最後の値と新しい値を格納std::listできる場所など、適切なコンテナを使用すると、おそらくよりシンプルで効率的になります。pop_backpush_front

于 2010-03-07T14:58:39.817 に答える
1

3つの要素の配列があり、その要素を右にシフトし、シフトされたインデックスセルを「SHIFTED」文字列に置き換えたいと考えています。これは、すべてのセルに「SHIFTED」文字列が含まれるまでループする必要があります。

これはまったく意味がありません。数値 (整数) の配列がありますが、もちろん文字列を含めることはできません。できることは、たとえば、シフトされた要素を示すために 0 または -1 を挿入することです。

シフト自体は、 std::rotate操作によって簡単に実装できます。

しかし、すべての要素には最終的に同じものが含まれているため、シフトせずに直接割り当ててみませんか?

于 2010-03-07T14:57:31.647 に答える
1

これが私の単純な古いCでの簡単な解決策です

#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv) {

    int MAX_LEN = 11;
    int numOfShifts = 1;
    if ( argc > 1 ) {
        numOfShifts = atoi(argv[1]);
    }
    printf("Number of shifts = %d\n",numOfShifts);

    int arr[] = { 0,1,2,3,4,5,6,7,8,9,10 };
    int i;
    int n; // number of shifts index

    for ( n = 0; n < numOfShifts; n++ ) {
        for ( i = MAX_LEN - 1; i >= 0; i-- ) {
            if ( i == 0 ) {
                arr[i] = -1;
            } else {
                arr[i] = arr[i-1];
            }
        }
    }

    // output
    for( i = 0; i < MAX_LEN; i++ ) {
        printf("Arr[%d] = %d\n", i, arr[i]);
    } 

}
于 2010-03-07T15:56:56.260 に答える
0

これは宿題の匂いがする...

行ったシフトの数を追跡して、印刷するときにそれを考慮に入れることができない理由はありますか?

#include <iostream>

int const ARRAY_SIZE=3;

class Shifter
{
    public:
        Shifter(const int* array_, size_t size_)
        : array(array_), size(size_), shifts(0) {}
        void Shift(size_t steps = 1) { shifts += steps; if(shifts > size) shifts = size; }
        void Print(std::ostream& os) const;
    private:
        const int* array;
        size_t size;
        size_t shifts;
};

void Shifter::Print(std::ostream& os) const
{
    for(size_t i = 0; i < size; ++i)
    {
        os << "Cell [" << i << "]  = ";
        if(i < shifts)
            os << "SHIFTED";
        else
            os << array[i - shifts];
        os << std::endl;
    }
}

std::ostream& operator <<(std::ostream& os, const Shifter& sh)
{
    sh.Print(os);
    return os;
}

int main(void)
{
    // Initialize the array.
    int a[ARRAY_SIZE];
    for(size_t iniPostion=0; iniPostion < ARRAY_SIZE; iniPostion++)
        a[iniPostion] = iniPostion;
    Shifter sh(a, ARRAY_SIZE);
    std::cout << "Initial contents:" << std::endl;
    std::cout << sh << std::endl;

    // Do the shifts.
    for(size_t newPostion = 0; newPostion < ARRAY_SIZE; newPostion++)
    {
        std::cout << "Shift by 1..." << std::endl;
        sh.Shift();
        std::cout << sh << std::endl;
    }

    return 0;
}
于 2010-03-07T15:30:57.280 に答える
0

メモリを簡単に移動できます。

// shift down
int a[ 10 ];
memmove( a, a +1, sizeof( a ) -sizeof( a[ 0 ] ) );
于 2012-04-17T15:50:28.143 に答える