在C中绘制一个ASCII艺术圣诞树

loading...


1

我想使用带参数的函数在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;
    }
}

loading...

3回答

  • 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;
        }
    

  • 0

    代码中的问题如下:

    在函数 drawFoliage 中,您打算循环并为每一行调用 drawALineOfFoliage . 但是每次调用 drawALineOfFoliage 时,你实际上都会画出整棵树 .

    为了修复你的代码,只需在 drawAXMasTree 函数中替换 drawALineOfFoliagedrawALineOfFoliage ,如下所示:

    void drawAXmasTree()
    {
        int treeHeight = 0;
    
        getValidHeight(treeHeight); //read in a valid value for the tree height
        drawALineOfFoliage(treeHeight); 
        drawTrunk(treeHeight);          
    }
    

    Notice you don't need the second argument in drawALineOfFoliage since you don't actually use it.

    至于 drawFoliage ,你可以删除它 .


  • 1

    感谢您的帮助,我设法解决了这个问题,因为就像人们说它正在绘制整棵树而不仅仅是一条线 . 我所要做的就是将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;   
    }
    
评论

暂时没有评论!