清华大学严蔚敏编著《数据结构(c语言版)》源程序

源代码在线查看: value.cpp

软件大小: 95 K
上传用户: lidaniel
关键词: 清华大学 数据结构 c语言 源程序
下载地址: 免注册下载 普通下载 VIP

相关代码

				//Value.cpp
				
				#include 
				#include 
				#include 
				#include 
				#include 
				
				#define MAX_ARRAY_DIM 8
				#define ERROR 0
				#define OVERFLOW -1
				#define UNDERFLOW -2
				#define OK 1
				
				typedef int ElemType;
				typedef struct
				{	ElemType *base;
					int dim;
					int *bounds;
					int *constants;
				}Array;
				
				int Locate(Array A,va_list ap,int &off)               //Locate() function
				{//To get the offset of  element in the array A
					int ind,i;
					off=0;
				    for(i=0;i					{	ind=va_arg(ap,int);
						if(ind=A.bounds[i]) return OVERFLOW;  //Illegal substript
						off+=A.constants[i]*ind;
					}//end of for
					return OK;
				}//end of Locate() function
				 
				int Assign(Array &A,ElemType e,...)                   //Assign() function
				{
					va_list ap;
					int result,off=0;
					va_start(ap,e);
					if((result=Locate(A,ap,off))					*(A.base+off)=e;
					va_end(ap);
					return OK;
				}//end of Assign() function
				
				int Value(Array A,ElemType e,...)                     //Value() function
				{
					int result,off=0;
					va_list ap;
					va_start(ap,e);
					if((result=Locate(A,ap,off))					e=*(A.base+off);
					va_end(ap);
					return e;                                   //that is OK;
				}//end of Value() function
				
				int InitArray(Array &A,int dim,...)             //InitArray() function 
				{
					int i,elemtotal=1;                          //Initialize total element of A 
					va_list ap;
					if(dimMAX_ARRAY_DIM) return (ERROR);//if dimention is illegal then return ERROR;
					A.dim=dim;                                  //set dimention of array
					A.bounds=(int*)malloc(dim*sizeof(int));     //allocate space for array A.bounds
				    if(!A.bounds) return (OVERFLOW);            //failure to allocate
					va_start(ap,dim);                           //ap are the type of va_list
					for(i=0;i					{A.bounds[i]=va_arg(ap,int);
					 if(A.bounds[i]					 elemtotal *=A.bounds[i];
					}//end of if
				
					va_end(ap);
					A.base=(ElemType*)malloc(elemtotal*sizeof(ElemType));//allocating space to A.base
					if(!A.base) return(OVERFLOW);
					
					A.constants=(int*)malloc(dim*sizeof(int));           //allocating space to A.constants
					if (!A.constants) return(OVERFLOW);
					A.constants[dim-1]=1;                                 
				
					for(i=dim-2;i>=0;--i)                                //get the const Ci and save into A.constants[i-i],i=1,..,dim
						A.constants[i]=A.bounds[i+1]*A.constants[i+1];
					return elemtotal;
				}//end of InitArray() function
				
					
				void main()                                               //main() function
				{
					Array A;
					int dim=2,r,i=0;                                      //default dimention is 2;
					int off=0,e=50,GetValue;                              //default Assign values=50;
				
					int bound[MAX_ARRAY_DIM];
					int Assbound[MAX_ARRAY_DIM];
					int GetVal[MAX_ARRAY_DIM];
					printf("Value.cpp\n=========\n\n");
					printf("Please input the dimentions of array you want to create:  \n");
					scanf("%d",&dim);                   
					printf("please assign the length of each dimention: \n\n");
					
					for(i=0;i						{   printf("Dimention%d.length= ",i+1);
						    scanf("%d",&bound[i]);
							
						}//end of for
				
					if(r=InitArray(A,dim,bound[0],bound[1],bound[2],bound[3],bound[4],bound[5],bound[6],bound[7]))
					{
						cout						    				        printf("Now begin to Assign!\n");
						printf("Assign values = ");
						scanf("%d",&e) ;
					    cout													//default: Abound[0]=0;Abound[1]=4;Abound[2]=1;Abound[3]=0;Abound[4]=1;Abound[5]=1;Abound[6]=0;Abound[7]=1;
						for(i=0;i						{   printf("J%d= ",i+1);
						    scanf("%d",&Assbound[i]);
						}//end of for  
				
						if(Assign(A,e,Assbound[0],Assbound[1], Assbound[2], Assbound[3], Assbound[4], Assbound[5], Assbound[6], Assbound[7])==OK)
							 cout						else cout				
				
				        cout						cout													//default: Abound[0]=0;Abound[1]=4;Abound[2]=1;Abound[3]=0;Abound[4]=1;Abound[5]=1;Abound[6]=0;Abound[7]=1;
						for(i=0;i						{   printf("J%d= ",i+1);
						    scanf("%d",&GetVal[i]);
						}//end of for  
				
						r=Value(A,GetValue,GetVal[0],GetVal[1], GetVal[2],GetVal[3],GetVal[4], GetVal[5], GetVal[6],GetVal[7]);
						if(r!=OVERFLOW&&r!=UNDERFLOW&&r!=ERROR)
					 		 cout							     						         								 				        else cout					}//end of if
				     getch();
				}//end of main() function
				
				
				
							

相关资源