Я хочу нарисовать ASCII-художественную елку в C ++, используя функции с параметрами.
Это то, что я получил до сих пор, но это работает по большей части, однако при рисовании фактического тела дерева(уходит), кажется, повторяет это несколько раз.
Этот эффект, похоже, увеличивается по мере увеличения высоты дерева.Так, например, если для дерева введена высота 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;
}
}