读简单的源码1-6.c
最近还是经常读gawk1.01,grep的代码,感觉grep的代码还是比较好读,毕竟比较简单,如果结合《精通正则表达式》来读,就基本上能理解了。
但gawk就复杂了,要懂bison,如果不懂这个,你C语言来通,还是不能串起来。所以要补充编译原理的知识。
最近还想读linux0.11的源码。感觉还是要把汇编搞通,才是。
于是,手头就找些简单的代码来读。现在感觉,如果不看到代码,心里就有些慌。使用软件时,也希望能是开源的。这样,就更有控制的感觉。
今天读了一段代码。感觉很是难懂。关键是其中:
typedef struct
{
ElemType *base; /* 数组元素基址,由InitArray分配 */
int dim; /* 数组维数 */
int *bounds; /* 数组维界基址,由InitArray分配 */
int *constants; /* 数组映象函数常量基址,由InitArray分配 */
}Array;
对constants总是不能理解。作者给它赋值的过程也很怪。
(*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;
作者实现了数组的操作,感觉写得还是很流畅的。作者的演示程序是三维数组,我改成了二维数组。当然先要编译后执行一次。结果如下:
./a.out
A.bounds=3 4
A.constants=4 1
3?40о?:
A[0][0]= 0
A[0][1]= 1
A[0][2]= 2
A[0][3]= 3
A[1][0]=10
A[1][1]=11
A[1][2]=12
A[1][3]=13
A[2][0]=20
A[2][1]=21
A[2][2]=22
A[2][3]=23
A.base=
0 1 2 3
10 11 12 13
20 21 22 23
----
4,
1,
读代码一定要编译,调试,因此,除非你能在头脑中很好的模拟代码的执行,否则,读代码真是要在电脑上读。读别人的代码,感觉自己的水平真是差呀。
现在把代码放后面。
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<math.h> /* floor(),ceil(),abs() */
#include<stdarg.h>
#define OK 1
#define ERROR 0
#define MAX_ARRAY_DIM 8
#include<stdlib.h>
#define OVERFLOW 88888
#define UNDERFLOW 999999
typedef int ElemType;
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
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,...)
{ /* ...依次为各维的下标值,若各下标合法,则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]数组 */
int i,j,k,*p,dim=2,bound1=3,bound2=4; /* a[3][4][2]数组 */
ElemType e,*p1;
// InitArray(&A,dim,bound1,bound2,bound3); /* 构造3*4*2的3维数组A */
InitArray(&A,dim,bound1,bound2); /* 构造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);
printf("\n%d页%d行%d列矩阵元素如下:\n",bound1,bound2);
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] */
Assign(&A,i*10+j*1,i,j); /* 将i*100+j*10+k赋值给A[i][j][k] */
Value(&e,A,i,j); /* 将A[i][j][k]的值赋给e */
printf("A[%d][%d]=%2d ",i,j,e); /* 输出A[i][j][k] */
}
printf("\n");
}
printf("\n");
}
p1=A.base;
printf("A.base=\n");
for(i=0;i<bound1*bound2;i++) /* 顺序输出A.base */
{
printf("%4d",*(p1+i));
if(i%(bound2)==bound2-1)
printf("\n");
}
printf("\n----\n");
for(i=0;i<A.dim;i++){
printf("%d,\n",A.constants[i]);
}
DestroyArray(&A);
}