fmt

来源:互联网 发布:免费程序化交易软件 编辑:程序博客网 时间:2024/04/30 23:40

Unix 的 fmt 程序可以对文本中的段落进行重排,保证每一行不超过 72 个字符。重排时它会遵守以下规则:

  • 新的一行可以从任何一个有空格的地方开始。如果开始一个新行,则在上一行的末尾和新行的开始都不应该有任何空格。
  • 在输出时,可以删除段落中的换行。文章的段落是通过一个空白行来划分的。
  • 在行尾不应该有任何空格。
  • 如果在一长串的字符间没有空格,且其长度超过了 72 个字符,则应该把它们放在一个单独的行上。

Sample Imput

Unix fmt

The unix fmt program reads lines of text, combining
and breaking lines so as to create an
output file with lines as close to without exceeding
72 characters long as possible. The rules for combining and breaking
lines are as follows.

1. A new line may be started anywhere there is a space in the input.
If a new line is started, there will be no trailing blanks at the
end of the previous line or at the beginning of the new line.

2. A line break in the input may be eliminated in the output, provided
it is not followed by a space or another line break. If a line
break is eliminated, it is replaced by a space.

Sample Output

   Unix fmtThe unix fmt program reads lines of text, combining and breaking linesso as to create an output file with lines as close to without exceeding72 characters long as possible.  The rules for combining and breakinglines are as follows.   1.  A new line may be started anywhere there is a space in the input.If a new line is started, there will be no trailing blanks at the end ofthe previous line or at the beginning of the new line.   2.  A line break in the input may be eliminated in the output,provided it is not followed by a space or another line break.  If a linebreak is eliminated, it is replaced by a space.

Source

Miguel Revilla 2002-06-15
 测试输入关于“测试输入”的帮助期待的输出关于“期待的输出”的帮助时间限制关于“时间限制”的帮助内存限制关于“内存限制”的帮助额外进程关于“{$a} 个额外进程”的帮助测试用例 1以文本方式显示
  1. Unix fmt
  2. The unix fmt program reads lines of text, combining
  3. and breaking lines so as to create an
  4. output file with lines as close to without exceeding
  5. 72 characters long as possible. The rules for combining and breaking
  6. lines are as follows.
  7. 1. A new line may be started anywhere there is a space in the input.
  8. If a new line is started, there will be no trailing blanks at the
  9. end of the previous line or at the beginning of the new line.
  10. 2. A line break in the input may be eliminated in the output, provided
  11. it is not followed by a space or another line break. If a line
  12. break is eliminated, it is replaced by a space.
以文本方式显示
  1. Unix fmt
  2. The unix fmt program reads lines of text, combining and breaking lines
  3. so as to create an output file with lines as close to without exceeding
  4. 72 characters long as possible. The rules for combining and breaking
  5. lines are as follows.
  6. 1. A new line may be started anywhere there is a space in the input.
  7. If a new line is started, there will be no trailing blanks at the end of
  8. the previous line or at the beginning of the new line.
  9. 2. A line break in the input may be eliminated in the output,
  10. provided it is not followed by a space or another line break. If a line
  11. break is eliminated, it is replaced by a space.
1秒64M0ple Input

 

 

 

 

#include <stdio.h>
#include <string.h>
main()
{
 char a[100000];
 int i=0,j,n=0,m,s[20000],x=0,y=0,d=0,c;
 while((a[i++]=getchar())!=EOF)
 {
 }
 for(j=0;j<i;j++)
 {
  if((a[j]=='\n')&&(a[j+1]!='\n')&&(a[j-1]!='\n'))
  {
   a[j]=' ';
  }
 }
for(m=0;m<i-2;m++)
{
 if(m!=0&&a[m-1]!='\n')
 {
  if(a[m]==' ')
  {
   for(d=m;d<=m+71;d++)
   {if(a[d]!=' ') {m=d;break;}}
  }
 }
 x=m+71;
 if(x>=i-2) {x=i-3;}
 for(d=m;d<=x;d++)
     {
   if(a[d]=='\n')
   {
    for(c=m;c<d;c++)
    {printf("%c",a[c]);}
    printf("\n");printf("\n");
       m=d+1;goto k;
   }
      }
 if(a[x]==' '&&a[x-1]!=' ')
 {
  for(y=m;y<x;y++)
   {
    putchar(a[y]);
      }
  m=x;
  printf("\n");
 }
 else if(a[x-1]==' '&&a[x]==' ')
 {
  for(y=x-1;1;y--)
  {
   if(a[y]!=' ') break;
  }
  for(d=m;d<=y;d++)
  {
   printf("%c",a[d]);
  }
  m=x;printf("\n");
 }
 else
 {
  if(a[x+1]==' ')
     { 
  for(c=m;c<=x;c++)
     {printf("%c",a[c]);}
  }
  else
     {
      for(d=x;d>=m;d--)
               {
              if(a[d]==' ')break;
               }
      if(d==m-1)
      {
       for(y=m;;y++){if (a[y]==' ') break;}
        for(c=m;c<y;c++) printf("%c",a[c]);
     printf("\n");m=y;goto k;
      }
         for(y=d;y>=m;y--)
             {
            if(a[y]!=' ') break;
             }
        for(c=m;c<=y;c++)
     {printf("%c",a[c]);}
  }
  printf("\n");
  m=d;
 }
 k:;
}

}

 

原创粉丝点击