【问题标题】:my task is to print a string inside a frame for example a square我的任务是在框架内打印一个字符串,例如一个正方形
【发布时间】:2018-01-01 09:18:29
【问题描述】:
#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";
    }
} 

该程序在正方形内显示字符串,但正方形的星形图案变得混乱,尤其是最右边的列 任何帮助将不胜感激

【问题讨论】:

  • 如果您查看正在打印字符串的代码,请注意您开始在正方形中间打印字符串,但您没有考虑应该打印的字符串的长度。您需要计算中间相对于字符串的位置。另外,我建议实际使用 string 类而不是 char 数组,然后您可以使用 getline 来读取它。
  • 每个单词后面的星号都没有打印出来,这弄乱了最右边的列
  • 您能否编辑您的问题以解释问题到底出在哪里?预期的输出是什么?添加示例!
  • 是的,你能补充一些吗?这将极大地帮助您解决问题
  • 请不要大喊大叫

标签: c++ string loops nested nested-loops


【解决方案1】:

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

#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 *
*          *
*          *
*          *
*          *
************

【讨论】:

    【解决方案2】:

    主要问题在于:

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

    在这里,当您要放置输入字符串时,您还要输入一个新行并以星号 (*) 开头。您不仅没有将最后一个星号放在输入字符串的行上,而且您也没有在此处更新 j,它仍然大于 0,并且当代码以 for(j=0;j&lt;=n;j++) 继续时,j 已经有了换行符 + 星号的剩余值。

    试试:

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

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

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

    【讨论】:

    • 我喜欢堆栈溢出,感谢大家,你们都是救世主
    猜你喜欢
    • 1970-01-01
    • 2022-07-12
    • 2020-02-28
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多