读书人

一个关于数组中满足条件的元素取舍、及

发布时间: 2012-09-08 10:48:07 作者: rapoo

一个关于数组中满足条件的元素选择、及函数变换得c语言函数
函数ReadDat()的功能是实现从文件ENG65.IN中读取一篇英文文章,存入到字符串数组xx中;请编制函数encryptChar(),按给定的替代关系对数组xx中的所有字符进行替代,仍存入数组xx的对应位置上,最后调用函数WriteDat()把结果xx输出到文件PS65.DAT中。
替代关系:f(p)=p*11 mod 256(p是数组中某一个字符的ASCII值,f(p)是计算后新字符的ASCII值),如果计算后f(p)值小于等于32或f(p)对应的字符是数字0至9或大于128,则该字符不变,否则将f(p)所对应的字符进行替代。
注意:部分源程序已给出。原始数据文件存放的格式是:每行的宽度均小于80个字符。请勿改动主函数main()、读函数ReadDat()和写函数WriteDat()的内容。

#include  <stdio.h>#include  <string.h>#include  <windows.h>#include  <conio.h>#include  <ctype.h>unsigned char xx[50][80];int maxline=0;/*文章的总行数*/int ReadDat(void);void WriteDat(void);void encryptChar(){int i,j;char temp;for(i=0;i<50;i++)for(j=0;j<80;j++){temp = (xx[i][j] * 11) % 256;if(!((temp<=32)||((temp>=48)&&(temp<=57))||(temp>128)))xx[i][j] = temp;}}void main(){   system("cls");  if(ReadDat())  { printf("数据文件ENG65.IN不能打开!\n\007"); return; }  encryptChar();  WriteDat();}int ReadDat(void){  FILE *fp;  int i=0;  unsigned char *p;  if((fp=fopen("ENG65.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("PS65.DAT","w");  for(i=0;i<maxline;i++)  { printf("%s\n",xx[i]); fprintf(fp,"%s\n",xx[i]); }  fclose(fp);}

读书人网 >C语言

热点推荐