在这里,我们将看到一个有趣的问题。在这个问题中,我们将看到如何随机打印圣诞树。因此,树会像圣诞树灯一样闪烁。
为了打印圣诞树,我们将打印各种大小的金字塔,一个接一个地放置。对于装饰叶子,将从给定的字符列表中随机打印一个字符。高度和随机性是可调节的。
在生成树之后,整个屏幕被清除,然后再次生成,这就是为什么它看起来像是闪烁的树。
示例
#include #include #include #include #define REFRESH_RATE 40000#define RANDOM_NESS 5 // The higer value indicates less randomvoid clear_screen() { system("@cls||clear");}void display_random_leaf() { char type_of_leaves[5] = { '.', '*', '+', 'o', 'O' }; //these are the leaf types int temp = rand() % RANDOM_NESS; if (temp == 1) printf("%c ", type_of_leaves[rand() % 5]); //if temp is 1, then use other leaves else printf("%c ", type_of_leaves[1]); //otherwise print *}void tree_triangle(int f, int n, int toth) { int i, j, k = 2 * toth - 2; for (i = 0; i"); }}void display_tree(int h) { int start = 1, end = 0, diff = 3; while (end
"); }}main() { srand(time(NULL)); int ht = 15; while (1) { clear_screen(); display_tree(ht); display_log(ht); usleep(REFRESH_RATE); //use sleep before replacing }}
登录后复制
输出
以上就是C语言中的圣诞树程序的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582008.html