详解C语言算法

来源:百度知道 编辑:UC知道 时间:2024/05/25 12:00:06
#include <stdarg.h>
#define maxdim 10
Typedef struct
{ElemType *base;
int dim;
int *bounds;
int *constans;
}Array
Satus IntialArray(Array &a,int dim,…)
{
if(dim<1‖din>maxdim) rreturn ERROR;
A.dim=dim;
A.bounds=(int*)malloc(dim*sizeof(int));
if(!A.bounds) exit(OVERFLOW);
elemtotal=1;
va_start(ap.dim);
for(i=0;i<dim;i++)
{
A.bound[i]=va_arg(ap.int);
if(A.bounds<0) return UNDERFLOW;
elemtotal*=A.bounds[i];

}
va_end(ap);
A.base=(ElemType*)malloc(elemtotal*sixeof(ElemTytpe));
if(!A.base) exit(OVERFLOW);
a.constants=(int*)malloc(dim*sizeof(int));
if(!A.constants) exit(OVERFLOW);
for(i=dim-2;i>=0;i--)
A.constants[i]=A.bounds[i+1]*A.counstants[i+1];
return OK;
}

完整的程序如下:
typedef int status;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10
#include<stdarg.h>
#include<iostream.h>
#include<stdlib.h>
typedef int ElemType;
#define MAXDIM 8
typedef struct{
ElemType * base;
int dim;
int *bounds;
int *constants;
}Array;

status InitArray(Array &A,int dim,...)
{va_list ap;
if(dim<1||dim>MAXDIM) return ERROR;
A.dim=dim;
int i;
int total=1;
A.bounds=(int *)malloc(dim*sizeof(int));
if(!A.bounds) return ERROR;
va_start(ap,dim);
for(i=0;i<dim;i++)
{A.bounds[i]=va_arg(ap,int);
if(A.bounds[i]<0) return ERROR;
total*=A.bounds[i];
}
va_end(ap);
A.base=(ElemType*)malloc(total*sizeof(ElemType));
if(!A.base) return ERROR;
A.constants=(int *)malloc(dim*sizeof(int));
if(!A.constants) return ERROR;
A.