二维数组:二维数组的实现方法



本课主题: 实验 5 实验
教学目: 掌握 2维实现思路方法
教学重点:  2维存储表示, 2维基本操作
教学难点:  2维基本操作
授课内容:
顺序存储表示和实现:
#<stdarg.h>
# MAX_ARRAY_DIM 8
typedef struct {
ElemType *base;
 dim;
 *bounds;
 *constants;
}Array;
Status InitArray(Array &A, dim,...);
Status DestroyArray(Array &A);
Status Value(Array A,ElemType &e,...);
Status Assign(Array &A,ElemType e,...);
基本操作算法描述:
Status InitArray(Array &A, dim,...){
(dim<1||dim>MAX_ARRAY_DIM)  ERROR;
A.dim=dim;
A.bounds=( *)malloc(dim *());
(!A.bounds) exit(OVERFLOW);
elemtotal=1;
va_start(ap,dim);
for(i=1;i<dim;i){
A.bounds[i]=va_arg(ap,);
(A.bounds[i]<0)  UNDERFLOW;
elemtotal*=A.bounds[i];
}
va_end(ap);
A.base=(ElemType *)malloc(elemtotal*(ElemType));
(!A.base) exit(OVERFLOW);
A.constants=( *)malloc(dim*());
(!A.constants) exit(OVERFLOW);
A.constants[dim-1]=1;
for(i=dim-2;i>=0;--i)
A.constants[i]=A.bounds[i+1]*A.constants[i+1];
 OK;
}
Status DestoyArray(Array &A){
(!A.base)  ERROR;
free(A.base); A.base=NULL;
 !(A.bounds)  ERROR;
free(A.bounds); A.bounds=NULL;
!(A.constatns)  ERROR;
free(A.constants); A.constants=NULL;
 OK;
}
Status Locate(Array A,va_list ap, &off){
off=0;
for(i=0;i<A.dim;i){
ind=va_arg(ap,);
(ind<0||ind>=A.bounds[i])  OVERFLOW;
offA.constants[i]*ind;
}
 OK;
}
Status Value(Array A,ElemType &e,...){
va_start(ap,e);
((result=Locate(A,ap,off))<=0  result;
e=*(A.base+off);
 OK;
}
Status Assign(Array &A,ElemType e,...){
va_start(ap,e);
((result=Locate(A,ap,off))<=0)  result;
*(A.base+off)=e;
 OK;
}
Tags:  vb二维数组 js二维数组 java二维数组 二维数组

延伸阅读

最新评论

发表评论