三维数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三维数组相关的知识,希望对你有一定的参考价值。

参考技术A #include<stdio.h>

#include<stdlib.h>

#define ERROR 0

#define OK 1

#define NotPresent 2

#define Duplicate 3

#define IllegalIndex 4

typedef int Status;

typedef struct triplearray

int m1;

int m2;

int m3;

int *array;

TripleArray;

//创建运算:CreateArray(A,m1,m2,m3,...mn)申请n维数组A所需存储空间并分配给数组A,每个维度的长度分别为m1,m2,m3......;成功分配则返回OK,否则,返回ERROR;

Status CreateArray(TripleArray *TripleArray, int m1, int m2, int m3)

TripleArray->m1 = m1;

TripleArray->m2 = m2;

TripleArray->m3 = m3;

TripleArray->array = (int*)malloc(m1*m2*m3*sizeof(int));

if (!TripleArray->array)

return ERROR;

return OK;



//清除运算:判断数组是否存在,若存在,则撤销数组,函数返回OK,否则,返回ERROR;

Status DestroyArray(TripleArray *TripleArray)

if (!TripleArray)

return ERROR;

if (TripleArray->array)

free(TripleArray->array);

free(TripleArray);

return OK;



//数组元素查询运算,判断数组是否存在,若数组不存在,函数返回ERROR,否则对数组下标边界检查,若下标合法,在参数x中返回下标为 i1,i2,i3,... 的数组元素;

Status RetrieveArray(TripleArray TripleArray, int i1, int i2, int i3, int *x)

if (!TripleArray.array)

return NotPresent;

if (i1 < 0 || i2 < 0 || i3 < 0 || i1 >= TripleArray.m2 || i3 >= TripleArray.m3) //越界判断

return IllegalIndex; //元素不存在

*x = *(TripleArray.array + i1*TripleArray.m2*TripleArray.m3 + i2*TripleArray.m3 + i3); //传递地址

return OK;



//数组元素的赋值运算:判断数组是否存在,若数组不存在,函数返回ERROR,若下标合法,则将下标为 i1,i2,i3,... 的数组元素值设为x

Status StoreArrayItem(TripleArray *TripleArray, int i1, int i2, int i3, int x)

if (!TripleArray->array)

return NotPresent;

*(TripleArray->array + i1*TripleArray->m2*TripleArray->m3 + i2*TripleArray->m3 + i3)=x;

return OK;



//数乘运算:判断数组A是否存在,若不存在,则返回ERROR,否则,将数组所有元素依次输出;

void OutputArray(TripleArray TripleArray)

if (!TripleArray.array)

return ;

int i1, i2, i3;

for (i1 = 0; i1 < TripleArray.m1; i1++)

for (i2 = 0; i2 < TripleArray.m2; i2++)

for (i3 = 0; i3 < TripleArray.m3; i3++)

int value;

RetrieveArray(TripleArray, i1, i2, i3, &value);

printf("array[%d][%d][%d]=%d\n", i1, i2, i3, value);









//拷贝运算:判断A,B数组存在,若不存在,则返回ERROR,否则,判断数组A,B是否大小相同,若不同,返回ERROR,否则,将A中元素依次拷贝到数组B中;

Status CopyArray(TripleArray *TripleArrayA, TripleArray *TripleArrayB)

if (!TripleArrayA->array || !TripleArrayB->array)

return NotPresent;

if (TripleArrayA->array == TripleArrayB->array)

return Duplicate;

if (TripleArrayA->m1 != TripleArrayB->m1 || TripleArrayA->m2 != TripleArrayB->m2 || TripleArrayA->m3 != TripleArrayB->m3)

return ERROR;

int i1, i2, i3;

for (i1 = 0; i1 < TripleArrayA->m1; i1++)

for (i2 = 0; i2 < TripleArrayA->m2; i2++)

for (i3 = 0; i3 < TripleArrayA->m3; i3++)

int value;

RetrieveArray(*TripleArrayA, i1, i2, i3, &value);

StoreArrayItem(TripleArrayB, i1, i2, i3, value);







return OK;



int main()

TripleArray TripleArrayA, TripleArrayB;

CreateArray(&TripleArrayA, 2, 2, 2);

CreateArray(&TripleArrayB, 2, 2, 2);

int i1, i2, i3;

for (i1 = 0; i1 < TripleArrayA.m1; i1++)

for (i2 = 0; i2 < TripleArrayA.m2; i2++)

for (i3 = 0; i3 < TripleArrayA.m3; i3++)

StoreArrayItem(&TripleArrayA, i1, i2, i3, 10);

StoreArrayItem(&TripleArrayB, i1, i2, i3, 5);







printf("请输出数组TripleArrayA元素:\n");

OutputArray(TripleArrayA);

printf("请输出数组TripleArrayB元素:\n");

OutputArray(TripleArrayB);

CopyArray(&TripleArrayA, &TripleArrayB);

printf("请输出数组TripleArrayA元素:\n");

OutputArray(TripleArrayA);

printf("请输出数组TripleArrayB元素:\n");

OutputArray(TripleArrayB);

以上是关于三维数组的主要内容,如果未能解决你的问题,请参考以下文章

c++怎么用三维数组

遍历三维数组

关于三维数组转换2维数组的问题

三维数组转二维数组

2D 数组到 3D 数组的数量,扩展第三维

PHP三维数组拼装