首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求教南开100之第三题。解决方案

2012-03-11 
求教南开100之第三题。。。有答案但是其一个循环看不懂什么意思,请高人指教。谢谢了。。for(jstrl-1j index

求教南开100之第三题。。。
有答案但是其一个循环看不懂什么意思,请高人指教。谢谢了。。
            for(j=strl-1;j> =index;j--)   /*这个循环看不懂。请高人指教啊*/
                  {ch=xx[I][strl-1];
                  for(k=strl-1;k> 0;k--)
                        xx[I][k]=xx[I][k-1];
                  xx[I][0]=ch;
            }


        题目3:函数READDAT()实现从文件IN.DAT中读取一篇英文文章存入到字符串数组XX中;请编制函数STROR(),其函数功能是:以行为单位把

字符串中的所有小写字母O左边的字符串内容移到该串的右边存放,然后并把小写字母O删除,余下的字符串内容移到已处理字符串的左边存放.最

后把已处理的字符串仍按行重新存入字符串数组XX中,最后调用函数WRITEDAT()把结果XX输出到文件OUT.DAT中.
例如:原文:You   can   create   an   index   on   any   field.
                    you   have   the   correct   record.
          结果:   n   any   field.You   can   create   an   index
            rd.yu   have   the   crrect   rec
        原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格.
-------------------
类型:字符串(单词)的倒置和删除。
答案:
void   StrOR(void)   /*标准答案*/
{int   I,j,k,index,strl;
      char   ch;
      for(I=0;I <maxline;I++)
            {strl=strlen(xx[I]);
            index=strl;

            for(j=0;j <strl;j++)
                  if(xx[I][j]== 'o ')
                  {
                        for(k=j;k <strl-1;k++)
                              xx[I][k]=xx[I][k+1];  
                        xx[I][strl-1]=   '   ';
                        index=j;
                  }

            for(j=strl-1;j> =index;j--)   /*这个循环看不懂。请高人指教啊*/
                  {ch=xx[I][strl-1];
                  for(k=strl-1;k> 0;k--)
                        xx[I][k]=xx[I][k-1];
                  xx[I][0]=ch;
            }
      }
}


============================源码:================
#   include "stdio.h "
#   include "string.h "
#   include "conio.h "

char   xx[50][80];
int   maxline=0;

int   ReadDat(void);
void   WriteDat(void);

void   StrOR(void)
{

}

void   main()
{clrscr();
if(ReadDat())
{printf( "Can 't   open   the   file!\n ");
return;}
StrOR();
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(fp);
return   0;
}

void   WriteDat(void)
{FILE   *fp;
int   i;
fp=fopen( "out.dat ", "w ");
for(i=0;i <maxline;i++)
{printf( "%s\n ",xx[i]);
fprintf(fp, "%s\n ",xx[i]);
}
fclose(fp);
}

[解决办法]
帮顶,接分

热点排行