CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

TCPL习题1-16的疑问

楼主catylifen(风林)2006-03-04 16:15:05 在 C/C++ / C语言 提问

在TCPL   P20有这样一个问题:编写一个程序,读入一组文本行,并把最长的文本行打印出来.书中已经有一程序,功能不完善.在作业1-16作者把他作为习题让读者自己解决.在习题解答中有两个程序,如下:  
  /*   This   is   the   first   program   exercise   where   the   spec   isn't   entirely  
    *   clear.   The   spec   says,   'Revise   the   main   routine',   but   the   true  
    *   length   of   an   input   line   can   only   be   determined   by   modifying  
    *   getline.   So   that's   what   we'll   do.   getline   will   now   return   the  
    *   actual   length   of   the   line   rather   than   the   number   of   characters  
    *   read   into   the   array   passed   to   it.  
    */  
     
  #include   <stdio.h>  
     
  #define   MAXLINE   1000   /*   maximum   input   line   size   */  
     
  int   getline(char   line[],   int   maxline);  
  void   copy(char   to[],   char   from[]);  
     
  /*   print   longest   input   line   */  
  int   main(void)  
  {  
      int   len;                               /*   current   line   length   */  
      int   max;                               /*   maximum   length   seen   so   far   */  
      char   line[MAXLINE];         /*   current   input   line   */  
      char   longest[MAXLINE];   /*   longest   line   saved   here   */  
     
      max   =   0;  
     
      while((len   =   getline(line,   MAXLINE))   >   0)  
      {  
          printf("%d:   %s",   len,   line);  
     
          if(len   >   max)  
          {  
              max   =   len;  
              copy(longest,   line);  
          }  
      }  
      if(max   >   0)  
      {  
          printf("Longest   is   %d   characters:\n%s",   max,   longest);  
      }  
      printf("\n");  
      return   0;  
  }  
     
  /*   getline:   read   a   line   into   s,   return   length   */  
  int   getline(char   s[],   int   lim)  
  {  
      int   c,   i,   j;  
     
      for(i   =   0,   j   =   0;   (c   =   getchar())!=EOF   &&   c   !=   '\n';   ++i)  
      {  
          if(i   <   lim   -   1)  
          {  
              s[j++]   =   c;  
          }  
      }  
      if(c   ==   '\n')  
      {  
          if(i   <=   lim   -   1)  
          {  
              s[j++]   =   c;  
          }  
          ++i;  
      }  
      s[j]   =   '\0';  
      return   i;  
  }  
     
  /*   copy:   copy   'from'   into   'to';   assume   'to'   is   big   enough   */  
  void   copy(char   to[],   char   from[])  
  {  
      int   i;  
     
      i   =   0;  
      while((to[i]   =   from[i])   !=   '\0')  
      {  
          ++i;  
      }  
  }  
     
   
   
   
   
  Chris   Sidi,   however,   was   not   convinced   -   he   thought   this   answer   was   "too   easy",   so   he   checked   with   bwk,   who   agreed.   Chris   writes:   "Looks   like   Mr.   Kernighan   meant   for   "main   routine"   in   Exercise   1-16   to   refer   to   function   main(),   saying   your   solution   of   modifying   getline()   is   "too   easy."   :)   (Though   I   think   your   solution   shouldn't   be   removed   from   the   Answers   web   site,   just   complimented   with   another   one   that   only   modifies   main())"    
   
  Cue   Mr   "386sx",   riding   to   the   rescue   on   a   white   horse...    
   
     
  /*   Exercise   1-16   */  
     
  #include   <stdio.h>  
     
  #define   MAXLINE   20  
     
  int   getline(char   s[],   int   lim);  
  void   copy(char   to[],   char   from[]);  
     
  int   main(void)  
  {  
          char   line[MAXLINE];  
          char   longest[MAXLINE];  
          char   temp[MAXLINE];  
          int   len,   max,   prevmax,   getmore;  
           
          max   =   prevmax   =   getmore   =   0;  
          while((len   =   getline(line,   MAXLINE))   >   0)  
          {  
                  if(line[len   -   1]   !=   '\n')  
                  {  
                          if(getmore   ==   0)  
                                  copy(temp,   line);  
                          prevmax   +=   len;  
                          if(max   <   prevmax)  
                                  max   =   prevmax;  
                          getmore   =   1;  
                  }  
                  else  
                  {  
                          if(getmore   ==   1)  
                          {  
                                  if(max   <   prevmax   +   len)  
                                  {  
                                          max   =   prevmax   +   len;  
                                          copy(longest,   temp);  
                                          longest[MAXLINE   -   2]   =   '\n';  
                                  }  
                                  getmore   =   0;  
                          }  
                           
                          else   if(max   <   len)  
                          {  
                                  max   =   len;  
                                  copy(longest,   line);  
                          }  
                          prevmax   =   0;  
                  }  
          }  
          if(max   >   0)  
          {  
                  printf("%s",   longest);  
                  printf("len   =   %d\n",   max);  
          }  
     
          return   0;  
  }  
     
  int   getline(char   s[],   int   lim)  
  {  
          int   c,   i;  
     
          for(i   =   0;  
                  i   <   lim   -   1   &&   ((c   =   getchar())   !=   EOF   &&   c   !=   '\n');  
                  ++i)  
                  s[i]   =   c;  
     
          if(c   ==   '\n')  
          {  
                  s[i]   =   c;  
                  ++i;  
          }  
          else   if(c   ==   EOF   &&   i   >   0)  
          {  
                  /*   gotta   do   something   about   no   newline   preceding   EOF   */  
                  s[i]   =   '\n';    
                  ++i;  
          }  
          s[i]   =   '\0';  
          return   i;  
  }  
     
  void   copy(char   to[],   char   from[])  
  {  
          int   i;  
     
          i   =   0;  
          while((to[i]   =   from[i])   !=   '\0')  
                  ++i;  
  }    
  对这两程序有几个问题,请教一下:  
  两个程序中while语句的结束条件是getline函数的返回值小于或者等于0,问题是:如果在文本的结尾那一行,没有换行,只有EOF,那么EOF在getline读最后一行时就被读过了,下一次getline做了什么呢?  
  另外,EOF怎么输入电脑呢?文本编辑编辑程序会自动添加吗?  
   
  问题点数:20、回复次数:2Top

