C ++中的平行四边形使用while或Do / While

时间:2015-02-17 01:44:44

标签: c++ loops for-loop while-loop do-while

我的作业需要一个程序来创建特定大小和化妆的平行四边形。我需要使用至少一个for循环和一个while或do / while循环。到目前为止,我已经创建了一个程序来执行此操作:

Enter the symbol to use to draw your parallelogram: 
+
Enter the number of symbols to use for each side: 
5

+
++
+++
++++
+++++
++++
+++
++
+

很棒,但不是平行四边形。应该这样做:

+
++
+++
++++
+++++
+++++
+++++
+++++
+++++
_++++
__+++
___++
____+

忽略下划线 - 我只是用它们来创造空间。

我想我需要创建一个while或do / while循环,说“一旦你达到了指定的数字(在上面的情况下,5),打印该行5次。”然后我需要创建另一个for循环,它说:“一旦你完成了那个或者做/ while循环,开始从左侧减去字符,直到你回到一个。” 有人能告诉我如何做到这一点,或者即使这个想法是好的吗?我目前的代码如下。 // while(row = length)位是我一段时间的开始尝试,这就是为什么我现在把它作为评论。提前谢谢!

#include <iostream>
#include <cstdlib>
using namespace std;

int main () {
    int length;
    char symb;

    cout <<"Enter the symbol to use to draw your parallelogram: "<<     endl;
    cin >> symb;
    cout <<"Enter the number of symbols to use for each side: "<< endl;
    cin >> length;

    int row, col;
    for (row=0; row<=length; row++) //Up and over row
    {
        for (col=0; col<=row; col++) //Up and over column
            cout << symb;
        cout << endl;
    }
    //while (row=length)
    //{


    for (row=length; row>=1; row--) //Down and over row
    {
        for (col=row; col>=1; col--) //Down and over column
            cout << symb;
        cout << endl;
    }


}

0 个答案:

没有答案