-1

セルオートマトンを c で作成しましたが、クラスとオブジェクトを使用して c++ に変換したいと考えています。私は c++ が初めてなので、あなたの助けが必要です。10 進数を入力すると、プログラムがクラッシュします。機能間でデータが正しく転送されていないと思いますが、数時間送信しても取得できません。私のエラーがどこにあるのかを見つけるためのアドバイスをいただければ幸いです。私は3つのファイルを持っています。1 つは私のメイン、1 つは関数を含むファイル、最後の 1 つはヘッダーです。

主要:

#include <iostream>
#include "cellular.h"
using namespace std;

int main()
{
    CA myCA;
    myCA.run();
    return 0;
}

関数を含むファイル:

#include "cellular.h"
#include <cstdio>


CA::CA()
{
    int WIDTH = 59;
    int numOfRules = 8;
    currentState = new int [WIDTH];
    nextState = new int[WIDTH];
    storeTheRules = new int[numOfRules];
}
CA::~CA()
{
    delete [] currentState;
    delete [] nextState;
    delete [] storeTheRules;
}

void CA::run()
{
    int x;
    int t;

    //enter which cellular you want to print out
    printf("Enter the number of cellular you want to print out 0-255 (-1 to end):\n");
    scanf("%d", &number);

    while(number != -1) {

        if(number >= 0 && number <= 255) {

            for(x = 0; x < WIDTH; x++) {

                currentState[x] = 0;
            }

            for(x = 0; x < WIDTH; x++) {

                t = (int)WIDTH/2;
                currentState[t] = 1;
            }

            // convert decimal number to binary
            decimalToBinary(number);
            // print binary number
            printf("In binary:");

            for(x = 0; x < numOfRules; x++)
            {
                printf("%d", storeTheRules[x]);
            }

            printf("\n");
            //print current state
            printCellular();
            printf("\n");
            // calculate for next generation
            calcNextGeneration();
            // update array
            updateArray();
        }

        else {

            printf("\nWrong number entered! Try again\n");
        }

        //enter which cellular you want to print out
        printf("\nEnter the number of cellular you want to print out 0-255 (-1 to end):\n");
        scanf("%d", &number);
    }
}
void CA::calcNextGeneration()
{
    int i;
    int j;
    int LENGHT = 27;
    for(j = 0; j < LENGHT; j++) {

        for (i = 0; i < WIDTH; i++) {

            left = currentState[i-1];
            middle = currentState[i];
            right = currentState[i+1];
            nextState[i] = rules(left, middle, right);

        }
        updateArray();
        printCellular();
        printf("\n");
    }
}

int CA::rules(int left,int middle, int right)
{

    if(left == 1 && middle == 1 && right == 1)

        return storeTheRules[0];

    else if(left == 1 && middle == 1 && right == 0)

        return storeTheRules[1];

    else if(left == 1 && middle == 0 && right == 1)

        return storeTheRules[2];

    else if(left == 1 && middle == 0 && right == 0)

        return storeTheRules[3];

    else if(left == 0 && middle == 1 && right == 1)

        return storeTheRules[4];

    else if(left == 0 && middle == 1 && right == 0)

        return storeTheRules[5];

    else if(left == 0 && middle == 0 && right == 1)

        return storeTheRules[6];

    else if(left == 0 && middle == 0 && right == 0)

        return storeTheRules[7];

    return 0;
}

void CA::printCellular()
{
    int i;
    for(i = 0; i < WIDTH; i++) {

        if(nextState[i] == 1 || currentState[i] == 1)

            printf("#");

        else
            printf(" ");
    }
}

void CA::updateArray()
{
    int i;
    for(i = 0; i < WIDTH; i++) {

        currentState[i] = nextState[i];
    }
}

// function to convert decimal number to binary
void CA::decimalToBinary(int n)
{
  int k;
  int i = 0;

  for (numOfRules = 7; numOfRules >= 0; numOfRules--) {

    k = n >> numOfRules;

    if (k & 1)

      storeTheRules[i] = 1;

    else

      storeTheRules[i] = 0;
      i++;
  }
  printf("\n");
}

ヘッダ:

#ifndef CELLULAR_H_INCLUDED
#define CELLULAR_H_INCLUDED


// A cellular automaton class
class CA {
    private:
        int WIDTH;
        int numOfRules;
        int *currentState;
        int *nextState;
        int *storeTheRules;
        int number;
        int left, middle, right;

    public:
        // Constructor
        CA();
        // Destructor
        ~CA();
        // Functions
        void run();
        int rules(int left, int middle, int right);
        void calcNextGeneration();
        void printCellular();
        void updateArray();
        void decimalToBinary(int n);
};

#endif // CELLULAR_H_INCLUDED

CodeBlocks でコードを作成しています。そして.. cstdio を含めるのは、printf を C コードからまだ変更していないためです。

助けてくれてありがとう。よろしく、 ネル

4

1 に答える 1