★題目96(無(wú)憂(yōu)id 87 字符替換題)
函數(shù)ReadDat()實(shí)現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串?dāng)?shù)組xx中;請(qǐng)編制函數(shù)encryptChar(),按給定的替代關(guān)系對(duì)數(shù)組xx中的所有字符進(jìn)行替代,仍存入數(shù)組xx的對(duì)應(yīng)的位置上,最后調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件PS5.DAT中。
替代關(guān)系:f(p)=p*11mod 256 (p是數(shù)組中某一個(gè)字符的ASCII值,f(p)是計(jì)算后新字符的ASCII值),如果計(jì)算后f(p)值小于等于32或f(p)對(duì)應(yīng)的字符是小寫(xiě)字母,則該字符不變,否則將f(p)所對(duì)應(yīng)的字符進(jìn)行替代。
部分源程序存在文件prog1.c中。原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個(gè)字符。
請(qǐng)勿改動(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()
{ int i,j;
for(i=0;i for(j=0;j if(xx[i][j]*11%256<=32||xx[i][j]*11%256>='a'&&xx[i][j]*11%256<='z') continue;
else xx[i][j]=xx[i][j]*11%256;
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件ENG.IN不能打開(kāi)!\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("ps5.dat","w");
for(i=0;i printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
此題還有許多解法,方法可看題8
函數(shù)ReadDat()實(shí)現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串?dāng)?shù)組xx中;請(qǐng)編制函數(shù)encryptChar(),按給定的替代關(guān)系對(duì)數(shù)組xx中的所有字符進(jìn)行替代,仍存入數(shù)組xx的對(duì)應(yīng)的位置上,最后調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件PS5.DAT中。
替代關(guān)系:f(p)=p*11mod 256 (p是數(shù)組中某一個(gè)字符的ASCII值,f(p)是計(jì)算后新字符的ASCII值),如果計(jì)算后f(p)值小于等于32或f(p)對(duì)應(yīng)的字符是小寫(xiě)字母,則該字符不變,否則將f(p)所對(duì)應(yīng)的字符進(jìn)行替代。
部分源程序存在文件prog1.c中。原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個(gè)字符。
請(qǐng)勿改動(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()
{ int i,j;
for(i=0;i
else xx[i][j]=xx[i][j]*11%256;
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件ENG.IN不能打開(kāi)!\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("ps5.dat","w");
for(i=0;i
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
此題還有許多解法,方法可看題8