一道计算机三级的题目,在线等。
函数loop(s,m,n,str)对长为n的字符串str,从第s个字符开始的间隔为m的部分字符进行循环左移,即str[s]←str[s+m]←...←str[s+km]←...←str[s](k为正整数)且当字符位置s+k*m大于等于n时,要求用s+k*m-n作新的字符位置。要求编写此函数实现其功能,最后调用函数WriteDat()把结果输出到文件out.dat中。
例:loop(1,2,8,str)
位置 0 1 2 3 4 5 6 7
调用前str=A B C D E F G H
调用后str=A D C F E H G B
部分源程序已给出。
请勿改动主函数main()和写函数WriteDat()的内容。
#include <stdio.h>
#include <string.h>
loop(int s,int m,int n,char str[])
{
//这里是要补充的函数
}
main()
{
char buf[81];
strcpy(buf,\ ABCDEFGH\ );
printf(\ 调用前=%s,\ ,buf);
loop(1,2,8,buf);
printf(\ 调用后=%s\\n\ ,buf);
writeDat();
}
writeDat()
{
FILE *in,*out;
char buf[81];
int i,s,m,n;
in=fopen(\ in.dat\ ,\ r\ );
out=fopen(\ out.dat\ ,\ w\ );
for(i=0;i<5;i++){
fscanf(in,\ %d,%d,%d,%s\ ,&s,&m,&n,buf);
loop(s,m,n,buf);
fprintf(out,\ %s\\n\ ,buf);
}
fclose(in);
fclose(out);
}
--------------------------------------------
主要偶根据题目意思不知道左移到什么样才算完成。就是循环到什么情况才算结束。
急呀。谢了。各位。