#include<stdarg.h>#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#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 OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
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(char *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,char 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 S,T;
int i,j,k,u,m=8,n=3,*p,*q,dim=1,bound=80; /* a[80]数组 */
char e,x;
ElemType *p1,*p2;
InitArray(&S,dim,bound); /* 构造1维数组A */
p=S.bounds;
printf("S.bounds=");
for(i=0; i<dim; i++) /* 顺序输出A.bounds */
printf("%d ",*(p+i));
p=S.constants;
printf("\nS.constants=");
for(i=0; i<dim; i++) /* 顺序输出A.constants */
printf("%d ",*(p+i));
printf("请输入长度为8的主串:\n");
for(i=0; i<bound; i++)
{
scanf("%c",&x);
if(x=='\n')
break;
Assign(&S,x,i); /* 将m赋值给A[i] */
}
printf("主串:");
for(j=0; j<i; j++)
{
Value(&e,S,j); /* 将A[j]的值赋给e */
printf("%c",e); /* 输出A[j] */
}
printf("\n");
InitArray(&T,dim,bound); /* 构造1维数组B*/
q=T.bounds;
printf("T.bounds=");
for(i=0; i<dim; i++) /* 顺序输出B.bounds */
printf("%d ",*(q+i));
q=T.constants;
printf("\nt.constants=");
for(i=0; i<dim; i++) /* 顺序输出B.constants */
printf("%d ",*(q+i));
printf("请输入长度为3的子串:\n");
for(i=0; i<bound; i++)
{
scanf("%c",&x);
if(x=='\n')
break;
Assign(&T,x,i); /* 将m赋值给A[i] */
}
printf("子串:");
for(j=0; j<i; j++)
{
Value(&e,T,j); /* 将A[j]的值赋给e */
printf("%c",e); /* 输出A[j] */
}
printf("\n");
p1=S.base;
p2=T.base;
u=m;
printf("子串的位置:");
while(m>=n)
{
i=0;
j=0;
k=0;
while(*(p2+j)!='\0')
{
if(*(p1+i)==*(p2+j))
k+=1;
else
break;
i++;
j++;
}
if(k==n)
{
printf("%d\n",u-m+1);
break;
}
m--;
p1+=1;
}
if(k<n)
printf("0\n");
DestroyArray(&S);
system("PAUSE");
}
运行结果: