Я сделал программу на C++, которая рисует Рождественскую елку, но у меня есть некоторые проблемы с пнем. Я буквально не понимаю, почему он не хочет появляться посередине (в соответствии с вершиной дерева, чтобы все дерево было симметричным).Количество вычислений пробелов в цикле
Однако у меня есть вложенные петли, я рассчитываю правильные пробелы?
Вот то, что я до сих пор с точки зрения кода:
int treeHeight;
int main()
{
cout << "Enter the size of the tree (4-20): ";
cin >> treeHeight;
void getValidHeight();
getValidHeight();
// Set tree height to 2 lower than it is (part of spec I'm following)
treeHeight = treeHeight - 2;
// Set tree character
char leaf = '#';
// Set tree stump character
char treeStump = '|';
// First leaf (top of tree)
int leaves = 1;
int treeWidth = treeHeight/2;
int stumpHeight = 2;
for (int total = treeHeight; total > 0; --total)
{
//control the amount of spaces
for (int i = (total - 1); i > 0; --i)
cout << " ";
//control the amount of leaves
for (int j = 0; j < leaves; ++j)
{
cout << leaf;
}
// Next row needs 2 extra leaves
leaves += 2;
cout << '\n';
}
// Create the stump
for (int i = 0; i < stumpHeight; ++i)
{
// Spaces to the center of the tree so that the stump is centered
for (int j = 0; j < treeWidth; ++j)
cout << " ";
for (int k = 0; k < 1; ++k)
cout << treeStump;
cout << '\n';
}
system("pause");
return 0;
}
void getValidHeight()
{
while (treeHeight > 20 || treeHeight < 4)
{
cout << "\nEROOR: Invalid height! Enter the size of the tree (4-20): ";
cin >> treeHeight;
}
}
treeWidth вообще не используется при рисовании дерева, поэтому вряд ли это будет правильным для пня. – Dave