1 楼cxc014(有心插柳柳不活,无心栽花花开花?)回复于 2006-03-04 17:04:08 得分 0

那么EOF在getline读最后一行时就被读过了,下一次getline做了什么呢?  
  ===========  
  it   calls   setstate(failbit).   In   any   case,   it   returns   *this.(MSDN)  
  可以用流的fail()函数来检测.  
   
  EOF怎么输入电脑呢?文本编辑编辑程序会自动添加吗?  
  ==========  
  windows   console   下CTRL   +   Z  
  文本编辑编辑程序会自动添加.  
  Top

2 楼jixingzhong(瞌睡虫·星辰)回复于 2006-03-04 17:52:51 得分 0

EOF怎么输入电脑呢?文本编辑编辑程序会自动添加吗?  
  -----------------------  
  win   下   ctrl+z  
  unix下   ctrl+d  
   
  EOF   这个东西没有在文件中的   ....  
  所以也没有自动添加的说法     ....  
   
  要知道细节,  
  可以去看看   FILE   结构体的成员和含义,  
  也就是文件信息,  
  这里有文件的结束信息,  
  而不是把文件结束信息以结束符号的形式保存到文件中的   ~Top

相关问题

  • 习题!!
  • C++习题集。
  • 习题2。22~~~~~~~~
  • 练习题
  • 一道习题!
  • 书上习题
  • 习题求助::
  • C++练习题
  • 一个习题
  • 习题求教

关键词

  • 文本
  • 文件
  • 电脑
  • 信息
  • getline
  • maxline
  • 程序
  • longest
  • 习题
  • 编辑

得分解答快速导航

  • 帖主:catylifen

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo