数组的实现

#include<iostream>
#include<stdarg.h>
using namespace std;
#define MAXDIM  8   //定义最大维度为8维
   typedef class Arrays
        {
        public:
            int dim;//表示维度
            int *base;//表示元素的基地址
            int *bounds; //表示数组的维界基址 (就是每一维度有多少个元素)
            int *constants;//数组映像函数常量基址 (就是从左至右这一维的下面几维一共有多少个元素)
        }*Array,arry;

 void init_arry(Array &L) ; //初始数组
 void creat_arry(Array &L,int dim,...) ;//创建一个数组
 void destroy_arry(Array &L) ;  // 销毁数组
 int locate_arry(Array &L,va_list ap,int &e); //将ap里面的元素所在的位置赋给 e
 void get_value(int &e,Array &L,...);//将小标对应的值保存在 e里面
 void fu_value(int e,Array &L...); //将e 赋给指定的元素
 void printlist(Array &L); //输出这个四维矩阵

    int main()
        {
            cout<<"构建一个四维的数组"<<endl;
            int dim; //维度
            cout<<"请输入维度"<<endl;
            cin>>dim;
            int bounds1,bounds2,bounds3,bounds4;//分别代表没一维的长度
            cout<<"请分别输入每一维的长度"<<endl;
            cin>>bounds1>>bounds2>>bounds3>>bounds4;
            Array L;
            init_arry(L);
            creat_arry(L,dim,bounds1,bounds2,bounds3,bounds4);
            for(int i=0;i<bounds1*bounds2*bounds3*bounds4;i++)
            {
                L->base[i]=i;

            }
            printlist(L);

            int e;
            int a[5];
            cout<<"请输入你想获取的下标所对应的值"<<endl;
            cin>>a[1]>>a[2]>>a[3]>>a[4];
            get_value(e,L,a[1],a[2],a[3],a[4]);
            if(e==-1)
            {
                cout<<"下标错误呦"<<endl;
            }
            else
            {
                cout<<"这个值是"<<e<<endl;

            }

            cout<<"请输入一个值"<<endl;
            cin>>a[0];
            cout<<"请输入你想赋值给的下标的元素"<<endl;
            cin>>a[1]>>a[2]>>a[3]>>a[4];
            fu_value(a[0],L,a[1],a[2],a[3],a[4]);
            cout<<endl;
            printlist(L);
            destroy_arry(L);
            printlist(L);
        }

    void init_arry(Array &L)  //初始数组
        {
            L=new arry;
            L=NULL;
        }

    void creat_arry(Array &L,int dim,...)    //创建一个数组
        {

            if(L!=NULL)
            {
                cout<<"不用初始化辽 已经初始化完了"<<endl;
                return ;

            }
            L=new arry;
            if(dim>MAXDIM)
            {
                cout<<"您输入的维度过多傲"<<endl;
                return ;
            }

            else
            {
                int number=1;  //记录这个数组总共有多少个值
                va_list lists;
                va_start(lists,dim);
                L->dim=dim;
                L->bounds=new int[dim];
                for(int i=0;i<L->dim;i++)

                    {
                        L->bounds[i]=va_arg(lists,int);
                        number=number*L->bounds[i];
                    }

                va_end(lists);
                L->constants=new int[dim];
                L->constants[dim-1]=1;
                for(int i=dim-2;i>=0;i--)
                {
                    L->constants[i]=L->constants[i+1]*L->bounds[i+1];
                }

                L->base=new int[number];   //存储多维数组其实就是存储一位数组


            }
        }



        void destroy_arry(Array &L)   // 销毁数组
            {
                if(L==NULL)
                {
                    cout<<"还没有数据傲,不需要销毁"<<endl;
                    return ;
                }

                else
                {
                   delete L->base;
                   delete L->bounds;
                   delete L->constants;
                   delete L;
                   L=NULL;
                }
            }



        int locate_arry(Array &L,va_list ap,int &e) //将ap里面的元素所在的位置赋给 e
            {
                if(L==NULL)
                {
                    cout<<"数组还没有初始化呦"<<endl;
                    return -1;
                }
                int position=0;
                int temp;
                for(int i=0;i<L->dim;i++)
                {
                    temp=va_arg(ap,int);
                    if(temp>L->bounds[i])
                    {
                        e=-1;
                        return e;
                    }
                    position+=temp*L->constants[i];

                }
                e=position;

                return position;
            }


         void get_value(int &e,Array &L,...)  //将小标对应的值保存在 e里面
            {
                if(L==NULL)
                {
                    return ;
                }

                else
                {
                    int result_position;
                    va_list ap;
                    va_start(ap,L);
                    if(-1==locate_arry(L,ap,result_position))  //此时  result_position=1
                    {

                        return ;
                    }

                    else
                    {
                        e=L->base[result_position];//  将目标值赋给e result_position为对应的下标
                    }
                }
            }


            void fu_value(int e,Array &L...) //将e 赋给指定的元素
                {
                    if(L==NULL)
                        {
                            return ;
                        }

                    else
                        {
                            int t;
                            va_list ap;
                            va_start(ap,L);
                            if(locate_arry(L,ap,t)==-1)
                            {
                                cout<<"下标错误傲"<<endl;
                                e=-1;
                                return ;
                            }
                            else
                            {
                               L->base[t]=e;  //将对应的下标元素所对应的值赋给 e
                            }

                        }
                }

        void printlist(Array &L)
            {
                    if(L==NULL)
                    {
                        cout<<"还没有初始化呦"<<endl;
                        return ;
                    }
                  int a1,a2,a3,a4;
                  int counts=0;
                   for(a1=0;a1<L->bounds[0];a1++)
                     {
                       for(a2=0;a2<L->bounds[1];a2++)
                       {
                           for(a3=0;a3<L->bounds[2];a3++)
                           {
                               for(a4=0;a4<L->bounds[3];a4++)
                               {
                                   cout<<"a["<<a1<<"]["<<a2<<"]["<<a3<<"]["<<a4<<"] ="<<L->base[counts]<<"  ";
                                   counts++;
                                   if(counts%5==0)
                                   {
                                       cout<<endl;
                                   }
                               }
                           }
                       }
                   }
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值