数组Array
基本操作
?
|
1
2
3
4
5
|
Status InitArray(int dimm,...)//若维数dim和随后的各维长度合法,则构造相应的数组A,并返回OK
Status DestroyArray() //销毁数组A
Status Locate(va_list ap,int &off) //若ap指示的各下标值合法,则求出该元素在A中相对地址off
Status Value(ElemType &e,...) //A是n维数组,e为元素变量,随后是n个下标值。若各下表不越界,则e赋值为所指定的A的元素值,并返回OK。
Status Assign(ElemType e,...) //A是n维数组,e为元素变量,随后是n各下表值。/若各下标不越界,则将e的值付给所指定的A的元素,并返回OK。
|
几个小程序(代码正误检验)
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 8
const int OK=1;
const int ERROR=0;
const int INFEASIBLE=-1;
typedef int Status;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
LL n,m,lll,ans;
typedef int ElemType;
#include<stdarg.h> //标准头文件,提供宏va_start、va_arg、va_end 用于存取变长参数表
const int MAX_ARRAY_DIM=8; //假设数组维数的最大值为8
typedef struct
{
ElemType *base; //数组元素基址,由InitArray分配
int dim; //数组维数
int *bounds; //数组维界基址,由InitArray分配
int *constants; //数组映像函数常量基址,由InitArray分配
int elemtotal;
Status InitArray(int dimm,...)//若维数dim和随后的各维长度合法,则构造相应的数组A,并返回OK
{
int i;
va_list ap;
if(dimm<1 || dimm>MAX_ARRAY_DIM)return ERROR;
dim=dimm;
bounds=(int *)malloc(dim*sizeof(int));
if(!bounds)exit(OVERFLOW);//若各维长度合法,则存入A.bounds,并求出A的元素总数elemtotal
elemtotal=1;
va_start(ap,dim); //ap为va_list类型,是存放变长参量数表信息的数组
for(i=0;i<dim;i++)
{
bounds[i]=va_arg(ap,int);
if(bounds[i]<0)return UNDERFLOW;
elemtotal*=bounds[i];
}
va_end(ap);
base=(ElemType *)malloc(elemtotal*sizeof(ElemType));
if(!base)exit(OVERFLOW);
constants=(int *)malloc(dim*sizeof(int));
//求映像函数的常数ci,并存入A.constants[i-1],i=1,...,dim
if(!constants)exit(OVERFLOW);
constants[dim-1]=1; //L=1,指针的增减以元素的大小为单位
for(i=dim-2;i>=0;i--)
constants[i]=bounds[i+1]*constants[i+1];
return OK;
}//InitArray
Status DestroyArray() //销毁数组A
{
if(!base)return ERROR;
free(base);base=NULL;
if(!bounds)return ERROR;
free(bounds);bounds=NULL;
if(!constants)return ERROR;
free(constants);constants=NULL;
return OK;
}//DestroyArray
Status Locate(va_list ap,int &off) //若ap指示的各下标值合法,则求出该元素在A中相对地址off
{
int i,ind;
off=0;
for(i=0;i<dim;i++)
{
ind=va_arg(ap,int);
if(ind<0 || ind>=bounds[i])return OVERFLOW;
off+=constants[i]*ind;
}
return OK;
}//Locate
Status Value(ElemType &e,...) //A是n维数组,e为元素变量,随后是n个下标值。
//若各下表不越界,则e赋值为所指定的A的元素值,并返回OK。
{
va_list ap;
int result,off;
va_start(ap,e);
if((result=Locate(ap,off))<=0)return result;
e=*(base+off);
return OK;
}//Value
Status Assign(ElemType e,...) //A是n维数组,e为元素变量,随后是n各下表值。
//若各下标不越界,则将e的值付给所指定的A的元素,并返回OK。
{
va_list ap;
int result,off;
va_start(ap,e);
if((result=Locate(ap,off))<=0)return result;
*(base+off)=e;
return OK;
}//Assign
}Array;
void ArrayCheck()//代码正误检验
{
int i,j,k;
Array A;
ElemType e;
A.InitArray(3,2,3,2);
printf("维度:%d\\n总元素个数:%d\\n各维维界:",A.dim,A.elemtotal);
for(i=0;i<A.dim;i++)
printf("%d ",A.bounds[i]);
puts("");
for(i=0;i<A.bounds[0];i++)
for(j=0;j<A.bounds[1];j++)
for(k=0;k<A.bounds[2];k++)
A.Assign(i*100+j*10+k+111,i,j,k);
for(i=0;i<A.bounds[0];i++,puts(""))
for(j=0;j<A.bounds[1];j++,puts(""))
for(k=0;k<A.bounds[2];k++)
printf("%d ",(A.Value(e,i,j,k),e));
A.DestroyArray();
puts("");
}
|
?
|
1
2
3
4
5
6
7
8
9
10
11
|
程序结果:
维度:3
总元素个数:12
各维维界:2 3 2
111 112
121 122
131 132
211 212
221 222
231 232
|
主函数:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
int main()
{
#ifndef ONLINE_JUDGEW
// freopen("1.txt","r",stdin);
freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z,xx,yy;
// init();
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d%d",&n,&m))
{
ArrayCheck();
}
return 0;
}
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
猜你喜欢
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 15
-
2025-05-29 89
-
2025-05-25 40
-
2025-05-27 12
-
2025-06-04 77
热门评论

