我的任务是在一个框架内打印一个字符串,例如一个正方形

问题描述 投票:-3回答:2
#include<iostream>
using namespace std;
int main()
{
    int i=1,len;
    char ch[26][26],ch2;
    cout<<"enter string: "<<endl;   
    for(i=0;;i++)
    {
        cin>>ch[i];
        len++;
        if(getchar()=='\n')
        break;
    }
    int n,j;
    cout<<"enter size: "<<endl;
    cin>>n;
    int k;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=n;j++)
        {
            if(i==0||i==n||j==0||j==n)
            {
                cout<<"*";
            }
            else
                cout<<" ";
            if(i==((n/2)-2)&&j==((n/2)-2))
            {
                for(k=0;k<len;k++)
                {
                    cout<<ch[k]<<endl;
                    cout<<"*";
                }
            }
        }   
        cout<<"\n";
    }
} 

这个程序在正方形内显示字符串,但是正方形的星形模式特别混淆了最右边的列任何帮助都会受到极大的赞赏

c++ string loops nested nested-loops
2个回答
0
投票

主要问题在于:

for(k=0;k<len;k++)
{
    cout<<ch[k]<<endl;
    cout<<"*";
}

在您输入输入字符串时,您还要输入一个新行并以星号(*)开头。你不仅没有把最后一个星号放在输入字符串的行上,而且你也没有更新j,它仍然大于0,当代码继续使用for(j=0;j<=n;j++)时,j已经有来自换行符+星号的剩余值。

尝试:

for( k = 0; k<len; k++ )
{
    cout << ch[k];
    j += strlen( ch[k] );
}

这样j将更新到输入字符串的最后位置。

PS:对于常见的编码实践,也将len的开头初始化为0。


0
投票

由于您没有在代码中提供太多详细信息,因此我从一开始就使用了新代码,这就是我想出的:

#include <iostream>
#include <vector>

使用矢量为您的字符串,动态调整大小(如果,在您的代码中,我输入超过26个单词?提示:分段错误!)

using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;

最好避免使用using namespace std;。只需导入您真正需要的东西。

int main() {
    vector<string> strings;

你肯定想在这里使用字符串,而不是char数组。

    cout << "Enter string: ";

输入提示后不要断行! (作为Linux用户,我个人讨厌它)

    for(;;) {

你不需要这里的变量i,只需运行一个无限循环(尝试重新排列,如果你可以避免无限循环再一次,while(getchar() != '\n')更加不言自明。

        string s;
        cin >> s;
        strings.push_back(s);

正如pstrjds在评论中建议的那样,如果可以,请使用getline()

        if(getchar() == '\n')
            break;

就像我说的那样,尝试用while条件重新制定。

    }
    unsigned int n, i, j;
    cout << "Enter size: ";
    cin >> n;

    // assuming strings.size() < n
    unsigned int empty_lines_around_text((n - strings.size()) / 2);

由于你想在正方形内打印你的单词,你必须显示不到半平方的* (...) *线:实际上是半个正方形减去要打印的字符串数量的一半。

    // first horizontal row of stars
    for(j = 0; j < n; ++j)
        cout << '*';
    cout << endl;

广场的上方。

    for(i = 1; i < empty_lines_around_text; ++i) {
        cout << '*';
        for(j = 1; j < n - 1; ++j) {
            cout << ' ';
        }
        cout << '*' << endl;
    }

第一行打印,其中没有字符串。

    //here we do the actual printing of the strings
    for(i = 0; i < strings.size(); ++i) {
        string s = strings[i];

        // once again, assuming the size of each string is < n
        unsigned int empty_chars_around_string((n - s.size()) / 2);
        cout << '*';
        for(j = 0; j < empty_chars_around_string; ++j)
            cout << ' ';
        cout << s;
        for(j = empty_chars_around_string + s.size() + 1; j < n - 1; ++j)
            cout << ' ';
        cout << '*' << endl;
    }

这是有问题的部分。就像空行一样,我们需要一个变量来包含我们在字符串之前打印多少空格,使它看起来居中(变量empty_chars_around_string)。 我们打印那么多空格,字符串,然后我们在行结尾*之前用空格填充行,这对于数组中的每个字符串都是如此。

    for(i = empty_lines_around_text + strings.size() + 1; i < n; ++i) {
        cout << '*';
        for(j = 1; j < n - 1; ++j) {
            cout << ' ';
        }
        cout << '*' << endl;
    }

在打印完字符串后,我们用空行完成正方形。

    // last horizontal line of '*' (we close the square)
    for(j = 0; j < n; ++j)
        cout << '*';
    cout << endl;

... Aaand我们关闭广场。

return 0;
}

现在,这段代码并不完美,有一堆重构和优化要做,但它最大限度地利用了C ++特性。

Here是一个包含整个代码的PasteBin。

使用字符串Hello friends和大小12运行时的输出:

************
*          *
*          *
*          *
*          *
*   hello  *
*  friends *
*          *
*          *
*          *
*          *
************
© www.soinside.com 2019 - 2024. All rights reserved.