zoukankan      html  css  js  c++  java
  • 练习1-22 编写一个程序,把较长的输入行折成短一些的两行或者多行,折行的位置在输入行的第N列之前的最后一个非空格之后。要保持程序能够智能地处理输入行很长以及在制定的列前没有空格或者制表符时的情况。

    题目理解:自定义n为行的极限长度,输入行超过n列则进行折行操作,并且需要每列的开头不能是空格和制表符

    例如n = 3  输入 aaaa 则输出  aaa

                  a

    在有空格和制表符的情况输入 aaa空格空格制表符制表符a

               输出 aaa

                  a(不带空格和制表符)。

    我的答案:

     1 #include <stdio.h>
     2 #define LINEN 2 /*设置折行位置字符*/
     3 
     4 main()
     5 {
     6     int c, pos;
     7     pos = 1;/*当前输入位置*/
     8     while((c = getchar()) != EOF)
     9         if(c == ' ' || c == '	')
    10         {
    11             putchar('');
    12         }else if (c == '
    ')
    13         {
    14             putchar(c);
    15             pos = 1;
    16         }else
    17         {
    18             putchar(c);
    19             ++pos;
    20         while(pos > LINEN){
    21         putchar('
    ');
    22         putchar(c);
    23         break;
    24         }
    25         }
    26 
    27         return 0;
    28 
    29 }
    View Code

    达不到题目要求.看答案

     1 #include <stdio.h>
     2 #define MAXCOL 3   /* maximum colum of input*/
     3 #define TABINC 8    /* tab increment size*/
     4 
     5 char line[MAXCOL]; /* input line*/
     6 
     7 int exptab(int pos);
     8 int findblnk(int pos);
     9 int newpos(int pos);
    10 void printl(int pos);
    11 /*fold long input lines into two or more shorter lines*/
    12 
    13 main()
    14 {
    15     int c, pos;
    16 
    17 pos = 0; /*position is the line*/
    18 while ((c = getchar()) != EOF)
    19 {
    20     line[pos] = c; /* store current character*/
    21 if(c == '	')      /* expand tab character */
    22     pos = exptab(pos);
    23     else if (c == '
    ')
    24     {
    25         printl(pos); /*  print current input line */
    26 pos = 0;
    27     }else if (++ pos >= MAXCOL)
    28     {
    29         pos = findblnk(pos);
    30         printl(pos);
    31         pos = newpos(pos);
    32     }
    33 
    34 }
    35 
    36 }
    37 
    38 /*printl: print line until pos column8*/
    39 
    40 void printl(int pos)
    41 {
    42     int i;
    43     for(i = 0; i < pos; ++i)
    44         putchar(line[i]);
    45     if(pos > 0)         /*any chars printed?*/
    46         putchar('
    ');
    47 }
    48 
    49 
    50 /* exptab: expand tab into blanks */
    51 
    52 int exptab(int pos)
    53 {
    54     line[pos] = ' ';        /*tab is at least one blank */
    55     for(++pos; pos < MAXCOL && pos % TABINC != 0; ++pos)
    56         line[pos] = ' ';
    57     if(pos < MAXCOL)               /*room left incurrent line*/
    58         return pos;
    59     else                            /* current line is full*/
    60     {
    61         printl(pos);
    62         return 0;                   /* reset current position*/
    63     }
    64 }
    65 
    66 
    67 /* findblnk : find blank's position*/
    68 
    69 int findblnk(int pos)
    70 {
    71     while (pos > 0 && line[pos] != ' ')
    72         --pos;
    73     if(pos == 0)                /*no blanks in the line ?*/
    74         return MAXCOL;
    75     else                            /*at least one blank*/
    76         return pos+1;              /*position after the blank*/
    77 }
    78 
    79 
    80 /*newpos:  rearrange line with new position*/
    81 
    82 int newpos(int pos)
    83 {
    84     int i, j;
    85     if(pos <= 0 || pos >= MAXCOL)
    86         return 0;           /*nothing to rearrange*/
    87     else
    88     {
    89         i = 0;
    90         for(j = pos ; j < MAXCOL; ++j)
    91         {
    92             line[i] = line[j];
    93             ++i;
    94         }
    95         return i;       /*new position in line*/
    96     }
    97 }

    对题目要求还不够理解透彻

     题目要求的“要保持程序能够智能地处理输入行很长以及在制定的列前没有空格或者制表符时的情况” 答案貌似也没能够去掉多余的空格和制表符.

  • 相关阅读:
    随笔
    3.1作业
    关于JavaDate数据返回到前端变数字的问题(并引申到前后端时间的传输)
    utf-8转换为base64
    base64转换为utf-8
    Java Web基础——jsp调用动态界面
    Java Web基础——JSP指令标记
    2020软件工程最后一次作业
    软件工程第二次结对作业
    软件工程第三次作业
  • 原文地址:https://www.cnblogs.com/jango/p/3388731.html
Copyright © 2011-2022 走看看