數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array實(shí)例詳解
成都創(chuàng)新互聯(lián)公司專注于貴陽(yáng)企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,成都做商城網(wǎng)站。貴陽(yáng)網(wǎng)站建設(shè)公司,為貴陽(yáng)等地區(qū)提供建站服務(wù)。全流程專業(yè)公司,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
數(shù)組Array
基本操作
Status InitArray(int dimm,...)//若維數(shù)dim和隨后的各維長(zhǎng)度合法,則構(gòu)造相應(yīng)的數(shù)組A,并返回OK Status DestroyArray() //銷毀數(shù)組A Status Locate(va_list ap,int &off) //若ap指示的各下標(biāo)值合法,則求出該元素在A中相對(duì)地址off Status Value(ElemType &e,...) //A是n維數(shù)組,e為元素變量,隨后是n個(gè)下標(biāo)值。若各下表不越界,則e賦值為所指定的A的元素值,并返回OK。 Status Assign(ElemType e,...) //A是n維數(shù)組,e為元素變量,隨后是n各下表值。/若各下標(biāo)不越界,則將e的值付給所指定的A的元素,并返回OK。
幾個(gè)小程序(代碼正誤檢驗(yàn))
// //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> //標(biāo)準(zhǔn)頭文件,提供宏va_start、va_arg、va_end 用于存取變長(zhǎng)參數(shù)表 const int MAX_ARRAY_DIM=8; //假設(shè)數(shù)組維數(shù)的最大值為8 typedef struct { ElemType *base; //數(shù)組元素基址,由InitArray分配 int dim; //數(shù)組維數(shù) int *bounds; //數(shù)組維界基址,由InitArray分配 int *constants; //數(shù)組映像函數(shù)常量基址,由InitArray分配 int elemtotal; Status InitArray(int dimm,...)//若維數(shù)dim和隨后的各維長(zhǎng)度合法,則構(gòu)造相應(yīng)的數(shù)組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);//若各維長(zhǎng)度合法,則存入A.bounds,并求出A的元素總數(shù)elemtotal elemtotal=1; va_start(ap,dim); //ap為va_list類型,是存放變長(zhǎng)參量數(shù)表信息的數(shù)組 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)); //求映像函數(shù)的常數(shù)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() //銷毀數(shù)組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指示的各下標(biāo)值合法,則求出該元素在A中相對(duì)地址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維數(shù)組,e為元素變量,隨后是n個(gè)下標(biāo)值。 //若各下表不越界,則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維數(shù)組,e為元素變量,隨后是n各下表值。 //若各下標(biāo)不越界,則將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()//代碼正誤檢驗(yàn) { int i,j,k; Array A; ElemType e; A.InitArray(3,2,3,2); printf("維度:%d\n總元素個(gè)數(shù):%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(""); }
程序結(jié)果: 維度:3 總元素個(gè)數(shù):12 各維維界:2 3 2 111 112 121 122 131 132 211 212 221 222 231 232
主函數(shù):
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; }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
標(biāo)題名稱:數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array實(shí)例詳解
文章來(lái)源:http://chinadenli.net/article28/giicjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、用戶體驗(yàn)、定制開(kāi)發(fā)、網(wǎng)頁(yè)設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)