C程序创建数组结构

时间:2013-11-10 17:30:08

标签: c arrays struct enums

我正在尝试创建一个允许用户使用的数组库 使用单个接口创建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

谢谢

0 个答案:

没有答案