数组顺序存储。
很好,这本书成功的激起了我的兴趣,坚持。。。。
//数组的顺序存储(三元组)
#include<stdio.h>
#include<stdarg.h>
#include<stdlib.h>
#define OK 1
#define ERROR -1
#define OVERFLOW -1
#define MAX_ARRAY_DIM 8
typedef int Status; //这里有分号
typedef struct{ //typedef struct这个地方c语言知识要重新看一下,忘记了
int *base; //数组元素基址
int dim; //维数
int *bounds; //数组维界基址
int *constants; //数组映像函数基地址
}Array;
//省略号后面是各维度
Status InitArray(Array *A,int dim,...);
Status DestoryArray(Array *A);
//用数组元素赋值给e的函数,省略号后面为n个下标值
Status ValueArray(Array A, int *e, ...);
//用e给数组元素赋值的函数,省略号后面为n个下标值
Status AssignArray(Array *A,int e,...);
Status InitArray(Array *A,int dim,...)
{
va_list ap;
int i,elemtotal = 1;
//if(dim <= MAX_ARRAY_DIM ) //各维度长度合法还没有限制
if(dim < 1 || dim > MAX_ARRAY_DIM) return ERROR;
A->dim = dim;
A->bounds = (int *)malloc(dim * sizeof(int)); //这里是A->bounds而不是bounds 是dim * sizeof 而不是sizeof
//漏写了
if( !A->bounds ) exit(OVERFLOW);
va_start(ap,dim);
//for(; i != -1; i = va_arg(ap,int),bounds++ )
//*bounds = i;
//后面都漏写了
for(i = 0;i<dim;i++)
{
A->bounds[i] = va_arg(ap,int); //. 和 ->的区别 a->b 的含义是 (*a).b
if(A->bounds[i] < 0) return OVERFLOW;
elemtotal *= A->bounds[i]; //A的元素总数
}
va_end(ap);
A->base = (int *)malloc(elemtotal * sizeof(int)); //size of(是数据类型占的空间大小)与strlen(求字符串的长度)的区别
if(!A->base) exit(OVERFLOW);
//不明白,理解有点点小费劲
A->constants = (int*)malloc(dim*sizeof(int));
if(!A->constants) exit(OVERFLOW);
A->constants[dim -1] = 1;
//constants[0] = b[1] * constants[1];
for(i = dim -2;i>=0;--i)
A->constants[i] = A->bounds[i+1] * A->constants[i+1];
return OK;
}
//求出ap指示的值在数组A中相对位置
Status LocateArray(Array A,va_list ap,int * off)
{
int i,ind;
*off = 0;
for(i = 0;i<A.dim;i++)
{
ind = va_arg(ap,int);
if(ind<0 || ind>=A.bounds[i])
return OVERFLOW;
*off += A.constants[i]*ind; //某个维度的单位元素个数*需要跨过的单位
}
return OK;
}
Status AssignArray(Array *A,int e,...)
{
va_list ap;
Status result;
int off;
va_start(ap,e);
result = LocateArray(*A,ap,&off);
if(result == OVERFLOW)
return result;
*(A->base+off) = e;
return OK;
}
Status ValueArray(Array A,int *e,...)
{
va_list ap;
Status result;
int off;
va_start(ap,*e);
result = LocateArray(A,ap,&off);
if(result == OVERFLOW)
return result;
*e = *(A.base + off);
return OK;
}
Status DestoryArray(Array *A)
{
if(!A->base)
return ERROR;
free(A->base);
A->base = NULL;
if(!A->bounds)
return ERROR;
free(A->bounds);
A->bounds = NULL;
if(!A->constants)
return ERROR;
free(A->constants);
A->constants = NULL;
A->dim = 0;
return OK;
}
void ArrayPrint(Array A)
{
int i,j;
for(i = 0,j = 1;i<A.dim;i++)
j *= A.bounds[i];
for(i = 0;i<j;i++)
printf("%d ",A.base[i]);
}
int main(int argc, char const *argv[])
{
Array A;
InitArray(&A,2,2,3);
printf("AssignArray test!\n");
{
int i , j;
int e = 4;
for(i = 0;i<A.bounds[0];i++)
{
for(j = 0;j<A.bounds[1];j++)
{
printf("赋值:A[%d][%d] = %d\n", i,j,++e);
AssignArray(&A,e,i,j);
}
}
}
ArrayPrint(A);
printf("LocateArray test && ValueArray test!\n");
{
int e;
printf("获取A中下标为(1,1)的元素的值,Locate用于求出A[1][1]的相对位置。。。\n");
ValueArray(A,&e,1,1);
printf("A[1][1] = %d\n",e);
printf("\n");
}
printf("DestoryArray test!\n");
{
printf("destory before: ");
A.dim != 0 ? printf("A exists!\n") : printf("A not exists!\n");
DestoryArray(&A);
printf("after Destory: ");
A.dim != 0 ? printf("A exists!\n") : printf("A not exists!\n");
printf("\n");
}
return 0;
}