C++ christmas tree

C++ christmas tree

本文关键字:tree christmas C++      更新时间:2023-10-16

我刚刚开始学习C++,并决定通过创建一个简单的圣诞树程序来给自己一个挑战。到目前为止,一切都在工作,尽管代码没有产生我期望的结果。我花了相当多的时间试图找出问题所在,但似乎无法弄清楚为什么它不起作用。

到目前为止,代码如下所示:

#include <iostream>
#include <string>
int main(){
 char cTree;
 int iSize, iSpace;
 std::cout << "Christmas Tree Application" << std::endl;
 std::cout << "Enter the size of your christmas tree: ";
 std::cin >> iSize;
 std::cout << "Enter the character you would like to use for your tree: ";
 std::cin >> cTree;
 iSpace = iSize / 2;
 for(int i = 0; i < iSize; i++){
    std::string sTree(i, cTree);
    //std::string sSpace(iSpace, ' ');
    std::cout << sTree << std::endl;
    //iSpace -= 1;
 }
 return 0;
}

我还注释掉了"间距"部分,因为每当我执行它时,它都会创建一个错误,我不知道为什么会这样说。我知道我可以用不同的方式做间距,但我想使用 std::string 的方式。有什么帮助吗?

iSpace设置为 iSize / 2 ,但您运行循环 iSize 次,每次iSpace减少一次。最终,iSpace将是负数,这就是您出现错误的原因。试试这个:

iSpace = iSize;
for(int i = 0; i < iSize; i++){
   std::string sTree(i, cTree);
   std::string sSpace(iSpace, ' ');
   std::cout << sSpace << sTree << sTree << std::endl;
   iSpace -= 1;
}

你循环了错误的iSpace值,因为它在整个执行过程中保持不变,而不是像变量那样变化。

此外,您的代码可以稍微整理一下。我已经在下面发布了更新的代码。我特意将空格替换为下划线字符,以更好地表示我的逻辑。

祝你好运!

代码清单


#include <iostream>
#include <string>
using namespace std;
int main(void) {
   char cTree;
   int iSize;
   int iSpace;
   int iChars;
   cout << "Christmas Tree Application" << endl;
   cout << "Enter the height of your christmas tree: ";
   cin >> iSize;
   cout << "Enter the character you would like to use for your tree: ";
   cin >> cTree;
   for(int i = 0; i < iSize; i++){
      iSpace = (iSize-i)-1;
      iChars = (2*i)+1;
      string sSpace(iSpace, '_');
      string sTree(iChars, cTree);
      cout << sSpace << sTree << sSpace << endl;
   }
   return 0;
}

示例运行


Christmas Tree Application
Enter the height of your christmas tree: 10
Enter the character you would like to use for your tree: #
_________#_________
________###________
_______#####_______
______#######______
_____#########_____
____###########____
___#############___
__###############__
_#################_
###################

awesomeyi 对错误是正确的,但是如果您希望它看起来更"树状",则需要加倍循环。

#include <iostream>
#include <string>
int main(){
    char cTree;
    int iSize, iSpace;
    std::cout << "Christmas Tree Application" << std::endl;
    std::cout << "Enter the size of your christmas tree: ";
    std::cin >> iSize;
    std::cout << "Enter the character you would like to use for your tree: ";
    std::cin >> cTree;
    iSpace = iSize;
    for (int i = 0; i < iSize*2; i+=2){
        std::string sTree(i, cTree);
        std::string sSpace(iSpace, ' ');
        std::cout << sSpace + sTree + sSpace<< std::endl;
        iSpace -= 1;
    }
    return 0;
}

只是为了好玩,这里有一个递归实现:

#include <iostream>
#include <string>
#include <cstdio>
void drawTree(int level, int height, char c) {
        if(level > 0)
                drawTree(level-1, height, c);
        std::cout << std::string(height-level, ' ') << std::string((2*level)+1, c) << std::endl;
}
int main(){
        char cTree;
        int iSize;
        std::cout << "Christmas Tree Application" << std::endl;
        std::cout << "Enter the size of your christmas tree: ";
        std::cin >> iSize;
        std::cout << "Enter the character you would like to use for your tree: ";
        std::cin >> cTree;
        drawTree(iSize, iSize, cTree);
        return 0;
}