zoukankan      html  css  js  c++  java
  • c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出

    Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.

    /* 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); //自己编写getline()函数,接收整行字符串
    void copy(char to[], char from[]); //和c语言库函数strcpy()实现同样的功能。
    
    /* 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:
    %s", max, longest);
      }
      printf("
    ");
      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 != '
    '; ++i)
      {
        if(i < lim - 1)
        {
          s[j++] = c;
        }
      }
      if(c == '
    ')
      {
        if(i <= lim - 1)
        {
          s[j++] = c;
        }
        ++i;
      }
      s[j] = '';
      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]) != '')
      {
        ++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] != ' ') { 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] = ' '; } getmore = 0; } else if(max < len) { max = len; copy(longest, line); } prevmax = 0; } }
    if(max > 0) { printf("%s", longest); printf("len = %d ", max); } return 0; }
    //重新实现getline,使得getline的容错性更强。接收后的getline一定以' '结束。
    int getline(char s[], int lim) { int c, i; for(i = 0; i < lim - 1 && ((c = getchar()) != EOF && c != ' '); ++i) s[i] = c; if(c == ' ') { s[i] = c; ++i; } else if(c == EOF && i > 0) { /* gotta do something about no newline preceding EOF */ s[i] = ' '; ++i; } s[i] = ''; return i; } void copy(char to[], char from[]) { int i; i = 0; while((to[i] = from[i]) != '') ++i; }
    
    
  • 相关阅读:
    2013级机试D题解析
    关于C# 委托(delegate)与事件(event)的用法及事例
    ASP.NET 简单的柱形图实现(附带示例)
    jQuery 关于IE9上传文件无法进入后台问题的原因及解决办法(ajaxfileupload.js第四弹)
    jQuery 自制上传头像插件-附带Demo实例(ajaxfileupload.js第三弹)
    jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)
    ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload.js第一弹)
    jQuery 表格中实现“删除线”的增进方法
    Skype坑爹报错:“旧版本无法删除,请联络您的技术支持小组 ”的解决办法
    ASP.NET Button控件的UseSubmitBehavior属性引发的血案
  • 原文地址:https://www.cnblogs.com/haore147/p/3647948.html
Copyright © 2011-2022 走看看