修复/改进自动换行功能

发布于 2024-11-03 12:07:53 字数 1298 浏览 1 评论 0原文

我有一个简单的自动换行函数,它接受一个长字符串作为输入,然后将该字符串分解为较小的字符串,并将它们添加到稍后输出的数组中。现在最后一两个词还没有输出。这是主要问题。不过,我也想改进这个功能。我知道这有点混乱。我想知道是否有更好的方法来解决这个问题。我认为该数组是不必要的,但我不知道还能怎么做。在数组填充了所有较小的字符串后,我将它们输出到文本文件中。任何建议将不胜感激。

这是自动换行函数:

void WordWrap(string inputString, string formatedAr[], const int SIZE)
{
    unsigned int index;
    unsigned int word;
    unsigned int max = 65;
    string outWord;
    string outLine;

    outWord = "";
    outLine = "";
    word = 0;

    for(int i = 0; i < SIZE; i++)
    {
        formatedAr[i] = "";
    }

    for(index = 0; index <= inputString.length(); index++)
    {
        if(inputString[index] != ' ')
        {
            outWord += inputString[index];
        }
        else
        {
            if(outLine.length() + outWord.length() > max)
            {
                formatedAr[word] = outLine;
                word++;
                outLine.clear();
            }
            outLine += outWord + " ";
            outWord.clear();
        }
    }
    formatedAr[word] = outLine;
}

这是我调用该函数并输出数组的地方:

WordWrap(dvdPtr -> synopsis, formatedAr, SIZE);

index = 0;

while(index < SIZE && formatedAr[index] != "")
{
    outFile << formatedAr[index] << endl;
    index++;
}

I have a simple word wrap function that takes a long string as an input and then breaks that string into smaller strings and adds them to an array to be outputted later. Right now the last word or two isn't outputting. That's the main problem. However, I would also like to improve the function. I know it's kind of messy. I was wondering if there are any better ways of solving this problem. I think the array is unnecessary but I don't know how else to do it. After the array is filled with all the smaller strings, I just output them to a text file. Any suggestions would be greatly appreciated.

Here's the Word Wrap function:

void WordWrap(string inputString, string formatedAr[], const int SIZE)
{
    unsigned int index;
    unsigned int word;
    unsigned int max = 65;
    string outWord;
    string outLine;

    outWord = "";
    outLine = "";
    word = 0;

    for(int i = 0; i < SIZE; i++)
    {
        formatedAr[i] = "";
    }

    for(index = 0; index <= inputString.length(); index++)
    {
        if(inputString[index] != ' ')
        {
            outWord += inputString[index];
        }
        else
        {
            if(outLine.length() + outWord.length() > max)
            {
                formatedAr[word] = outLine;
                word++;
                outLine.clear();
            }
            outLine += outWord + " ";
            outWord.clear();
        }
    }
    formatedAr[word] = outLine;
}

And this is where I call the function and output the array:

WordWrap(dvdPtr -> synopsis, formatedAr, SIZE);

index = 0;

while(index < SIZE && formatedAr[index] != "")
{
    outFile << formatedAr[index] << endl;
    index++;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

起风了 2024-11-10 12:07:53

这是一个示例代码。

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void WordWrap(const string& inputString, vector<string>& outputString, unsigned int lineLength)
{
   istringstream iss(inputString);

   string line;

   do
   {
      string word;
      iss >> word;

      if (line.length() + word.length() > lineLength)
      {
         outputString.push_back(line);
         line.clear();
      }
      line += word + " ";

   }while (iss);

   if (!line.empty())
   {
      outputString.push_back(line);
   }
}

/*
A simple test:

Input string: "aaa bbb ccccccc dddd d111111111111111 33333 4444444444 222222222  ajdkjklad 341343"

Length per line: 20

Output lines of strings:

Line 1: aaa bbb ccccccc dddd
Line 2: d111111111111111
Line 3: 33333 4444444444
Line 4: 222222222  ajdkjklad
*/

Here is an example code.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void WordWrap(const string& inputString, vector<string>& outputString, unsigned int lineLength)
{
   istringstream iss(inputString);

   string line;

   do
   {
      string word;
      iss >> word;

      if (line.length() + word.length() > lineLength)
      {
         outputString.push_back(line);
         line.clear();
      }
      line += word + " ";

   }while (iss);

   if (!line.empty())
   {
      outputString.push_back(line);
   }
}

/*
A simple test:

Input string: "aaa bbb ccccccc dddd d111111111111111 33333 4444444444 222222222  ajdkjklad 341343"

Length per line: 20

Output lines of strings:

Line 1: aaa bbb ccccccc dddd
Line 2: d111111111111111
Line 3: 33333 4444444444
Line 4: 222222222  ajdkjklad
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文