输出行到控制台的宽度约束

时间:2014-01-31 18:03:14

标签: c++

Class将字符串输出到控制台。 如何使输出行的宽度等于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;
         }
      }

1 个答案:

答案 0 :(得分:0)

以下是适合我的答案

#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;
};