C++ christmas tree
C++ christmas tree
我刚刚开始学习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;
}
相关文章:
- 使用 boost::p roperty_tree::p tree 如何获取特定键的值
- Boost R-Tree中的最小边界矩形计算
- C++ EXC_BAD_ACCESS (Binary Tree)
- Boost::p tree - 访问列表中包含的属性树节点
- Deserilizing n-ary tree
- Balancing KD Tree
- 如何填充'Tree'结构'Declaratively'
- 为什么qmap使用skiplist而不是ob rb-tree
- 避免在节点中添加重复的子级,使用 Kasper Peeters 的 Tree.hh 库
- SPOJ GSS1 WA - Segment tree
- Flattening the tree
- C++ AVL Tree Deletion
- RedBlack and AVL tree c++
- C++中已经有为 LinkedList、HashMap、Tree 等编写的类
- boost::p roperty_:tree - 解析和处理数据
- making tree with unordered_map
- AVL Tree in C++ Exception
- 使用 boost::p roperty_tree::p tree 将注释写入 ini 文件
- C++将具有非常相关数据的类 Tree 拆分为两个类
- C++ christmas tree