#include <iostream>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define MAX_ARRAY_DIM 8
using namespace std;
typedef int ElemType;
int elemtotal=1;
struct array
{
ElemType *base; //数组地址
int dim; //数组维数
int *bounds; //数组维界基址
int *constants; //映像函数
};
int initarray(array *A,int dim, ...) //初始化一个多维数组
{
va_list ap;
if(dim<1||dim>MAX_ARRAY_DIM)
return ERROR;
A->dim=dim;
A->bounds=(int *)malloc(sizeof(int)*dim);
if(!A->bounds)
return ERROR;
va_start(ap,dim);
for(int i=0;i<dim;i++)
{
A->bounds[i]=va_arg(ap,int);
if(A->bounds[i]<0)
return ERROR;
elemtotal*=A->bounds[i];
}
va_end(ap);
A->base=(ElemType *)malloc(elemtotal+15*sizeof(ElemType));
if(!A->base)
return ERROR;
A->constants=(int *)malloc(dim*sizeof(int));
if(!A->constants)
return ERROR;
A->constants[dim-1]=1;
for(int i=dim-2;i>=0;i--)
A->constants[i]=A->bounds[i+1]*A->constants[i+1];
return OK;
}
void destroyarray(array *A) //释放空间
{
if(A->base)
free(A->base);
if(A->bounds)
free(A->bounds);
if(A->constants)
free(A->constants);
A->base=A->bounds=A->constants=NULL;
A->dim=0;
}
int locate(array *A,va_list ap,int *off) //求位置
{
int ind;
int x=0;
for(int i=0;i<A->dim;i++)
{
ind=va_arg(ap,int);
if(ind<0||ind>=A->bounds[i])
return ERROR;
x+=A->constants[i]*ind;
}
*off=x;
return OK;
}
int value(ElemType *e,array *A, ...) //赋值e
{
va_list ap;
int off;
va_start(ap,A);
if(locate(A,ap,&off)==ERROR)
return ERROR;
*e=*(A->base+off);
return OK;
}
int assign(array *A,ElemType e, ...)
{
va_list ap;
int off;
va_start(ap,e);
if(locate(A,ap,&off)==ERROR)
return ERROR;
*(A->base+off)=e;
return OK;
}
int getarray(array *A,ElemType e, ...)
{
va_list ap;
int off;
va_start(ap,e);
if(locate(A,ap,&off)==ERROR)
return ERROR;
for(int i=elemtotal-1;i>=off;i--)
{
*(A->base+i+1)=*(A->base+i);
}
*(A->base+off)=e;
return OK;
}
int delarray(array *A,ElemType *e, ...)
{
va_list ap;
int off;
va_start(ap,e);
if(locate(A,ap,&off)==ERROR)
return ERROR;
*e=*(A->base+off);
for(int i=off+1;i<=elemtotal;i++)
{
*(A->base+i-1)=*(A->base+i);
}
return OK;
}
int main()
{
array A;
int *p,dim=3,bound1=3,bound2=4,bound3=2;
ElemType e,*p1;
initarray(&A,dim,bound1,bound2,bound3);
p=A.bounds;
printf("A->bounds=");
for(int i=0;i<dim;i++)
{
printf("%d ",*(p+i));
}
p=A.constants;
printf("\nA->constants=");
for(int i=0;i<dim;i++)
{
printf("%d ",*(p+i));
}
printf("\n%d页%d行%d列矩阵元素如下:\n",bound1,bound2,bound3);
for(int i=0;i<bound1;i++)
{
for(int j=0;j<bound2;j++)
{
for(int k=0;k<bound3;k++)
{
assign(&A,i*100+j*10+k,i,j,k);
value(&e,&A,i,j,k);
printf("A[%d][%d][%d]=%2d ",i,j,k,e);
}
printf("\n");
}
printf("\n");
}
p1=A.base;
printf("A->base=\n");
for(int i=0;i<bound1*bound2*bound3;i++)
{
printf("%4d",*(p1+i));
if(i%(bound2*bound3)==bound2*bound3-1)
printf("\n");
}
getarray(&A,999,1,1,1);//插入
printf("插入一个数之后的数组:\n");
for(int i=0;i<bound1*bound2*bound3+1;i++)
{
printf("%4d",*(p1+i));
if(i%(bound2*bound3)==bound2*bound3-1)
printf("\n");
}
printf("\n");
printf("删除一个元素之后的数组:\n");
int ee;
delarray(&A,&ee,1,1,1);
for(int i=0;i<bound1*bound2*bound3;i++)
{
printf("%4d",*(p1+i));
if(i%(bound2*bound3)==bound2*bound3-1)
printf("\n");
}
printf("删除的数为:%d\n",ee);
printf("A->dim=%d\n",A.dim);
destroyarray(&A);
return 0;
}
数据结构_数组
最新推荐文章于 2023-01-16 22:21:23 发布