c语言滚动数组,C 练习实例68

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

#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***2969@qq.com2年前 (2019-09-07)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值