fmt

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 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.

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秒64M0
ple 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:;
}

}

 

### 关于 FMT 的学习资料和教程 #### 什么是 FMT? `fmt` 是一个现代化的 C++ 格式化库,旨在提供高效、安全且易于使用的字符串格式化功能。它类似于 Python 中的 `str.format()` 方法或 C# 中的 `String.Format()` 函数[^1]。 --- #### 基础概念 `fmt` 提供了一种替代传统 C 风格 `printf` 和 C++ 风格流操作符的方式来进行字符串格式化。它的主要特点包括: - **高性能**:相比传统的 `std::ostringstream` 或 `sprintf` 更加高效。 - **安全性**:避免了常见的缓冲区溢出问题。 - **易用性**:提供了直观的 API 设计,支持复杂的格式化需求。 以下是几个核心组件: - **format()**: 主要用于生成格式化的字符串。 - **print()**: 将格式化后的结果直接打印到标准输出或其他目标设备。 - **string_view**: 支持高效的只读字符串视图,减少不必要的拷贝操作。 --- #### 安装与配置 如果需要在项目中使用 `fmt` 库,可以按照以下步骤安装: ##### Linux 下安装 可以通过源码方式安装 `fmt` 库。假设下载的是 `fmt-8.1.1.tar.gz` 文件,则执行以下命令完成解压和构建过程[^3]: ```bash tar -zxvf fmt-8.1.1.tar.gz cd fmt-8.1.1 mkdir build && cd build cmake .. make sudo make install ``` ##### 使用 CMake 导入 对于基于 CMake 构建的项目,可以直接通过 `find_package(fmt REQUIRED)` 来引入该库,并链接至目标模块。 --- #### 示例代码 下面是一些简单的例子展示如何使用 `fmt` 进行字符串格式化: ##### 基本用法 ```cpp #include <fmt/core.h> int main() { int value = 42; fmt::print("The answer is {}.\n", value); // 输出: The answer is 42. } ``` ##### 复杂格式化 ```cpp #include <fmt/format.h> #include <string> int main() { double pi = 3.14159; std::string name = "World"; auto formatted_str = fmt::format( "Hello, {}, the approximate value of Pi is {:.2f}.", name, pi ); fmt::print("{}\n", formatted_str); // 输出: Hello, World, the approximate value of Pi is 3.14. } ``` ##### 自定义类型的支持 `fmt` 可以轻松扩展以支持自定义类型的格式化: ```cpp #include <fmt/ostream.h> // 启用 ostream 扩展 struct Point { double x, y; }; // 实现 operator<< 即可自动适配 fmt std::ostream& operator<<(std::ostream& os, const Point& p) { return os << '(' << p.x << ", " << p.y << ')'; } int main() { Point pt{1.0, 2.0}; fmt::print("Point coordinates are {}\n", pt); // 输出: Point coordinates are (1, 2). } ``` --- #### Go 语言中的 fmt 包对比 虽然题目提到的内容也涉及到了 Go 语言的标准库 `fmt` 包[^2],但它与 C++ 的 `fmt` 并无关联。Go 的 `fmt` 主要是用来处理基本的数据格式化以及输入输出操作,而 C++ 的 `fmt` 则是一个独立开发的第三方库,专注于性能优化和灵活性。 例如,在 Go 中实现类似的浮点数格式化可能如下所示: ```go package main import ( "fmt" ) func main() { f := 3.14159 fmt.Printf("Scientific notation with lowercase e: %e\n", f) fmt.Printf("Fixed-point decimal representation: %f\n", f) fmt.Printf("Compact format based on context: %g\n", f) } ``` 尽管两者名称相同,但在设计哲学和技术细节上有显著差异。 --- #### 推荐的学习资源 为了更好地掌握 `fmt` 库的功能及其应用场景,建议参考以下文档和教程: 1. [官方文档](https://fmt.dev/latest/index.html): 最权威的信息来源,涵盖了从入门到高级的所有主题。 2. GitHub 仓库: https://github.com/fmtlib/fmt —— 查看最新动态及示例程序。 3. Stack Overflow 上的相关问答: 许多开发者分享了自己的实践经验,可以帮助解决实际遇到的问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值