带有类和对象的c ++中的元胞自动机

时间:2015-03-10 17:51:12

标签: c++ class object automaton

我在c中完成了我的元胞自动机,但现在我想使用类和对象将其转换为c ++。我是c ++的新手,这就是我需要你帮助的原因。键入十进制数后,我的程序崩溃了。我认为数据在功能之间没有正确传输,但我发送了几个小时,我无法得到它。如果我能在找到错误的时候得到任何建议,我会很高兴。我有3个档案。一个是我的主要文件,一个是带有函数的文件,最后一个是标题。

主:

#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是因为我还没有从C代码改变我的printf。

感谢您的帮助。 问候, NEL

1 个答案:

答案 0 :(得分:0)

我没有读完所有内容,但乍一看有几个问题:

  • 在构造函数中,您正在创建局部变量,而不是访问要修改的类变量。

    int WIDTH = 59;
    int numOfRules = 8;
    
  • 此外,作为个人偏好,我不会以这样的方式组织它,即从用户获取输入的数据输入循环是类的一部分。这可能只是个人偏好。