0

注:同じ名前の変数が2つありました...それに気付いてくれたStefan BirladeanuとHenrikに感謝します!*

最近、4つの変数を使用してbool関数の値をVeitch(Karnaugh)図に入力するのに役立つコードを書き始めました。コードは、要素を行列サイズ4x4に書き込む必要がありますが、これらのインデックスを使用します。

  1. 要素-インデックス3,3
  2. 要素-インデックス2,3
  3. 要素-インデックス3,2
  4. 要素-インデックス2,2
  5. 要素-インデックス0,3
  6. 要素-インデックス1,3
  7. 要素-インデックス0,2
  8. 要素-インデックス1,2
  9. 要素-インデックス3,0
  10. 要素-インデックス2,0
  11. 要素-インデックス3,1
  12. 要素-インデックス2,1
  13. 要素-インデックス0,0
  14. 要素-インデックス1,0
  15. 要素-インデックス0,1
  16. element --index 1,1これはmain()のコードです:

        void main()
        {
            int n;
    
        n=4;
    
        int **VeitchDiagram;
    
        //allocate memory for Veitch diagram
        VeitchDiagram = new int *[n];
        for(int i=0; i<n; i++)
            VeitchDiagram[i]=new int [n];
    
        //enter the elements
        for(int i=0; i<n; i++)
        {
            int j, k;
            if(i%2==1)
            {
                k=0;
                if(i<2)
                    j=4;
                else
                    j=-1;
                for(int k=0; k<2; k++)
                {
                    if(i<2)
                        j--;
                    else
                        j++;
                    cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                    k++;                            //0,3     1,3     0,2     1,2     if i%2==1 and i<2
                    cin >> VeitchDiagram[k][j];     //0,0     1,0     0,1     1,1     if i%2==1 and i>=2
                    k--;
                }
            }
            else
            {
                k=3;
                if(i<2)
                    j=4;
                else
                    j=-1;
                for(int k=0; k<2; k++)
                {
                    if(i<2)
                        j--;
                    else
                        j++;
                    cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                    k--;                            //3,3     2,3     3,2     2,2    if i%2==0 and i<2
                    cin >> VeitchDiagram[k][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                    k++;
                }
            }
        }
    
        //free memory allocated for VeitchDiagram
        for(int i=0; i<n; i++)
            delete [] VeitchDiagram[i];
        delete [] VeitchDiagram;
    }
    
4

3 に答える 3

2
        for(int k=0; k<2; k++)
        {
            if(i<2)
                j--;
            else
                j++;
            cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
            k--;                            //3,3     2,3     3,2     2,2    if i%2==0 and i<2
            cin >> VeitchDiagram[k][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                                 ^ k == -1

しかし、実際にはデバッガーの使用方法を学ぶ必要があります。

于 2012-03-23T14:17:42.343 に答える
2

i = 0の場合、このブランチに到達します

else
            {
                k=3;
                if(i<2)
                    j=4;
                else
                    j=-1;
                for(int k=0; k<2; k++)
                {
                    if(i<2)
                        j--;
                    else
                        j++;
                    cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                    k--;                            //3,3     2,3     3,2     2,2    if i%2==0 and i<2
                    cin >> VeitchDiagram[k][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                    k++;
                }
            }

k=0の場合

cin >> VeitchDiagram[k /* = 0  OK */][j];     //this part writes the input to elements with index (at least it should do that):
                    k--; //decrease it                            //3,3     2,3     3,2     2,2     if i%2==0 and i<2
                    cin >> VeitchDiagram[k /* here k = -1 BAD!!! */][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                    k++;
于 2012-03-23T14:21:59.237 に答える
1

他の場所で述べたように、配列の外側でインデックスを作成しています。
提案と同じように、テーブルベースのバージョンを正しく理解するのはそれほど難しいことではありません。

const size_t k_index[] = {3,2,3,2,0,1,0,1,3,2,3,2,0,1,0,1};
const size_t j_index[] = {3,3,2,2,3,3,2,2,0,0,1,1,0,0,1,1};

int main()
{
    const int n = 4;
    int VeitchDiagram[n][n]; // No need for dynamic allocation here.

    //enter the elements
    for(int i = 0; i < n * n; i++)
    {
        cin >> VeitchDiagram[k_index[i]][j_index[i]];
    }
}

同様に数行短くなっています。

于 2012-03-23T15:08:43.467 に答える