函數(shù)ReadDat()實(shí)現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串?dāng)?shù)組xx中;請編制函數(shù)encryptChar(),按行優(yōu)先把所有的小寫字母替換成它前面的字母其它不變,a換成z,例如:Aabm.() 輸出 Azal.() 最后調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件PS10.DAT中。
部分源程序已給出,原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個(gè)字符。
請勿改動(dòng)主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
unsigned char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void)
void WriteDat(void)
void encryptChar()
{
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件ENG.IN不能打開!\n\007");
return;
}
encryptChar();
WriteDat();
}
int ReadDat(void)
{
FILE *fp;
int i=0;
unsigned char *p;
if((fp=fopen("eng.in","r"))==NULL) return 1;
while(fgets(xx[i],80,fp)!=NULL){
p=strchr(xx[i],'\n');
if(p)*p=0;
i++;
}
maxline=i;
fclose(fp);
return 0;
}
void WriteDat(void)
{
FILE *fp;
int i;
fp=fopen("ps10.dat","w");
for(i=0;i
printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
參考答案:
void encryptChar()
{int i,j;
for (i=0;i<50;i++)
for (j=0;j<80;j++)
if (xx[i][j]=='a') xx[i][j]='z';
else (xx[i][j]>='b'&&xx[i][j]<='z') xx[i][j]-=1;
}