共有回帖数 0 个
-
以下代码是基于数据结构给出的算法之上编写的,但是程序运行后发生错误,从main函数中开始调试,发现assign没有问题,直至程序运行到Value函数中的Locate函数,...,本来在main中调用Value(A,e,0,0),那么va_arg应该取出e后面的0,但调试发现va_arg取出的是5(好像是变量 t 的当前值)代码如下
#include stdio.h
#include stdlib.h
#include malloc.h
#include stdarg.h //标准头文件,提供宏va_start、va_arg、va_end
//用于存取变长参数表
#define MAX_ARRAY_DIM 8 //假设数组维数的最大值是8
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define UNDERFLOW -3
//Status是函数的类型,其值是函数结果的状态代码
typedef int Status;
//ElemType 是数据结构的元素的类型,在没有声明的情况下,一般用整型int暂替
typedef int ElemType;
typedef struct{
ElemType *base; //数组元素基址,由InitArray分配
int dim; //数组维数
int *bounds; //数组维界基址,由InitArray分配
int *constants; //数组映像函数常量基址,由InitArray分配
}Array;
int i,j;
//-------基本操作的函数原型说明--------
Status InitArray(Array &A,int dim,...);
//若维数dim和随后的各维长度合法,则构造相应的数组A,并返回OK
Status SestroyArray(Array &A);
//销毁数组A
Status Value(Array A,ElemType &e,...);
//A是n维数组,e为元素变量,随后是n个下标值。
//若各下标不超界,则e赋值为所指定的A的元素值,并返回OK
Status Assign(Array &A,ElemType e,...);
//A是n维数组,e为元素变量,随后是n各下标值
//若各下标不超界,则将e的值赋给所指定的A的元素,并返回OK
//--------基本操作的算法描述---------
Status InitArray(Array &A,int dim,...){
//若维数dim和随后的各维长度合法,则构造相应的数组A,并返回OK
int elemtotal;//声明部分
va_list ap;
int i;
if(dim1||dimMAX_ARRAY_DIM)return ERROR;
A.dim=dim;
A.bounds=(int *)malloc(dim*sizeof(int));
if(!A.bounds)exit(OVERFLOW); //exit函数应添加头文件 stdlib.h
//若各维长度合法,则存入A.bounds,并求出A的元素总数elemtotal
elemtotal=1;
va_start(ap,dim); //ap为va_list类型,是存放变长参数表信息的数组
for(i=0;idim;++i){
A.bounds=va_arg(ap,int);
if(A.bounds0)return UNDERFLOW;
elemtotal*=A.bounds;
}
va_end(ap);
A.base=(ElemType *)malloc(elemtotal*sizeof(ElemType));
if(!A.base)exit(OVERFLOW);
//求映像函数的常数ci,并存入A.constants[i-1],i=1,...,dim
A.constants=(int *)malloc(dim*sizeof(int));
if(!A.constants)exit(OVERFLOW);
A.constants[dim-1]=1; //L=1,指针的增减以元素的大小为单位
for(i=dim-2;i=0;--i)
A.constants=A.bounds[i+1]*A.constants[i+1];
return OK;
}//InitArray
Status DestroyArray(Array &A){
//销毁数组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;
return OK;
}//DestroyArray
Status Locate(Array A,va_list ap,int &off){
//若ap指示的各下标值合法,则求出该元素在A中相对地址off
int ind,i;//声明
off=0;
for(i=0;iA.dim;++i){
ind=va_arg(ap,int);
if(ind0||ind=A.bounds)return OVERFLOW;
off+=A.constants*ind;
}
return OK;
}//Locate
Status Value(Array A,ElemType &e,...){
//A是n维数组,e为元素变量,随后是n个下标值
//若各下标不超界,则e赋值为所指定的A的元素值,并返回OK
va_list ap;
int off;//声明部分
Status result;
va_start(ap,e); ////////////////////////////////////ap[0]==5==t ???????????????
if((result=Locate(A,ap,off))=0)return result;
va_end(ap);
e=*(A.base+off);
return OK;
}//Value
Status Assign(Array &A,ElemType e,...){
//A是n维数组,e为元素变量,随后是n各下标值
//若各下标不超界,则将e的值赋给所指定的A的元素,并返回OK
va_list ap;
int off;//声明部分
Status result;
va_start(ap,e);
if((result=Locate(A,ap,off))=0)return result;
va_end(ap);
*(A.base+off)=e;
return OK;
}//Assign
void main()
{//主函数
Array A;int t=1;
ElemType e; /////////////////////int t=1;的位置有影响////////////////////////////////////////////////////////////////////
InitArray(A,2,2,2);
for(i=0;i2;i++)
for(j=0;j2;j++){
Assign(A,t,i,j);
++t;
}
for(i=0;i4;++i)
printf("%-3d",A.base);
Value(A,e,0,0);
printf("n");
printf("%d",e);
return;
}
楼主 2015-12-31 12:15 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知