#include<stdarg.h>
?#include<malloc.h> /* malloc()等 */
?#include<stdio.h> /* EOF(=^Z或F6),NULL */
?#include<stdlib.h> /* atoi() */
?#include<io.h> /* eof() */
?#include<math.h> /* floor(),ceil(),abs() */
?#include<process.h> /* exit() */
?/* 函數結果狀態代碼 */
?#define TRUE 1
?#define FALSE 0
?#define OK 1
?#define ERROR 0
?#define INFEASIBLE -1
?/* #define OVERFLOW -2 因為在math.h中已定義OVERFLOW的值為3,故去掉此行 */
?typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */
?typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */
?typedef int ElemType;
?#define MAX_ARRAY_DIM 8 /* 假設數組維數的最大值為8 */
?typedef struct
?{
? ?ElemType *base; /* 數組元素基址,由InitArray分配 */
? ?int dim; /* 數組維數 */
? ?int *bounds; /* 數組維界基址,由InitArray分配 */
? ?int *constants; /* 數組映象函數常量基址,由InitArray分配 */
?}Array;
?Status InitArray(Array *A,int dim,...)
?{ /* 若維數dim和各維長度合法,則構造相應的數組A,并返回OK */
? ?int elemtotal=1,i; /* elemtotal是元素總值 */
? ?va_list ap;
? ?if(dim<1||dim>MAX_ARRAY_DIM)
? ? ?return ERROR;
? ?(*A).dim=dim;
? ?(*A).bounds=(int *)malloc(dim*sizeof(int));
? ?if(!(*A).bounds)
? ? ?exit(OVERFLOW);
? ?va_start(ap,dim);
? ?for(i=0;i<dim;++i)
? ?{
? ? ?(*A).bounds[i]=va_arg(ap,int);
? ? ?if((*A).bounds[i]<0)
? ? ? ?return UNDERFLOW; /* 在math.h中定義為4 */
? ? ?elemtotal*=(*A).bounds[i];
? ?}
? ?va_end(ap);
? ?(*A).base=(ElemType *)malloc(elemtotal*sizeof(ElemType));
? ?if(!(*A).base)
? ? ?exit(OVERFLOW);
? ?(*A).constants=(int *)malloc(dim*sizeof(int));
? ?if(!(*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];
? ?return OK;
?}
?Status DestroyArray(Array *A)
?{ /* 銷毀數組A */
? ?if((*A).base)
? ?{
? ? ?free((*A).base);
? ? ?(*A).base=NULL;
? ?}
? ?else
? ? ?return ERROR;
? ?if((*A).bounds)
? ?{
? ? ?free((*A).bounds);
? ? ?(*A).bounds=NULL;
? ?}
? ?else
? ? ?return ERROR;
? ?if((*A).constants)
? ?{
? ? ?free((*A).constants);
? ? ?(*A).constants=NULL;
? ?}
? ?else
? ? ?return ERROR;
? ?return OK;
?}
?Status Locate(Array A,va_list ap,int *off) /* Value()、Assign()調用此函數 */
?{ /* 若ap指示的各下標值合法,則求出該元素在A中的相對地址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 Value(ElemType *e,Array A,...) /* 在VC++中,...之前的形參不能是引用類型 */
?{ /* ...依次為各維的下標值,若各下標合法,則e被賦值為A的相應的元素值 */
? ?va_list ap;
? ?Status result;
? ?int off;
? ?va_start(ap,A);
? ?if((result=Locate(A,ap,&off))==OVERFLOW) /* 調用Locate() */
? ? ?return result;
? ?*e=*(A.base+off);
? ?return OK;
?}
?Status Assign(Array *A,ElemType e,...)
?{ /* ...依次為各維的下標值,若各下標合法,則將e的值賦給A的指定的元素 */
? ?va_list ap;
? ?Status result;
? ?int off;
? ?va_start(ap,e);
? ?if((result=Locate(*A,ap,&off))==OVERFLOW) /* 調用Locate() */
? ? ?return result;
? ?*((*A).base+off)=e;
? ?return OK;
?}
?void main()
?{
? ?Array A;
? ?int i,j,k,*p,dim=3,bound1=3,bound2=4,bound3=2; /* a[3][4][2]數組 */
? ?ElemType e,*p1;
? ?InitArray(&A,dim,bound1,bound2,bound3); /* 構造3*4*2的3維數組A */
? ?p=A.bounds;
? ?printf("A.bounds=");
? ?for(i=0;i<dim;i++) /* 順序輸出A.bounds */
? ? ?printf("%d ",*(p+i));
? ?p=A.constants;
? ?printf("\nA.constants=");
? ?for(i=0;i<dim;i++) /* 順序輸出A.constants */
? ? ?printf("%d ",*(p+i));
? ?printf("\n%d頁%d行%d列矩陣元素如下:\n",bound1,bound2,bound3);
? ?for(i=0;i<bound1;i++)
? ?{
? ? ?for(j=0;j<bound2;j++)
? ? ?{
? ? ? ?for(k=0;k<bound3;k++)
? ? ? ?{
? ? ? ? ?Assign(&A,i*100+j*10+k,i,j,k); /* 將i*100+j*10+k賦值給A[i][j][k] */
? ? ? ? ?Value(&e,A,i,j,k); /* 將A[i][j][k]的值賦給e */
? ? ? ? ?printf("A[%d][%d][%d]=%2d ",i,j,k,e); /* 輸出A[i][j][k] */
? ? ? ?}
? ? ? ?printf("\n");
? ? ?}
? ? ?printf("\n");
? ?}
? ?p1=A.base;
? ?printf("A.base=\n");
? ?for(i=0;i<bound1*bound2*bound3;i++) /* 順序輸出A.base */
? ?{
? ? ?printf("%4d",*(p1+i));
? ? ?if(i%(bound2*bound3)==bound2*bound3-1)
? ? ? ?printf("\n");
? ?}
? ?DestroyArray(&A);
? ?system("PAUSE");
?}
?
?#include<malloc.h> /* malloc()等 */
?#include<stdio.h> /* EOF(=^Z或F6),NULL */
?#include<stdlib.h> /* atoi() */
?#include<io.h> /* eof() */
?#include<math.h> /* floor(),ceil(),abs() */
?#include<process.h> /* exit() */
?/* 函數結果狀態代碼 */
?#define TRUE 1
?#define FALSE 0
?#define OK 1
?#define ERROR 0
?#define INFEASIBLE -1
?/* #define OVERFLOW -2 因為在math.h中已定義OVERFLOW的值為3,故去掉此行 */
?typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */
?typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */
?typedef int ElemType;
?#define MAX_ARRAY_DIM 8 /* 假設數組維數的最大值為8 */
?typedef struct
?{
? ?ElemType *base; /* 數組元素基址,由InitArray分配 */
? ?int dim; /* 數組維數 */
? ?int *bounds; /* 數組維界基址,由InitArray分配 */
? ?int *constants; /* 數組映象函數常量基址,由InitArray分配 */
?}Array;
?Status InitArray(Array *A,int dim,...)
?{ /* 若維數dim和各維長度合法,則構造相應的數組A,并返回OK */
? ?int elemtotal=1,i; /* elemtotal是元素總值 */
? ?va_list ap;
? ?if(dim<1||dim>MAX_ARRAY_DIM)
? ? ?return ERROR;
? ?(*A).dim=dim;
? ?(*A).bounds=(int *)malloc(dim*sizeof(int));
? ?if(!(*A).bounds)
? ? ?exit(OVERFLOW);
? ?va_start(ap,dim);
? ?for(i=0;i<dim;++i)
? ?{
? ? ?(*A).bounds[i]=va_arg(ap,int);
? ? ?if((*A).bounds[i]<0)
? ? ? ?return UNDERFLOW; /* 在math.h中定義為4 */
? ? ?elemtotal*=(*A).bounds[i];
? ?}
? ?va_end(ap);
? ?(*A).base=(ElemType *)malloc(elemtotal*sizeof(ElemType));
? ?if(!(*A).base)
? ? ?exit(OVERFLOW);
? ?(*A).constants=(int *)malloc(dim*sizeof(int));
? ?if(!(*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];
? ?return OK;
?}
?Status DestroyArray(Array *A)
?{ /* 銷毀數組A */
? ?if((*A).base)
? ?{
? ? ?free((*A).base);
? ? ?(*A).base=NULL;
? ?}
? ?else
? ? ?return ERROR;
? ?if((*A).bounds)
? ?{
? ? ?free((*A).bounds);
? ? ?(*A).bounds=NULL;
? ?}
? ?else
? ? ?return ERROR;
? ?if((*A).constants)
? ?{
? ? ?free((*A).constants);
? ? ?(*A).constants=NULL;
? ?}
? ?else
? ? ?return ERROR;
? ?return OK;
?}
?Status Locate(Array A,va_list ap,int *off) /* Value()、Assign()調用此函數 */
?{ /* 若ap指示的各下標值合法,則求出該元素在A中的相對地址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 Value(ElemType *e,Array A,...) /* 在VC++中,...之前的形參不能是引用類型 */
?{ /* ...依次為各維的下標值,若各下標合法,則e被賦值為A的相應的元素值 */
? ?va_list ap;
? ?Status result;
? ?int off;
? ?va_start(ap,A);
? ?if((result=Locate(A,ap,&off))==OVERFLOW) /* 調用Locate() */
? ? ?return result;
? ?*e=*(A.base+off);
? ?return OK;
?}
?Status Assign(Array *A,ElemType e,...)
?{ /* ...依次為各維的下標值,若各下標合法,則將e的值賦給A的指定的元素 */
? ?va_list ap;
? ?Status result;
? ?int off;
? ?va_start(ap,e);
? ?if((result=Locate(*A,ap,&off))==OVERFLOW) /* 調用Locate() */
? ? ?return result;
? ?*((*A).base+off)=e;
? ?return OK;
?}
?void main()
?{
? ?Array A;
? ?int i,j,k,*p,dim=3,bound1=3,bound2=4,bound3=2; /* a[3][4][2]數組 */
? ?ElemType e,*p1;
? ?InitArray(&A,dim,bound1,bound2,bound3); /* 構造3*4*2的3維數組A */
? ?p=A.bounds;
? ?printf("A.bounds=");
? ?for(i=0;i<dim;i++) /* 順序輸出A.bounds */
? ? ?printf("%d ",*(p+i));
? ?p=A.constants;
? ?printf("\nA.constants=");
? ?for(i=0;i<dim;i++) /* 順序輸出A.constants */
? ? ?printf("%d ",*(p+i));
? ?printf("\n%d頁%d行%d列矩陣元素如下:\n",bound1,bound2,bound3);
? ?for(i=0;i<bound1;i++)
? ?{
? ? ?for(j=0;j<bound2;j++)
? ? ?{
? ? ? ?for(k=0;k<bound3;k++)
? ? ? ?{
? ? ? ? ?Assign(&A,i*100+j*10+k,i,j,k); /* 將i*100+j*10+k賦值給A[i][j][k] */
? ? ? ? ?Value(&e,A,i,j,k); /* 將A[i][j][k]的值賦給e */
? ? ? ? ?printf("A[%d][%d][%d]=%2d ",i,j,k,e); /* 輸出A[i][j][k] */
? ? ? ?}
? ? ? ?printf("\n");
? ? ?}
? ? ?printf("\n");
? ?}
? ?p1=A.base;
? ?printf("A.base=\n");
? ?for(i=0;i<bound1*bound2*bound3;i++) /* 順序輸出A.base */
? ?{
? ? ?printf("%4d",*(p1+i));
? ? ?if(i%(bound2*bound3)==bound2*bound3-1)
? ? ? ?printf("\n");
? ?}
? ?DestroyArray(&A);
? ?system("PAUSE");
?}
?