zoukankan      html  css  js  c++  java
  • 数组的实现和一些基本操作

    头文件:

    #include<cstdarg>//标准头文件,提供宏va_start,va_arg和va_end,用于存取变长参数表
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define MYOVERFLOW -2
    typedef int Status;
    typedef int Elemtype;//用指定标识符Elemtype代表int类型,顾名思义表示元素类型为int型
    #define MAX_ARRAY_DIM 8//假设数组维数的最大值为8
    typedef struct{
        Elemtype *base;      //数组元素基址,由InitArray分配
        int dim;             //数组维数
        int *bounds;         //数组维界基址,有InitArray分配
        int *constants;      //数组映像函数常量基址,由InitArray分配
    }Array;
    
    //-----------------基本操作的函数原型说明-------------------
    
    Status InitArray(Array &A, int dim, ...);
    //若数组维数dim和各维长度合法,则构造相应的数组A,并返回OK。
    
    Status DestroyArray(Array &A);
    //销毁数组A。
    
    Status Locate(Array A, va_list ap, int &off);
    //若ap指示的各下标值合法,则求出该元素在A中相对地址off
    
    Status Value(Array A, Elemtype &e,int dim, ...);
    //A是n维数组,e为元素变量,随后是n个下标值
    //若各下标不超界,则e赋值为所指定的A的元素值,并返回OK
    
    Status Assign(Array &A, Elemtype e,int dim, ...);
    //A是n为数组,e为元素变量,随后是n个下标值
    //若各下标不超界,将e的值赋给所指定的A的元素值,并返回OK

    上述操作的实现:

    #include"stdafx.h"
    Status InitArray(Array &A, int dim, ...)
    //若数组维数dim和各维长度合法,则构造相应的数组A,并返回OK。
    {
        if (dim<1 || dim>MAX_ARRAY_DIM) return ERROR;
        A.dim = dim;
        A.bounds = (int *)malloc(dim*sizeof(int));
        if (!A.bounds)exit(MYOVERFLOW);
        int elemtotal = 1;
        va_list ap;
        va_start(ap, dim);//ap为va_list类型,是存放变长参数表信息的数组
        for (int i = 0; i < dim; ++i){
            A.bounds[i] = va_arg(ap, int);
            if (A.bounds[i] < 0)return ERROR;
            elemtotal *= A.bounds[i];
        }
        va_end(ap);
        A.base = (Elemtype*)malloc(elemtotal*sizeof(Elemtype));
        if (!A.base)exit(MYOVERFLOW);
        //求映像函数的常数C(i),并存入A.constants[i-1],i=1,...,dim
        A.constants = (int *)malloc(dim*sizeof(int));
        if (!A.constants)exit(MYOVERFLOW);
        A.constants[dim - 1] = 1;//L=1,指针的增减以元素的大小为单位
        for (int 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)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;
    }
    
    Status Locate(Array A, va_list ap, int &off)
    //若ap指示的各下标值合法,则求出该元素在A中相对地址off
    {
        off = 0;
        for (int i = 0; i < A.dim; ++i){
            int ind = va_arg(ap, int);
            if (ind < 0 || ind >= A.bounds[i])return MYOVERFLOW;
            off += A.constants[i] * ind;
            
        }
        return OK;
    }
    
    Status Value(Array A, Elemtype &e,int dim, ...)
    //A是n维数组,e为元素变量,随后是n个下标值
    //若各下标不超界,则e赋值为所指定的A的元素值,并返回OK
    {
        va_list ap;
        va_start(ap, dim);
        int off,result;
        if (result = Locate(A, ap, off) <= 0)return result;
        e = *(A.base + off);
        return OK;
    }
    
    Status Assign(Array &A, Elemtype e,int dim, ...)
    //A是n为数组,e为元素变量,随后是n个下标值
    //若各下标不超界,将e的值赋给所指定的A的元素值,并返回OK
    {
        va_list ap;
        va_start(ap, dim);
        int result, off;
        if ((result = Locate(A, ap, off)) <= 0)return result;
        *(A.base + off) = e;
        return OK;
    }

    主函数:

    // Array.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Array arr;
        InitArray(arr, 2, 2, 3);
        Assign(arr, 100,2, 1, 2);
        Elemtype e;
        Value(arr, e,2, 1, 2);
        cout << e << endl;
        return 0;
    }

    结果:

  • 相关阅读:
    jsp页面中使用 splitfn:split注意事项
    【SQL】- 基础知识梳理(二)
    【SQL】- 基础知识梳理(一)
    面向对象编程思想-解释器模式
    NPOI操作Excel
    面向对象编程思想-备忘录模式
    面向对象编程思想-访问者模式
    面向对象编程思想-责任链模式
    面向对象编程思想-策略模式
    面向对象编程思想-状态模式
  • 原文地址:https://www.cnblogs.com/csudanli/p/4836951.html
Copyright © 2011-2022 走看看