分类信息
各地考试

当前位置:首页 » 软件水平考试 » 模拟试题 » 正文

2013年软件设计师考试:字符串程序模拟题2


时间:2013-10-21 来源:软件水平考试网 浏览次数:70  【华夏培训网:中国教育培训第一门户

函数ReadDat()实现从文件IN.Dat中读取一篇英语文章存入到字符串数组xx中;请编制函数ConvertCharD(),其函数的功能是:以行为单

函数ReadDat()实现从文件IN.Dat中读取一篇英语文章存入到字符串数组xx中;请编制函数ConvertCharD(),其函数的功能是:以行为单位把字符串中的所有小写字母改写成该字母的前一个字母,如果是a字母,则改写为z。大写字母和其他字符保持不变。把已处理的字符串仍按行重新存入字符串数组xx中。最后main()函数调用函数WriteDat(),把结果输出到文件OUT4.DAT中。  
  
例如: 
原文 
Adb.Bcdza     
abck.LLhj 
结果 
Aca.Bbcyz 
zabj.LLgi 
原始数把文件存放的格式是:每行的宽度均小于80个字符,含标点和空格。 
  
程序: 
#include  
#include  
#include  
  
char xx[50][80]; 
int maxline=0; /*文章总行数*/ 
  
int ReadDat(void); 
void WriteDat(void); 
  
void ConvertCharD(void) 
{/*采用字符串指针方法*/ 
 int i; 
 char *pf; 
 for (i=0;i 
 { 
  pf=xx[i]; 
  while (*pf) 
    if (*pf==’a’) 
      {*pf=’z’;pf++;} 
    else if (*pf>=’b’ && *pf<=’z’) 
           {*pf-=1;pf++;} 
    else 
      pf++; 
 } 
} 
  
void ConvertCharD(void) 
{/*采用字符数组方法*/ 
 int i,j; 
 char c; 
 for (i=1;i 
 { 
  for (j=0;j 
    if (xx[i][j]==’a’)  xx[i][j]=’z’; 
    else 
      if (xx[i][j]>=’b’ && xx[i][j]<=’z’) xx[i][j]--; 
 } 
} 
  
void main() 
{ 
 clrscr(); 
 if (ReadDat()) 
 { 
  printf("数据文件IN.DAT不能打!\n\007"); 
  return; 
 } 
 ConvertCharD(); 
 WriteDat(); 
 system("pause"); 
} 
  
int ReadDat(void) 
{ 
 FILE *fp; 
 int i=0; 
 char *p; 
  
 if ((fp=fopen("IN.DAT","r"))==NULL) return 1; 
 while (fgets(xx[i],80,fp)!=NULL) 
 { 
  p=strchr(xx[i],’\n’); 
  if (p)  *p=0; 
  i++; 
 } 
 maxline=i; 
 fclose(p); 
 return 0; 
} 
  
void WriteDat(void) 
{ 
 FILE *fp; 
 int i; 
  
 fp=fopen("OUT4.DAT","w"); 
 for (i=0;i<=maxline;i++) 
 { 
  printf("%s\n",xx[i]); 
  fprintf(fp,"%s\n",xx[i]); 
 } 
 fclose(fp); 
} 
  
说明:字符串数组xx是二维数组,xx[0]存放第一行字符,xx[1]存放第二行...特别是printf一句中"%s"用时注意。 




       软件水平考试
 

推荐图文

热门点击排行

©2015 hxpx.com All Rights Reserved 滇ICP备13002816号-1
华夏培训网唯一网址www.hxpx.com