最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語(yǔ)言實(shí)現(xiàn)一個(gè)閃爍的圣誕樹(shù)

 更新時(shí)間:2021年12月27日 15:55:26   作者:Linux猿  
本文詳細(xì)講解了C語(yǔ)言實(shí)現(xiàn)一個(gè)閃爍的圣誕樹(shù),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

圣誕節(jié)來(lái)啦!看到很多小伙伴用各種語(yǔ)言畫(huà)出了圣誕樹(shù),于是就想用 C 語(yǔ)言來(lái)畫(huà)一顆圣誕樹(shù),有點(diǎn)粗糙,下面先來(lái)看一下效果圖吧!

圖1 圣誕樹(shù)

下面來(lái)看下源碼,如下所示:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <stdbool.h>
 
#define N 15
char str[] = {'*', ' ', '@', ' ', '#', ' ', '\'',  ' ', '$', ' ', '%', ' ', '&', ' ', '!'};
 
void color(int a)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
 
void getCoord(double y, double x)
{
    COORD pos = { x,y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
 
void hideCursor()
{
    CONSOLE_CURSOR_INFO cursor= { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
}
 
void layer(int x, int y, int num, int col) {
    color(col);
    getCoord(x, y);
    int idx = rand()%N;
    printf("%c", str[idx]);
    for(int k = 1; k <= num; ++k) {
        idx = rand()%N;
        getCoord(x + k - 1, y);
        printf("%c", str[idx]);
        for(int i = 1; i <= (k*2-1)/2; i++) {
            getCoord(x + k - 1, y - i);
            idx = rand()%N;
            printf("%c", str[idx]);
            getCoord(x + k - 1, y + i);
            idx = rand()%N;
            printf("%c", str[idx]);
        }
    }
 
}
 
void triangle(int x, int y, int num, int col) {
    getCoord(x, y);
    color(col);
    printf("*");
 
    for(int i = 1; i <= num; ++i) {
            int x1 = x + i;
            int y1 = y - i;
        for(int j = 0; j < i * 2 + 1; ++j) {
            getCoord(x1, y1 + j);
            printf("*"); 
        }
    }
}
 
void triangleRight(double x, double y, double num, double col) {
    getCoord(x, y*2);
    color(col);
    printf("*");
 
    for(int i = 1; i <= num; ++i) {
            double x1 = x - i;
            double y1 = y - i;
        for(int j = 0; j < i * 2 + 1; ++j) {
            getCoord(x1 + j, y1 * 2);
            printf("*"); 
        }
    }
}
 
void triangleLeft(double x, double y, double num, double col) {
    getCoord(x, y*2);
    color(col);
    printf("*");
    for(int i = 1; i <= num; ++i) {
            double x1 = x - i;
            double y1 = y + i;
        for(int j = 0; j < i * 2 + 1; ++j) {
            getCoord(x1 + j, y1 * 2);
            printf("*"); 
        }
    }
}
 
void rectangle(int x, int y, int h, int w, int col1, int col2) {
    color(col1);
    for(int i = 0; i <= h; ++i) {
        for(int j = 0; j <= w/2; ++j) {
            getCoord(x + i, y - j);
            if(i % 3 || j % 2)
                printf("*");
            else {
                color(col2);
                printf("!");
                color(col1);
            }
                
            getCoord(x + i, y + j);
            if(i % 3 || j % 2)
                printf("*");
            else {
                color(col2);
                printf("!");
                color(col1);
            }
            
        }
    }
}
 
int main() {
    hideCursor();
    int colTop = 4;
    int colMid = 4;
    int colEnd = 13;
    while(true) {
        colTop = colTop == 4 ? 9 : 4;
        triangleLeft(5, 27.8, 2, colTop);
        triangleRight(5, 27.8, 2, colTop);
        Sleep(100);
        layer(5, 55, 10, 2);
        layer(9, 55, 16, 2);
        layer(14, 55, 26, 2);
        colMid = colMid == 4 ? 5 : 4;
        triangle(11, 55, 3, colMid);
        triangle(19, 60, 3, colMid);
        triangle(29, 42, 3, colMid);
        triangle(31, 57, 3, colMid);
        colEnd = colEnd == 13 ? 1 : 13;
        rectangle(40, 55, 15, 18, 6, colEnd);
        Sleep(200);
    }
    return 0;
}

上面便是圣誕樹(shù)的簡(jiǎn)單實(shí)現(xiàn),下面來(lái)說(shuō)下原理:

函數(shù) layer 畫(huà)出樹(shù)的層次,根據(jù)坐標(biāo)來(lái)輸出位置;

void layer(int x, int y, int num, int col) 

函數(shù) triangle 畫(huà)出小三角形,作為點(diǎn)綴;

void triangle(int x, int y, int num, int col)

函數(shù)?triangleRight 和?triangleLeft 畫(huà)出圣誕樹(shù)頂部的蝴蝶結(jié);

void triangleRight(double x, double y, double num, double col);
void triangleLeft(double x, double y, double num, double col);

函數(shù)?hideCursor 負(fù)責(zé)隱藏光標(biāo);

void hideCursor()

函數(shù)?getCoord 負(fù)責(zé)確定輸出字符的位置;

void getCoord(double y, double x)

函數(shù) color 負(fù)責(zé)設(shè)置輸出的顏色;

void color(int a)

主函數(shù)的原理如下:

void color(int a)

主函數(shù)通過(guò)一個(gè) while 循環(huán),不斷刷新圣誕樹(shù)和圣誕樹(shù)點(diǎn)綴的顏色。

以上所述是小編給大家介紹的C語(yǔ)言實(shí)現(xiàn)一個(gè)閃爍的圣誕樹(shù),希望對(duì)大家有所幫助。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • C++發(fā)送郵件實(shí)現(xiàn)代碼

    C++發(fā)送郵件實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了C++發(fā)送郵件的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • C++ Boost Atomic詳細(xì)講解

    C++ Boost Atomic詳細(xì)講解

    Boost是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱。Boost庫(kù)是一個(gè)可移植、提供源代碼的C++庫(kù),作為標(biāo)準(zhǔn)庫(kù)的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開(kāi)發(fā)引擎之一,是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱
    2022-11-11
  • strings命令分析淺談Go和C++編譯時(shí)的一點(diǎn)小區(qū)別

    strings命令分析淺談Go和C++編譯時(shí)的一點(diǎn)小區(qū)別

    今天小編就為大家分享一篇關(guān)于strings命令分析淺談Go和C++編譯時(shí)的一點(diǎn)小區(qū)別,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • 一篇文章帶你了解C++的KMP算法

    一篇文章帶你了解C++的KMP算法

    這篇文章主要介紹了c++ 實(shí)現(xiàn)KMP算法的示例,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下,希望能給你帶來(lái)幫助
    2021-08-08
  • C++實(shí)現(xiàn)LeetCode(18.四數(shù)之和)

    C++實(shí)現(xiàn)LeetCode(18.四數(shù)之和)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(18.四數(shù)之和),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 詳解C++文件讀寫操作

    詳解C++文件讀寫操作

    這篇文章主要為大家詳細(xì)介紹了C++文件讀寫操作,感興趣的小伙伴們可以參考一下
    2016-03-03
  • QT線程池的使用(QThreadPool類和QRunnable類)

    QT線程池的使用(QThreadPool類和QRunnable類)

    本文主要介紹了QT線程池的使用(QThreadPool類和QRunnable類),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • linux下c語(yǔ)言的多線程編程

    linux下c語(yǔ)言的多線程編程

    這篇文章主要介紹了linux下c語(yǔ)言的多線程編程,需要的朋友可以參考下
    2017-10-10
  • C++語(yǔ)言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實(shí)例代碼

    C++語(yǔ)言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實(shí)例代碼

    這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Visual?Studio?2022?安裝低版本?.Net?Framework的圖文教程

    Visual?Studio?2022?安裝低版本?.Net?Framework的圖文教程

    這篇文章主要介紹了Visual?Studio?2022?如何安裝低版本的?.Net?Framework,首先打開(kāi)?Visual?Studio?Installer?可以看到vs2022?只支持安裝4.6及以上的版本,那么該如何安裝4.6以下的版本,下面將詳細(xì)介紹,需要的朋友可以參考下
    2022-09-09

最新評(píng)論

长岭县| 资中县| 荣昌县| 刚察县| 玉林市| 安阳县| 正阳县| 湖南省| 安顺市| 壤塘县| 吉林市| 无为县| 特克斯县| 车险| 陕西省| 凉山| 宝山区| 屯留县| 涞源县| 将乐县| 赣州市| 浦东新区| 武鸣县| 固安县| 昭平县| 乐都县| 沧源| 许昌市| 利津县| 广丰县| 汪清县| 延川县| 南开区| 平昌县| 修武县| 敦化市| 固阳县| 石泉县| 于都县| 木里| 漳州市|