📜  圣诞快乐(C语言中的圣诞树程序)

📅  最后修改于: 2021-05-25 21:58:43             🧑  作者: Mango

由于圣诞节就在门口,因此该以程序员的方式来庆祝圣诞节了。让我们在C中建立一棵装饰圣诞树。

为了印刷一棵圣诞树,我们正在印刷各种尺寸的金字塔,它们之间一个都在另一个之下。
对于装饰,在每个位置都印有一个随机字符。

高度和随机性可以调整。逐帧重复此操作,以产生真实事件的错觉。

例子:

让我们看一下代码。

// C program to print a  Christmas tree
// It is recommended is try it with a desktop 
// compiler like CodeBlocks.
#include 
#include 
#include 
#include 
  
#define RefRate 40000
#define randomness 5 // high means less random
  
// Clear the shell
void clrscr()
{
    system("@cls||clear");
}
  
// Print a random character giving preference 
// to *
void printRandLeaf()
{
    char leaftypes[5] = { '.', '*', '+', 'o', 'O' };
    int temp = rand() % randomness;
  
    // Giving preference to *
    if (temp == 1)
        printf("%c ", leaftypes[rand() % 5]);
    else
        printf("%c ", leaftypes[1]);
}
  
void triangle(int f, int n, int toth)
{
    int i, j, k = 2 * toth - 2;
  
    for (i = 0; i < f - 1; i++)
        k--;
  
    // number of rows
    for (i = f - 1; i < n; i++) {
  
        // space handler
        for (j = 0; j < k; j++)
            printf(" ");
  
        // decrementing k after each loop
        k = k - 1;
  
        // number of columns, printing stars
        for (j = 0; j <= i; j++)
            printRandLeaf();
  
        printf("\n");
    }
}
  
// Prints multiple triangles
void printTree(int h)
{
    int start = 1, stop = 0, diff = 3;
    while (stop < h + 1) {
        stop = start + diff;
        triangle(start, stop, h);
        diff++;
        start = stop - 2;
    }
}
  
// Prints bottom part.
void printLog(int n)
{
    int i, j, k = 2 * n - 4;
  
    for (i = 1; i <= 6; i++) {
  
        // space handler
        for (j = 0; j < k; j++)
            printf(" ");
  
        for (j = 1; j <= 6; j++)
            printf("#");
  
        printf("\n");
    }
}
  
// Driver code
int main()
{
    srand(time(NULL));
    int ht = 6;
  
    printf("\n*********MERRY CHRISTMAS*********\n\n");
  
    // refresh loop
    while (1) {
        clrscr();
        printTree(ht);
  
        printLog(ht);
        usleep(RefRate);
    }
  
    return 0;
}
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。