数组定位-作业0403-(数组)

来源:互联网 发布:新手淘宝开店看什么书 编辑:程序博客网 时间:2024/05/22 02:40
这段代码是从老师那拷来的还没彻底理解。
  1. /*完成数组的初始化操作,
  2. *  从屏幕输入数组的维数和维界,
  3. *  初始化后返回成功或失败。
  4. * (可参照参考程序)*/
  5. #include <stdarg.h>
  6. #include <iostream.h>
  7. #include <malloc.h>
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #define MAX_ARRAY_DIM 8
  11. #define ERROR 0
  12. #define OVERFLOW -1
  13. #define UNDERFLOW -2
  14. #define OK 1
  15. typedef int ElemType;
  16. typedef struct
  17. {   ElemType *base;
  18.     int dim;
  19.     int *bounds;
  20.     int *constants;
  21. }Array;
  22. //数据元素定位,ap为不定长参数
  23. int Locate(Array A,va_list ap,int &off)                      
  24.     int ind,i;
  25.     off=0;
  26.     for(i=0;i<A.dim;++i)
  27.     {
  28.         ind=va_arg(ap,int);
  29.         if(ind<0||ind>=A.bounds[i]) return OVERFLOW;         
  30.         off+=A.constants[i]*ind;
  31.     }
  32.     return OK;
  33. }
  34. //数组初始化 
  35. int InitArray(Array &A,int dim,...)            
  36. {
  37.     int i,elemtotal=1;
  38.     va_list ap;
  39.     //初始化dim
  40.     if(dim<1||dim>MAX_ARRAY_DIM) return (ERROR);
  41.     A.dim=dim;
  42.     
  43.     //初始化bounds
  44.     A.bounds=(int*)malloc(dim*sizeof(int));    
  45.     if(!A.bounds) return (OVERFLOW);          
  46.     va_start(ap,dim);                           
  47.     for(i=0;i<dim;i++)
  48.     {
  49.         A.bounds[i]=va_arg(ap,int);
  50.         if(A.bounds[i]<0) return (UNDERFLOW);
  51.         elemtotal *=A.bounds[i];
  52.     }
  53.     va_end(ap);
  54.     //初始化基址base
  55.     A.base=(ElemType*)malloc(elemtotal*sizeof(ElemType));
  56.     if(!A.base) return(OVERFLOW);
  57.     //初始化常数constant
  58.     A.constants=(int*)malloc(dim*sizeof(int));          
  59.     if (!A.constants) 
  60.         return(OVERFLOW);
  61.     A.constants[dim-1]=1;                                 
  62.     for(i=dim-2;i>=0;--i)
  63.     {
  64.         A.constants[i]=A.bounds[i+1]*A.constants[i+1];
  65.     }
  66.     return elemtotal;
  67. }
  68.     
  69. void main()                           
  70. {
  71.     Array A;
  72.     int dim=2,r,i=0;                  
  73.     int off=0;
  74.     int Abound[MAX_ARRAY_DIM];        
  75.     int bound[MAX_ARRAY_DIM];
  76.     printf("Locate.cpp/n==========/n/n");
  77.     //从屏幕读取参数
  78.     printf("Please input the dimentions of array you want to create: /n");
  79.     scanf("%d",&dim);                   
  80.     printf("please assign the length of each dimention: /n");
  81.     
  82.     for(i=0;i<dim;i++)
  83.     {  
  84.         printf("Dimention%d.length= ",i+1);
  85.         scanf("%d",&bound[i]);
  86.     }
  87.     if(r=InitArray(A,dim,bound[0],bound[1],bound[2],bound[3],bound[4],bound[5],bound[6],bound[7]))//初始化数组
  88.     {
  89.         cout<<"Success to Initialize a Array!"<<endl
  90.             <<"Dimention= "<<dim<<endl<<"Total element is: "<<r<<endl<<endl;
  91.     
  92.         //从屏幕输入数据元素脚标
  93.         cout<<"Please input the array subscripts of which you want to get its offset:"<<endl
  94.             <<"Array[J1,J2,J3,...]"<<endl;
  95.             
  96.         for(i=0;i<dim;i++)              
  97.         {  
  98.             printf("J%d= ",i+1);
  99.             scanf("%d",&Abound[i]);
  100.         }
  101.         
  102.         
  103.         va_list ap;
  104.         ap=(char *)&Abound[0];
  105.         r=Locate(A,ap,off);//调用定位函数
  106.         if(r!=OVERFLOW) 
  107.             cout<<"Location in Array is:(Begin with Array[0,0,0...]) "<<off<<endl;
  108.         else
  109.             cout<<"OVERFLOW"<<endl;
  110.     }
  111. }