使用C++设计模式

3

我的教授给我分配了一个任务,即使用用户输入的字符串创建C++输出的模式。

程序应按以下方式运行:

Enter a string: ***

       ***
        ***
         ***
          ***     
           ***
***************
           ***
          ***
         ***
        ***
       ***

注意:用户输入的字符串的长度和字符数量都可以任意

以下是程序的限制:

  • cout 语句中不允许有字符串常量或空格。
  • 也禁止使用循环(这就是我被卡住的原因……我成功地创建了上面的程序,但是使用了循环)。
  • 不允许使用C++的高级概念(在学校里,我们只学习了语言如何工作的基本概念,请在回答时牢记这一点)。

我尝试了多种方法来创建上述程序,但由于给定的限制,我现在认为不可能了,所以我来到这里向社区求助。

以下是我使用循环的代码:

string userInput;
int m = 1, n = 9;
cout<<"\nEnter a three character string: ";
cin>>userInput; 
cout<<endl;
while (m <= 6)
      {
         if (m == 6)
           {                
        cout<<userInput<<userInput<<userInput<<userInput<<userInput<<endl; 
             m = 1;
             n = 13;
             while (m <= 5)
              {
                cout<<setw(n)<<userInput<<endl;
                m++;
                n--;
            }
            return 0; //this will finish the execution of the program
        }
        cout<<setw(n)<<userInput<<endl;
        n++;
        m++;
    }

以上程序仅适用于用户输入三个字符的字符串

非常感谢您的帮助!

对不起,我的英语不好,如果您发现任何错误或错误,请随意编辑和更正


你尝试了什么?使用循环展示你的代码... - OznOg
@Vivick,我们在课上还没有学习构造函数,教授告诉我们只能使用基本的C++逻辑和字符串以及iomanip头文件来设计上述代码。 - malik727
1
请发布您的代码。仅发布图像是不够的,因为它不能让人们轻松地测试您的代码。 - anatolyg
2
我已经对发布代码图片链接的问题进行了点踩。请[编辑]问题以包含实际代码。 - Martin Bonner supports Monica
@MartinBonner 我已按照您的要求添加了实际代码。 - malik727
显示剩余7条评论
3个回答

1

你可以使用称为递归函数的东西。这种方式不使用循环,而是使用递归,你只需要调用一次。

#include <iostream>
#include <string>

void coutLine(std::string output) {
    std::cout << output << '\n';
}
void recursiveWriter(std::string recursiveInput, int number, int iterator) {
    //Correct for even number of lines below and above
    number = number - (number % 2);

    //You should split this logic in another function, to keep it neat
    if (iterator < (number / 2)) {
        recursiveInput = std::string(1, ' ') + recursiveInput;
        coutLine(recursiveInput);
    } else if (iterator > (number / 2)) {
        //dividable by 2 triggers the middle line
        //iterator should be -1 because one time it has ran by the 'else'
        if (iterator - 1 > number / 2) {
            recursiveInput = recursiveInput.erase(0, 1);
        }

        coutLine(recursiveInput);
    } else {
        //Create the middle line
        coutLine(std::string(recursiveInput.length() + 1, '*'));
    }

    if (iterator < number) {
        iterator++;
        recursiveWriter(recursiveInput, number, iterator);
    }
}

我当然不知道所有具体的要求,但以下情况发生:

int main() {
    int lines = 11;
    int iterator = 0;
    recursiveWriter("***", lines, iterator);
}

//lines 10 and 11 generates:
 ***
  ***
   ***
    ***
     ***
*********
     ***
    ***
   ***
  ***
 ***

//lines 12 and 13 generates with input text *****:
 *****
  *****
   *****
    *****
     *****
      *****
************
      *****
     *****
    *****
   *****
  *****
 *****

所以这种方法使得上下行的行数始终相等。不过还有改进的空间。就像提到的一样,可能不符合要求(你没有非常具体地说明)。

1
谢谢你的回答,非常详细...它也给了我如何使用递归运算符的想法。 - malik727

0

你总是可以用递归来替换迭代。拿出你现有的代码,将循环转换为递归函数。我建议从最内层循环开始替换,然后逐步向外。

在我看来,这是递归的不良使用。递归对于处理自然递归结构(如树)以及当您可以通过分成两部分来解决问题时(如排序)非常有用。


谢谢你的回答,但我不熟悉递归。正如@Chris Pearce在他的回答中指出的那样,逻辑非常简单。 - malik727

0

由于需要重复输入字符串或空格的次数是固定的,因此您可以手动打印每个输入的重复。您可以使用库中的setw函数(请参见http://www.cplusplus.com/reference/iomanip/setw/)生成与输入字符串长度相匹配的空格。

以下是一个示例:

#include <iostream>
#include <iomanip>

int main() {
    std::string input = "test";
    std::cout << std::setw(input.length() * 3) << input << "\n";
    std::cout << input << input << input << "\n"; 
    std::cout << std::setw(input.length() * 3) << input << "\n";
    std::cout << "\n";
    return 0;
}

这将产生输出:

        test
testtesttest
        test

为了避免重复,您可以尝试使用递归。这可能不是您已经涵盖的概念,在这种情况下最好坚持第一个建议。如果您感兴趣,可以查找一下,因为它不涉及c++的高级功能。

1
这就是我一直在寻找的答案...谢谢伙计,你给了一个非常简单和基础的答案,我可以很容易地实现它。我理解了其中的逻辑,现在创建上述模式不会很困难了。 - malik727
@Gingitsune 然而,这虽然足够简单,但不能帮助您通过仅使用“线性规划”来创建“好”的代码。只是提醒一下要记住 =) - Revils
@Sliver2009 我知道,但我在问题中已经提到我的导师限制我只使用他教授的方法。我们是编程新手,将来会学习关于C++的高级方法。 - malik727
@Sliver2009 我完全同意。还有其他更有趣的选择,但在制作小模式时可能会过于复杂。尝试递归也是一个不错的练习。 - Chris Pearce
@Gingitsune 没问题 - Chris Pearce

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接