尝试用C++编写Word Wrap程序



好吧,所以我正在尝试制作一个单词包装程序,以便在我未来的程序和游戏尝试中实现,让自己更容易编写故事部分。我不完全确定出了什么问题,我正在做我认为最好的方法,但它似乎有一些问题,我不知道如何解释。有人能告诉我我做错了什么吗?

这是代码

#include <iostream>
#include <string>
using namespace std;
void wordWrapPrinting(string a);
int main()
{
     cout << "Hello, my name is ________. This is a test of my word wrap program. If it works as      intended, it will correctly fix strings such as this one and make sure that none of the words are cut off and carried over to another line. This will make writing stories for games much easiser for both myself and Kevin, and maybe even the whole class. This was a request from _____ (sort of, he was complaining about it and I said I could probably make one). Hopefully this works.";
     cout << endl;
     cout << endl;
     cout << endl;
     cout << endl;
     cout << endl;
     wordWrapPrinting("Hello, my name is ________. This is a test of my word wrap program. If it works as intended, it will correctly fix strings such as this one and make sure that none of the words are cut off and carried over to another line. This will make writing stories for games much easiser for both myself and Kevin, and maybe even the whole class. This was a request from _____ (sort of, he was complaining about it and I said I could probably make one). Hopefully this works.");
}
void wordWrapPrinting(string a)
{
    int iCounter = 0;
    bool spaced = false;
    for(int i=0; i < a.size(); i++)
    {
        /*if(i == 81)
            spaced = false;
        else if(i == 161)
            spaced = false;
        else if(i == 241)
            spaced = false;
        else if(i == 321)
            spaced = false;
        else if(i == 401)
            spaced = false;*/
        if(a[i] != ' ')
        {
            if(i == 80 && a[i] != ' ' /*&& spaced == false*/)
            {
                while(a[i] != ' ')
                {
                    iCounter++;
                    i--;
                }
                if(a[i] == ' ')
                {
                    a[i] = 'n';
                    i += iCounter;
                    //spaced = true;
                }
            }
            else if(i == 160 && a[i] != ' ' /*&& spaced == false*/)
            {
                while(a[i] != ' ')
                {
                    iCounter++;
                    i--;
                }
                if(a[i] == ' ')
                {
                    a[i] = 'n';
                    i += iCounter;
                    //spaced = true;
                }
            }
            else if(i == 240 && a[i] != ' ' /*&& spaced == false*/)
            {
                while(a[i] != ' ')
                {
                    iCounter++;
                    i--;
                }
                if(a[i] == ' ')
                {
                    a[i] = 'n';
                    i += iCounter;
                    //spaced = true;
                }
            }
            else if(i == 320 && a[i] != ' ' /*&& spaced == false*/)
            {
                while(a[i] != ' ')
                {
                    iCounter++;
                    i--;
                }
                if(a[i] == ' ')
                {
                    a[i] = 'n';
                    i += iCounter;
                    //spaced = true;
                }
            }
            else if(i == 400 && a[i] != ' ' /*&& spaced == false*/)
            {
                while(a[i] != ' ')
                {
                    iCounter++;
                    i--;
                }
                if(a[i] == ' ')
                {
                    a[i] = 'n';
                    i += iCounter;
                    //spaced = true;
                }
            }
            else if(i == 480 && a[i] != ' ' /*&& spaced == false*/)
            {
                while(a[i] != ' ')
                {
                    iCounter++;
                    i--;
                }
                if(a[i] == ' ')
                {
                    a[i] = 'n';
                    i += iCounter;
                    //spaced = true;
                }
            }
            else if(i == 560 && a[i] != ' ' /*&& spaced == false*/)
            {
                while(a[i] != ' ')
                {
                    iCounter++;
                    i--;
                }
                if(a[i] == ' ')
                {
                    a[i] = 'n';
                    i += iCounter;
                    //spaced = true;
                }
            }
        }
        else
            if(a[i] != ' ')
                if(i == 80 || i == 160 || i == 240 || i == 320 || i == 400 || i == 480 || i ==  560)
                    a[i] = 'n';
        /*if(((i >= 75 && i <=80) || (i >= 155 && i <=160) || (i >= 235 && i <=240) || (i >= 315 && i <= 320) || (i >= 375 && i <= 400) || (i >= 475 && i <= 480)) && a[i] != ' ' && spaced != false) 
        {
            while(a[i] != ' ')
                iCounter++;
            if(a[i] == ' ')
            {
                a[i] = 'n';
                spaced = true;
            }
        }
        else
            if(((i >= 75 && i <=80) || (i >= 155 && i <=160) || (i >= 235 && i <=240) || (i >= 315 && i <= 320) || (i >= 375 && i <= 400) || (i >= 475 && i <= 480)) && a[i] == ' ' && spaced != false)
            {
                a[i] = 'n';
                spaced = true;
            }*/
    }
    cout << a << endl;
}

正如其他人所说,把句子分解成单词。

如果行+sizeof(word)上的当前位置大于右边距,则处理该行并将新单词放在下一行(新行)的左边距。

如果你真的很擅长这一点,你可以拆分单词并使用连字符。

或者,如果你有更多的时间,你可以证明文本的合理性。

顺便说一句,如果你在一个窗口系统上,你必须将单词长度转换为像素长度。不同字体的单词有不同的宽度。很少有字体是固定空间。

最新更新