C ++圣诞树

时间:2014-04-02 19:53:24

标签: c++

我刚刚开始学习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方式。有什么帮助吗?

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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: #
_________#_________
________###________
_______#####_______
______#######______
_____#########_____
____###########____
___#############___
__###############__
_#################_
###################

答案 2 :(得分:0)

awesomeyi关于这个错误是对的,但是如果你想让它看起来像树一样&#34;&#34;你需要加倍你的循环。

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

答案 3 :(得分: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;
}