对于循环重复绘制问题

For loop repetition drawing trouble

本文关键字:绘制 问题 循环 于循环      更新时间:2023-10-16

我试图让一个循环来画一棵圣诞树,但输出是错误的,我尝试寻找答案,但我似乎找不到它,我被难住了。答案可能很明显,但我错过了很多,非常感谢任何帮助!

#include <iostream>
#include <assert.h>
#include <iomanip>
using namespace std;
const char blank = ' ';
const char leaf = '#';
const char wood = '|';
const int minSize = 4;
const int maxSize = 20;
int treeHeight;
int& getValidHeight(int&);
void drawALineOfFoliage(int);
void drawFoliage(int);
void drawTrunk(int);
void drawAXmasTree(int);
void drawAXmasTree(int treeHeight) {
getValidHeight(treeHeight);
drawFoliage(treeHeight);
drawTrunk(treeHeight);
}
int& getValidHeight(int& treeHeight) {
cout << ("Please enter the size of the tree (4-20):n");
cin >> treeHeight;
while ((treeHeight < minSize) || (maxSize < treeHeight)) {
cout << "ERROR: Invalid height! Enter the size of the tree (4-20):n";
cin >> treeHeight;
return treeHeight;
}
}
void drawALineOfFoliage(int treeHeight) {

for (int x = 0; x < treeHeight; ++x){
for (int y = treeHeight; y > x; --y){
cout << blank;}
for (int y = 0; y < x; ++y){
cout << leaf;}}}
void drawFoliage(int treeHeight) {
int branchLine = 1;
do {
drawALineOfFoliage(treeHeight);
branchLine += 1;
} while (branchLine <= (treeHeight - 2));}
void drawTrunk(int treeHeight) {
int trunkLine(1), spaces;
while (trunkLine <= 2) {
spaces = 1;
while (spaces <= (treeHeight - 3)) {
cout << blank;
spaces += 1;}
cout << wood << "n";
trunkLine += 1;
}
}

int main()
{
drawAXmasTree(treeHeight);
system("pause");}

输出只是解构的圣诞树,所以所有 关卡在同一条线上并重复多次

所以我拿了你的示例代码并试了一下。

首先,您的样式和制表符不一致,这会使代码非常难以阅读。

接下来,你的drawALineOfFoliage实际上是绘制没有树干的整棵树,而不仅仅是一条线。因此,在其他 2 个嵌套的 for 循环之后,您在主 for 循环中缺少一个cout << endl;

开始编辑:

编辑:我忘了谈论半棵树。

因此,在现有代码中,它只打印树的一半。像这样的东西...

#
##
###
####

这是半棵圣诞树。为了使它看起来像一棵真正的树,我所做的只是将*2添加到负责打印叶子的 for 循环中。(您也可以改为cout << leaf << leaf;)

for (int y = 0; y < x*2; ++y) {
cout << leaf;
}

结束编辑。

由于您的drawALineOfFoliage已经在打印树,因此在drawFoliage中,

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

这个 do while 循环正在循环树木的数量(同样,没有树干),所以应该删除它。

现在我们已经完成了顶部,让我们来看看后备箱。

while (spaces <= (treeHeight - 3))

- 3似乎凭空而来。而且它每行只打印一个|,看起来有点奇怪,所以我删除了- 3,让它打印 2 块木头。

现在输出看起来有点像这样...

Please enter the size of the tree (4-20):
6
##
####
######
########
##########
||
||

没事,但仍然很奇怪。TLDR,我做了一些调整,得到了这样的最终结果......

Please enter the size of the tree (4-20):
7
#
###
#####
#######
#########
###########
#############
|||
|||

完整代码如下:

#include <iostream>
#include <assert.h>
#include <iomanip>
using namespace std;
const char blank = ' ';
const char leaf = '#';
const char wood = '|';
const int minSize = 4;
const int maxSize = 20;
int treeHeight;
int& getValidHeight(int&);
void drawALineOfFoliage(int);
void drawFoliage(int);
void drawTrunk(int);
void drawAXmasTree(int);
void drawAXmasTree(int treeHeight) {
getValidHeight(treeHeight);
drawFoliage(treeHeight);
drawTrunk(treeHeight);
}
int& getValidHeight(int& treeHeight) {
cout << ("Please enter the size of the tree (4-20):n");
cin >> treeHeight;
while ((treeHeight < minSize) || (maxSize < treeHeight)) {
cout << "ERROR: Invalid height! Enter the size of the tree (4-20):n";
cin >> treeHeight;
}
return treeHeight;
}
void drawALineOfFoliage(int treeHeight) {
for (int x = 0; x < treeHeight; ++x) {
for (int y = treeHeight; y > x; --y) {
cout << blank;
}
for (int y = 0; y < x*2; ++y) {
cout << leaf;
}
cout << endl;
}
}
void drawALineOfFoliageOdd(int treeHeight) {
for (int x = 0; x < treeHeight; ++x) {
for (int y = treeHeight; y > x; --y) {
cout << blank;
}
cout << leaf;
for (int y = 0; y < x*2; ++y) {
cout << leaf;
}
cout << endl;
}
}
void drawFoliage(int treeHeight) {
int branchLine = 1;
drawALineOfFoliageOdd(treeHeight);
do {
branchLine += 1;
} while (branchLine <= (treeHeight - 2));
}
void drawTrunk(int treeHeight) {
int trunkLine(1), spaces;
while (trunkLine <= 2) {
spaces = 1;
while (spaces <= (treeHeight - 1)) {
cout << blank;
spaces += 1;
}
cout << wood << wood << wood << endl;
trunkLine += 1;
}
}
int main() {
drawAXmasTree(treeHeight);
system("pause");
}

注意:我没有进行任何形式的清理,因此其中有很多不必要的代码。我只是在你必须为你提供我得到的最佳解决方案的基础上工作。

您的lineOfFoliage似乎为所有x < height做行。 在我看来,树叶绘制代码的基本结构是:

drawFoliage(height) {
for(width = 0..height) cout << centeredLine(width);
}
drawCenteredLine(width) {
return blanks + leafs;
}