【问题标题】:Width constraint of the output lines to the console输出线到控制台的宽度约束
【发布时间】:2014-02-24 13:18:14
【问题描述】:

类将字符串打印到控制台。 如何使输出行的宽度等于characterWidth = 40, 即 40 个字符后转移到新行?

#include <string>
#include <iostream>

class StringProcessing {
  public:
      StringProcessing() : characterWidth(40),
                     textToBeFormatted("NULL") {}

      inline void StringProcessing::initString() {
          textToBeFormatted =
              "text    some text   some text some text   some text some text"
              "text some text  some text    some text some text some text"
              "text    some      text some    text some   text some text"
              "text some  text some    text some    text some text some text"
              "text   some     text some text some   text some text some text";
      }

      inline void displayString()
        { std::cout << textToBeFormatted << std::endl; }
  private:    
      int characterWidth;
      std::string textToBeFormatted;
};

我有一个想法但是这里控制台中的单词被截断了,所以需要转移到下一行并进行宽度对齐

inline void displayString()
      {
         const std::string& s = textToBeFormatted;
         for (int i = 0; i < s.length() / 40 + (bool)(s.length() % 40); ++i)
         {
            std::cout << std::left
                      << std::setfill(' ')
                      << std::setw(40)
                      << s.substr(i * 40, 40)
                      << std::endl;
         }
      }

【问题讨论】:

  • 还有什么问题?您是否试图证明文本的合理性?它让我想起了动态规划,这是一种“书例”:stackoverflow.com/q/18200593/1168156
  • 谢谢,我想这是我需要的

标签: c++


【解决方案1】:

这里是适合我的答案

#include <string>
#include <iostream>
#include <iomanip>

class StringProcessing
{
public:
    StringProcessing() : characterWidth(40),
        textToBeFormatted("NULL") {}

    inline void initString() {
        textToBeFormatted = "text    some text   some text some text   some text some text"
            "text some text  some text    some text some text some text"
            "text    some      text some    text some   text some text some text"
            "text some  text some    text some    text some text some text"
            "text   some     text some text some   text some text some text";
    }

    inline void displayString()
    {
        const std::string& s = textToBeFormatted;
        const int& width = characterWidth;
        for (int current = 0; current < s.length();)
        {
            if (s.length() < width)
            {
                output(s);
                break;
            }
            if (s.length() - current < width)
            {
                output(s.substr(current));
                break;
            }
            std::string substr = s.substr(current, width);
            current += width;
            size_t space = substr.rfind(' ');
            if (space != std::string::npos && (substr[width - 1] != ' ' &&
                (s.length() > current && s[current] != ' ')))
            {
                current -= width - space - 1;
                substr = substr.substr(0, space + 1);
            }
            output(substr);
        }
    }
private:
    inline void output(const std::string& s)
    {
        std::cout << setfill(' ') << std::right << std::setw(characterWidth) << s << std::endl;
    }
    int characterWidth;
    std::string textToBeFormatted;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多