1

我正在尝试创建一个数组库,允许用户使用单个接口来创建 int、char、float 或 double 类型的 1 维、2 维或 3 维数组。我目前的方法似乎太复杂了,我想知道我是否走在正确的轨道上,谢谢。

typedef enum type
{
    CHAR, INT, FLOAT, DOUBLE
}

Type;

typedef struct array
{
    void *values;          // pointer to the data
    Type type;           // type of data
    int rank;            // # of dimensions (1, 2, and 3)
    int dimensions[3];   // a,b,c array dimensions
}

MultiArray;

MultiArray * array_init(int rank, int dims[], Type type); //init new Array

Array a;
a.ranking = ranking;
a.type = type;

switch (ranking)
{

    case 3:
    a.dimensions[0] = dimensions[0];
    a.dimensions[1] = dimensions[1];
    a.dimensions[2] = dimensions[2];

    switch (type)
    {

            case CHAR:
            values = new char**[dimensions[0]];

            for (int i = 0;i < dimensions[0];i++)
            {
                (((char***)values)[i]) *= new char * [dimensions[1]];

                for (int j = 0;j < dimensions[1];j++)
                    ((((char***)values)[i])*[j]) *= new char[dimensions[2]];
            }

            break;

            case INT:
            values = new int**[dimensions[0]];

            for (int i = 0;i < dimensions[0];i++)
            {
                (((int***)values)[i]) *= new int * [dimensions[1]];

                for (int j = 0;j < dimensions[1];j++)
                    ((((int***)values)[i])*[j]) *= new int[dimensions[2]];
            }

            break;

            case FLOAT:
            values = new float**[dimensions[0]];

            for (int i = 0;i < dimensions[0];i++)
            {
                (((float***)values)[i]) *= new float * [dimensions[1]];

                for (int j = 0;j < dimensions[1];j++)
                    ((((float***)values)[i])*[j]) *= new float[dimensions[2]];
            }

            break;

            case DOUBLE:
            values = new double**[dimensions[0]];

            for (int i = 0;i < dimensions[0];i++)
            {
                (((double***)values)[i]) *= new double * [dimensions[1]];

                for (int j = 0;j < dimensions[1];j++)
                    ((((double***)values)[i])*[j]) *= new double[dimensions[2]];
            }

            break;
    }

    break;

    case 2:
    a.dimensions[0] = dimensions[0];
    a.dimensions[1] = dimensions[1];

    switch (type)
    {

            case CHAR:
            values = new char * [dimensions[0]];

            for (int i = 0;i < dimensions[0];i++)
                (((char**)values)[i]) *= new char[dimensions[1]];

            break;

            case INT:
            values = new int * [dimensions[0]];

            for (int i = 0;i < dimensions[0];i++)
                (((int**)values)[i]) *= new int[dimensions[1]];

            break;

            case FLOAT:
            values = new float * [dimensions[0]];

            for (int i = 0;i < dimensions[0];i++)
                (((float**)values)[i]) *= new float[dimensions[1]];

            break;

            case DOUBLE:
            values = new double * [dimensions[0]];

            for (int i = 0;i < dimensions[0];i++)
                (((double**)values)[i]) *= new double[dimensions[1]];

            break;
    }

    break;

    case 1:
    a.dimensions[0] = dimensions[0];

    switch (type)
    {

            case CHAR:
            values = new char[dimensions[0]];
            break;

            case INT:
            values = new int[dimensions[0]];
            break;

            case FLOAT:
            values = new float[dimensions[0]];
            break;

            case DOUBLE:
            values = new double[dimensions[0]];
            break;
    }

    break;
}

return a;

void display_array(Array * arr); // prints Array

谢谢

4

0 回答 0