在C ++中绘制ASCII艺术的圣诞树

时间:2018-12-10 12:36:20

标签: c++ function

我想使用带有参数的函数在C ++中绘制ASCII艺术的圣诞树。

到目前为止,这是我所能获得的,在大多数情况下都可以使用,但是在绘制树的实际主体(叶子)时,似乎要重复多次。

这种效果似乎随着树的高度增加而增加。因此,例如,如果为树输入4的高度,则将绘制主体2次。如果高度为5,则绘制3次。 6是4倍,依此类推。

有帮助吗?

#include <iostream>
using namespace std;

const char BLANK = ' ';     
const char LEAF = '#';      
const char WOOD = '|';      

void drawAXmasTree();
void drawFoliage(int);
void drawTrunk(int);
void getValidHeight(int&);
void drawALineOfFoliage(int, int);

int main()
{
    cout << "Due on 11 December 2018 \n\n";
    drawAXmasTree();
}

void drawAXmasTree()
{
    int treeHeight = 0;

    getValidHeight(treeHeight); //read in a valid value for the tree height
    drawFoliage(treeHeight);            //draw tree foliage
    drawTrunk(treeHeight);          //draw tree trunk
}

void drawFoliage(int trHgt) //draw the foliage
{
    int branchLine = 1;
    int treeHeight = trHgt;

    while (branchLine <= (trHgt - 2))
    {
        drawALineOfFoliage(treeHeight, branchLine); 
        branchLine += 1;
    }
}

void drawTrunk(int trHgt)   //draw the trunk
{
    int trunkLine = 1;
    int spaces;

    while (trunkLine <= 2)  // for each line in the truck
    {
        spaces = 1;

        while (spaces <= (trHgt - 2))   //draw the spaces on the left
        {
            cout << BLANK;
            spaces += 1;
        }

        cout << WOOD;       //draw the truck
        cout << endl;           //go to next line
        trunkLine += 1;
    }
}

void getValidHeight(int& trHgt)
{
    do
    {
        cout << "Enter the size of the tree(4 - 20): ";
        cin >> trHgt;
        if (trHgt < 4 || trHgt > 20)
        {
            cout << "ERROR: Invalid height! ";
        }
    } 
    while (trHgt < 4 || trHgt > 20);
}

void drawALineOfFoliage(int trHgt, int brLine)
{
    int treeHeight = trHgt;
    int branchLine = brLine;
    int spaces = trHgt - 2;

    for (int i = 0; i < (treeHeight - 2); i++) {
        for (int j = spaces; j > 0; j--)
        {
            cout << BLANK;
        }
        for (int foliage = 0; foliage <= i * 2; foliage++)
        {
            cout << LEAF;
        }
        spaces--;
        cout << endl;
    }
}

3 个答案:

答案 0 :(得分:1)

您的代码中的问题如下:

在功能drawFoliage中,您打算循环并为每一行调用drawALineOfFoliage。但是实际上,每次调用drawALineOfFoliage时,您都会绘制整棵树。

为了修复代码,只需在drawFoliage函数中将drawALineOfFoliage替换为drawAXMasTree,如下所示:

void drawAXmasTree()
{
    int treeHeight = 0;

    getValidHeight(treeHeight); //read in a valid value for the tree height
    drawALineOfFoliage(treeHeight); 
    drawTrunk(treeHeight);          
}

请注意,您实际上并没有使用drawALineOfFoliage中的第二个参数。

对于drawFoliage,您只需删除它即可。

答案 1 :(得分:0)

感谢您的帮助,我设法解决了这个问题,因为就像人们所说的那样,它绘制的是整棵树,而不只是一条线。我要做的就是将drawALineOfFoliage函数更改为以下内容;

void drawALineOfFoliage(int trHgt, int brLine)
{
    int treeHeight = trHgt;
    int branchLine = brLine - 1;
    int spaces = trHgt - 2;

    for (int i = spaces; i > branchLine; i--)
    {
        cout << BLANK;
    }
    for (int foliage = 0; foliage <= branchLine * 2; foliage++)
    {
        cout << LEAF;
    }
    spaces--;
    cout << endl;   
}

答案 2 :(得分:0)

检查一下:

    #include <iostream>
    using namespace std;

    const char BLANK = ' ';
    const char LEAF = '#';
    const char WOOD = '|';

    void drawAXmasTree();
    void drawFoliage(int);
    void drawTrunk(int);
    void getValidHeight(int&);
    void drawALineOfFoliage(int, int);

    int main()
    {
        cout << "Due on 11 December 2018 \n\n";
        drawAXmasTree();

        system("pause");
    }

    void drawAXmasTree()
    {
        int treeHeight = 0;

        getValidHeight(treeHeight); 
        drawFoliage(treeHeight);             
        drawTrunk(treeHeight);          
    }

    void drawFoliage(int trHgt) 
    {
        int branchLine = 1;
        int treeHeight = trHgt;

        while (branchLine <= (trHgt - 2))
        {
            drawALineOfFoliage(treeHeight, branchLine);
            branchLine += 1;
        }
    }

    void drawTrunk(int trHgt)   
    {
        int trunkLine = 1;
        int spaces;

        for (trunkLine; trunkLine <= 2; trunkLine++)
        {
            for (spaces = 1; spaces <= (trHgt - 1); spaces++)
            {
                cout << BLANK;
            }
            cout << WOOD;
            cout << "\n";

        }
    }

    void getValidHeight(int& trHgt)
    {
        do
        {
            cout << "Enter the size of the tree(4 - 20): ";
            cin >> trHgt;
            if (trHgt < 4 || trHgt > 20)
            {
                cout << "ERROR: Invalid height! ";
            }
        } while (trHgt < 4 || trHgt > 20);
    }

    void drawALineOfFoliage(int trHgt, int brLine)
    {
        int treeHeight = trHgt;
        int branchLine = brLine;
        int spaces(0);
        int tree(0);

        for (spaces; spaces < (trHgt - branchLine); spaces++)
        {
            cout << BLANK;
        }
        for (tree; tree < (branchLine * 2 - 1); tree++)
        {
            cout << LEAF;
        }
        cout << endl;
    }