//因为太懒没写输入错误维度和下标的情况
#ifndef PCH_H
#define PCH_H
#include<cstdlib>
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#include<iostream>
constexpr auto M = 3;
constexpr auto N = 4;
constexpr auto OK= 1;
constexpr auto ERROR = 0;
constexpr auto MAX_DIM = 5;
typedef int Status;
typedef struct {
int* base;//顺序存储
int dim;//维度
int* bound;//存储每个维度边界
int num;//计数
int* content;//计算每一维多少字节
}Array;
//三维数组的话
//typedef int Array3[M][N][N];等价于↓
//typedef int Array1[N];
//typedef Array1 Array2[N];
//typedef Array2 Array3[M];
/*特殊矩阵的压缩存储
1.对称矩阵
typedef int sa[(1+n)n/2];
sa[k]与矩阵元aij之间存在着一一对应关系:
k=
i(i-1)/2+j-1 i≥j
j(j-1)/2+i-1 i<j
2.三角矩阵
(1)上三角矩阵
k=
(n+n-i+1+1)(i-1)/2+(j-i) i≤j
(n+1)n/2 i>j
(2)下三角矩阵
k=
(1+i-1)(i-1)/2+j-1 i>=j
(1+n)n/2 i<j
*/
//创建相应的数组
Status InitArray(Array &A,int dim);
//元素偏移位数查找
int LocateDim(Array& A, int &off);
//测试用
Status GiveValue(Array& A);
//销毁数组
Status DestroyArray(Array &A);
Status Display(Array A);
//A是n维数组,e是元素变量,随后是n个下标值
//若下标不超界,则e被赋值为指定A的元素值
Status Value(Array A,int &e);
//若下标不超界,则将e的值赋给所指定的A的元素
Status Assign(Array &A, int e);
//以此类推,一个n维数组可以定义为其数组元素为n-1维数组类型的
//数组只有存取元素和修改元素值的操作
#endif
------------------------------------------------
#include "pch_h.h"
Status InitArray(Array& A, int dim)
{
int elemtotal = 1;//计算数组内元素个数
if (dim<1||dim>MAX_DIM)
{
std::cout << "数组维度不合法\n";
return ERROR;
}
A.dim = dim;
//bounds用于存储每一维的大小
A.bound = (int*)malloc(sizeof(int) * dim);
//给记录每个维数里的元素个数的数字分配空间
if (A.bound == NULL)
{
std::cout << "A.bound空间分配失败 ";
exit(-1);
}//空间分配失败
int ap = 0;
std::cout<<"知道了"<<dim<<"维,请输入每个维数的元素个数:";
for (int i=0;i<dim;i++) {
std::cin >>ap;
A.bound[i] =ap ;//第二参数是返回参数类型
if (A.bound[i]<0)
{
std::cout << "有一维数据个数获取失败";
exit(0);
}
elemtotal *= A.bound[i];
}
//为数组元素开启足够的空间(顺序
A.base = (int *)malloc(sizeof(int)* elemtotal);
A.num = elemtotal;
if (A.base == NULL)
{
std::cout << "A.base空间分配失败 ";
exit(-1);
}//喜闻乐见空间分配失败
A.content = (int *)malloc(sizeof(int)*dim);
//用于计算三个维度的字节数的数字!!!所以只乘了dim
if (A.content == NULL)
{
std::cout << "A.content 空间分配失败 ";
exit(-1);
}
A.content[dim - 1] = 1;
for (int i=dim-2;i>=0;i--){
A.content[i] = A.bound[i+1]* A.content[i+1];
}
return OK;
}
int LocateDim(Array& A, int &off)
{
int ap=0; off = 0;
for (int i=0;i<A.dim;i++)
{
std::cin >>ap;
off += A.content[i] * (ap - 1);
}
return OK;
}
Status GiveValue(Array& A)
{
int e = 0; int n = 1;
for (int i=0;i<A.dim;i++) {
n *= A.bound[i];
}
std::cout << "输入数组的"<<n<<"个元素值?";
for (int i=0;i<n;i++) {
std::cin >> e;
A.base[i] = e;
}
return OK;
}
Status DestroyArray(Array& A)
{//动态定义需要手动释放空间
if (A.base)
{
free(A.base);
A.base = NULL;
}
if (A.bound)
{
free(A.bound);
A.bound = NULL;
}
if (A.content)
{
free(A.content);
A.content = NULL;
}
return OK;
}
Status Value(Array A, int& e)
{
int off = 0;
std::cout << "赋予e哪个位置的值?";
LocateDim(A, off);
e = *(A.base + off);
return e;
}
Status Assign(Array& A, int e)
{
int off = 0;
std::cout << "赋予哪个位置e的值?";
LocateDim(A, off);
*(A.base + off) = e ;
return OK;
}
Status Display(Array A) {
for (int i=0;i<A.num;i++) {
std::cout << A.base[i]<<" ";
}
return OK;
}
---------------------------------------------------
#include "pch_h.h"
void main()
{
Array A;
int dim = 0; int off = 0; int e=0 ;
std::cout << "维度?";
std::cin >> dim;
std::cout<<"InitArray(A, 3) 执行 : "<<InitArray(A, dim) << "\n";
std::cout << "GiveValue(A)执行 : " << GiveValue(A)<<"\n";
std::cout << "Display(A)执行 : " << Display(A) << "\n";
std::cout << "Value(A,e, ...)执行" << Value(A, e) << "\n";
std::cout<<"e=" << e;//(2,2)=4
std::cout << "Assign(A, e)执行" << Assign(A, e) << "\n";
std::cout << "Display(A)执行 : " << Display(A) << "\n";
system("pause");
}
参考:数据结构-多维数组的实现