下面是用程序(C语言)
将文章保存为d:\article.txt文件。 生成的打乱文件将在d:\new_art.txt
你如果没有编译器的话,你留下邮箱,我将生成的exe文件发给你。
然后运行一下代码生成的可执行程序即可。
/*****************************************************************
*Author :wacs5
*Date :20081230(YYYYMMDD)
*Function :
* 打乱文章顺序(文章篇幅不多于8000字)
*input data :d:\article.txt
*output data :d:\new_art.txt
****************************************************************/
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXCHR 8000 /*最大处理8000个汉字*/
main()
{
char *text;
int *loc,*flag;
int i,count,readchr,rndord,temp;
fpos_t filelen;
FILE *fp,*fpout;
srand((unsigned)time(NULL));
fp=fopen("d:\\article.txt","r");
fpout=fopen("d:\\new_art.txt","w");
if (fp==NULL)
{
printf("ERROR: File Open Error:\nPress any key to exit:");
getch();
exit(1);
}
fseek(fp,0,SEEK_END); /*reach the End of File*/
fgetpos(fp,&filelen);
if (filelen>MAXCHR)
{
printf("ERROR: The Article is too big:\nPress any key to exit:");
getch();
exit(1);
}
text=(char *) calloc(filelen+20,sizeof(char));
loc =(int *) calloc(filelen+20,sizeof(int ));
flag=(int *) calloc(filelen+20,sizeof(int ));
if (text==NULL || loc==NULL || flag==NULL)
{
printf("ERROR: No enough Memory:\nPress any key to exit:");
getch();
exit(1);
}
for (i=0;i<filelen;i++)
loc[i]=flag[i]=0;
memset(text,'\0',filelen);
fseek(fp,0,SEEK_SET); /*Go to the beginning of the file*/
readchr=fread(text,sizeof(char),filelen,fp);
printf("filelen=%ld\nreadchr=%d\n",filelen,readchr);
i=0; /*读text内容的序号*/
count=0; /*文章中有多少个字(一个英文、一个汉字都算一个字)*/
while(i<readchr)
{
if (text[i]&0x80)
{
flag[count]=1; /*标记一下,这个位置为汉字*/
loc[count++]=i; /*记住位置*/
i+=2;
}
else
{
loc[count++]=i;
i++;
}
}
for (i=0;i<count;i++)
{
rndord=rand()%(count-i);
/*输出rndord的字(英文或中文)*/
if (flag[rndord])
fprintf(fpout,"%c%c",text[loc[rndord]],text[loc[rndord]+1]);
else
fprintf(fpout,"%c",text[loc[rndord]]);
temp=loc[count-i-1];
loc[count-i-1]=loc[rndord];
loc[rndord]=temp;
temp=flag[count-i-1];
flag[count-i-1]=flag[rndord];
flag[rndord]=temp;
}
free(text);
free(loc);
free(flag);
fclose(fp);
printf("OK\n");
getch();
}
温馨提示:答案为网友推荐,仅供参考