再用函数实现(方法有些复杂)

#include

#include

#define NUM 10

#define LEFT 1

#define RIGHT -1

#define ARRLEN(arr) ( sizeof(arr)/sizeof(*arr) )

#define PTARR(type,arr,n) {int i=0; printf("component:\n"); while(i

#define voidcopy(pa,pb,size) {int asize=(size); while(--asize>=0) *((char*)(pa)+asize) = ( pb==NULL?0:*((char*)(pb)+asize) ) ; }

void * arrmove(int direct,void *arr,int byte,int size,int n);

void * cirmove(int direct,void *arr,int byte,int size,int n);

int main()

{

int j;

double arr[NUM]={1,2,3,4,5,6,7,8,9,10};

PTARR(%.3lf,arr,10);

for(j=-11;j<12;j++)

{

cirmove(RIGHT,arr,sizeof(*arr),ARRLEN(arr),j);

PTARR(%.3lf,arr,10);

cirmove(RIGHT,arr,sizeof(*arr),ARRLEN(arr),-j);

getchar();

}

getchar();

return 0;

}

void * arrmove(int direct,void *arr,int byte,int size,int n) //方向: +:左 -:右 0:??

{

if(direct)

{

int start,step; //起点,步长

int i,j;

char *arri,*arrj;

direct = n>0 ? direct : -direct; //方向变换

n = n>0 ? n : -n; //n取绝对值

start = direct>0 ? 0 : size-1;//起点,左移为0

step = direct>0 ? 1 : -1;//步长,左移为1

while(start=0)

{

i = start;

j = direct>0 ? i+n : i-n;

arri=(char*)arr+byte*i;

arrj=(char*)arr+byte*j;

if(direct>0)

{

if(j

{

voidcopy(arri,arrj,byte);

voidcopy(arrj,NULL,byte);

} //不存在就把0赋值给arr[i]

else voidcopy(arri,NULL,byte);

} //类比:arr[i]=arr[j];

else

{

if(j>=0)

{

voidcopy(arri,arrj,byte);

voidcopy(arrj,NULL,byte);

}

else voidcopy(arri,NULL,byte);

}

start+=step;

}

return arr;

}

return NULL;//无方向

}

void * cirmove(int direct,void *arr,int byte,int size,int n) //方向: +:左 -:右 0:??

{

if(direct)

{

int start,step; //起点,步长

int i,j,st1,sp1;

char *str,*arri,*arrj;;

direct = n>0 ? direct : -direct; //方向变换

n = n>0 ? n : -n; //n取绝对值

n%=size; //循环 取余

if(n==0) return arr; //提前终止

str=(char*)malloc(size*n);

start = direct>0 ? 0 : size-1;//起点,左移为0

st1 = direct>0 ? 0 : n-1;

step = direct>0 ? 1 : -1;//步长,左移为1

sp1 = direct>0 ? 1 : -1;

while(direct>0 ? st1=0)

{

i=st1;

j=start;

arri=(char*)str+byte*i;

arrj=(char*)arr+byte*j;

voidcopy(arri,arrj,byte);

st1+=sp1;

start+=step;

}//复制

arrmove(direct,arr,byte,size,n); /*------位移-----*/

start = direct>0? size-n : 0;

for(st1=0;st1

{

i=start;

j=st1;

arri=(char*)arr+byte*i;

arrj=(char*)str+byte*j;

voidcopy(arri,arrj,byte);

}//粘贴

return arr;

}

return NULL;//无方向

}BakaCirno1001 BakaCirno1001

341***[email protected]

7年前 (2019-09-07)