用C語(yǔ)言實(shí)現(xiàn)圣誕樹(簡(jiǎn)易版+進(jìn)階版)
前言
圣誕節(jié)快到了,這是我到CSDN度過的第一個(gè)圣誕節(jié),也是我學(xué)習(xí)計(jì)算機(jī)的第一個(gè)圣誕節(jié),為了讓它更有意義,我嘔心瀝血,自己打了兩版初學(xué)者版,再在網(wǎng)上找了一版進(jìn)階版,都十分精美,廢話少說,代碼奉上:
初學(xué)者版:
#include<stdio.h>
#include<windows.h>
#include<string.h>
void color(int x)
{
if(x>=0 && x<=15)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void set_Christmastree(char *p){
int i,j,n=5,s;
color(2);
printf("\n");
for(i=1;i<=n;i++){
if(i==1){
for(j=1;j<=i;j++){
for(s=1;s<=n+11-j;s++){
printf(" ");
}
for(s=1;s<=2*j-1;s++){
printf("*");
}
printf("\n");
}
}
else{
for(j=i;j<=i+1;j++){
for(s=1;s<=n+11-j;s++)
{
printf(" ");
}
for(s=1;s<=2*j-1;s++)
{
if(s==n-3){
color(11);
printf("%c",p[0]);
color(2);
continue;
}
if(s==n-1){
color(5);
printf("%c",p[1]);
color(2);
continue;
}
if(s==n+1){
color(4);
printf("%c",p[2]);
color(2);
continue;
}
if(s==n+3){
color(1);
printf("%c",p[3]);
color(2);
continue;
}
if(s==n+5){
color(12);
printf("%c",p[4]);
color(2);
continue;
}
printf("*");
}
printf("\n");
}
}
}
color(6);
for(s=1;s<=n+11;s++)
{
if(s==n+9)
{
printf("|||||\n");
color(15);
printf(" %c%c%c%c%c",p[5],p[5],p[5],p[5],p[5]);
continue;
}
printf(" ");
}
printf("\n");
for(s=1;s<=36;s++)
{
if(s==n+11)
{
printf("V");
continue;
}
printf("=");
}
}
int main(){
int t=0;
color(11);
printf("首先祝這個(gè)電腦前的小可愛圣誕快樂(~ ̄▽ ̄)~");printf("\n");
printf("接下來有幾個(gè)選項(xiàng)需要您的選擇,你需要我的作者給您畫的圣誕樹還是自己做一個(gè)呢?");
printf("\n");
printf("看作者的選0,如果想自己做呢選1,祝您開心o(*^@^*)o");
printf("您的選擇是:");
scanf("%d",&t);
if(t==0){
char l[6]="HAPPYU",m[100]="Merry Christmas.";
set_Christmastree(l);
color(3);
printf("\nI want to say:\n");
puts(m);
printf("Made by zhanduanwanfeng.");
}
if(t==1){
char k[6],w[100]="Merry Christmas.";
printf("請(qǐng)選擇您要隱藏的字符,只有六個(gè)字符哦,可以有特殊含義(IMISSU,ILOVEU等哦)后面還可加一句你想說的話,與前面字符用空格隔開,字符間不要加空格哦>_<:");
scanf("%s",k);
gets(w);
set_Christmastree(k);
color(3);
printf("\nI want to say:\n");
puts(w);
printf("Made by zhanduanwanfeng.");
}
return 0;
} 這是效果圖:


初學(xué)者版2
比前一個(gè)多了雪花,復(fù)雜了些。
#include<stdio.h>
#include<windows.h>
#include<string.h>
void color(int x)
{
if(x>=0 && x<=15)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void set_Christmastree(char *p){ //打印圣誕樹和雪景的函數(shù)
int i,j,n=5,s,y=0;
color(2);
printf("\n");
for(i=1;i<=n;i++){
y++;
if(i==1){ //第一層是一個(gè)*,之后都是梯形
for(j=1;j<=i;j++){
for(s=1;s<=36;s++){
if((s<=n+11-j)||(s>=17)){
if(s%(i+3)==i){ //雪景
color(15);
putchar('*');
color(2);
}
else
printf(" ");
}
else if(s==n+11){
putchar('*');
}
}
printf("\n");
}
}
else{
for(j=i;j<=i+1;j++){ //打印梯形
for(s=1;s<=36;s++)
{
if((s<=n+11-j)||(s>=n+11+j)){
if(s%(i+3)==i){ //雪景
color(15);
putchar('*');
color(2);
}
else
printf(" ");
}
else{
if(s==n+2+y){ //這幾個(gè)if函數(shù)都是用來打印圣誕樹上的變色字符
color(11);
printf("%c",p[0]);
color(2);
continue;
}
else if(s==n+4+y){
color(5);
printf("%c",p[1]);
color(2);
continue;
}
else if(s==n+6+y){
color(4);
printf("%c",p[2]);
color(2);
continue;
}
else if(s==n+8+y){
color(1);
printf("%c",p[3]);
color(2);
continue;
}
else if(s==n+10+y){
color(12);
printf("%c",p[4]);
color(2);
continue;
}
else
printf("*");
}
}
printf("\n");
}
}
}
color(6);
for(s=1;s<=34;s++) //樹樁
{
if(s==n+9){
color(15);
printf("* * * * *");
color(6);
printf("|||||");
color(15);
printf(" * * * * * \n");
printf("* * * * *%c%c%c%c%c * * * * * ",p[5],p[5],p[5],p[5],p[5]);
continue;
}
/*else{
color(15);
printf("* ");
//color(6);
}*/
}
printf("\n");
for(s=1;s<=34;s++){
if(s==n+11){
printf("V");
continue;
}
else
printf("=");
}
}
int main(){
int t=0;
color(11);
printf("首先祝這個(gè)電腦前的小可愛圣誕快樂(~ ̄▽ ̄)~");printf("\n");
printf("接下來有幾個(gè)選項(xiàng)需要您的選擇,你需要我的作者給您畫的圣誕樹還是自己做一個(gè)呢?");
printf("\n");
printf("看作者的選0,如果想自己做呢選1,祝您開心o(*^@^*)o");
printf("您的選擇是:");
scanf("%d",&t);
if(t==0){
char l[6]="HAPPYU",m[100]="Merry Christmas.";
set_Christmastree(l);
color(3);
printf("\nI want to say:\n");
puts(m);
printf("Made by zhanduanwanfeng.");
}
if(t==1){
char k[6],w[100]="Merry Christmas.";
printf("請(qǐng)選擇您要隱藏的字符,只有六個(gè)字符哦,可以有特殊含義(IMISSU,ILOVEU等哦)后面還可加一句你想說的話,與前面字符用空格隔開,字符間不要加空格哦>_<:");
scanf("%s",k);
gets(w);
set_Christmastree(k);
color(3);
printf("\nI want to say:\n");
puts(w);
printf("Made by zhanduanwanfeng.");
}
return 0;
} 這是效果圖:


以下是進(jìn)階版,更炫:
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159265359
#define T px + scale * r * cosf(theta), py + scale * r * sin(theta)
float sx, sy;
float sdCircle(float px, float py, float r) {
float dx = px - sx, dy = py - sy;
return sqrtf(dx * dx + dy * dy) - r;
}
float opUnion(float d1, float d2) {
return d1 < d2 ? d1 : d2;
}
float f(float px, float py, float theta, float scale, int n) {
float d = 0.0f;
for (float r = 0.0f; r < 0.8f; r += 0.02f)
d = opUnion(d, sdCircle(T, 0.05f * scale * (0.95f - r)));
if (n > 0)
for (int t = -1; t <= 1; t += 2) {
float tt = theta + t * 1.8f;
float ss = scale * 0.9f;
for (float r = 0.2f; r < 0.8f; r += 0.1f) {
d = opUnion(d, f(T, tt, ss * 0.5f, n - 1));
ss *= 0.8f;
}
}
return d;
}
int ribbon() {
float x = (fmodf(sy, 0.1f) / 0.1f - 0.5f) * 0.5f;
return sx >= x - 0.05f && sx <= x + 0.05f;
}
int main(int argc, char* argv[]) {
int n = argc > 1 ? atoi(argv[1]) : 3;
float zoom = argc > 2 ? atof(argv[2]) : 1.0f;
for (sy = 0.8f; sy > 0.0f; sy -= 0.02f / zoom, putchar('\n'))
for (sx = -0.35f; sx < 0.35f; sx += 0.01f / zoom) {
if (f(0, 0, PI * 0.5f, 1.0f, n) < 0.0f) {
if (sy < 0.1f) {
putchar('.');
} else {
if (ribbon()) {
putchar('=');
} else {
putchar("............................#j&o"[rand() % 32]);
}
}
} else {
putchar(' ');
}
}
Sleep(1000);
printf("\n\n\n");
for (size_t i = 0; i < 40; i++) {
putchar(' ');
}
//_sleep(5000);
printf("Merry Christmas, baby~!\n");
Sleep(500);
for (size_t i = 0; i < 40; i++) {
putchar(' ');
}
printf("Never say die.\n");
Sleep(500);
for (size_t i = 0; i < 40; i++) {
putchar(' ');
}
printf("2016/12/24, 最愛你的UP主\n");
getchar();
}效果圖就不放了,感興趣可以試試,很炫哦。
總結(jié)
到此這篇關(guān)于用C語(yǔ)言實(shí)現(xiàn)圣誕樹(簡(jiǎn)易版+進(jìn)階版)的文章就介紹到這了,更多相關(guān)C語(yǔ)言圣誕樹內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)比C語(yǔ)言中execv相關(guān)的執(zhí)行文件的三個(gè)函數(shù)
這篇文章主要介紹了對(duì)比C語(yǔ)言中execv相關(guān)的執(zhí)行文件的三個(gè)函數(shù),分別為execv()函數(shù)和execve()函數(shù)以及execvp()函數(shù),需要的朋友可以參考下2015-08-08
C語(yǔ)言實(shí)現(xiàn)數(shù)學(xué)表達(dá)式運(yùn)算
這篇文章主要為大家詳細(xì)介紹了c語(yǔ)言實(shí)現(xiàn)數(shù)學(xué)表達(dá)式運(yùn)算,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Linux下g++編譯與使用靜態(tài)庫(kù)和動(dòng)態(tài)庫(kù)的方法
下面小編就為大家?guī)硪黄狶inux下g++編譯與使用靜態(tài)庫(kù)和動(dòng)態(tài)庫(kù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
深入分析為Visual Assist設(shè)置快捷鍵的方法
本篇文章是對(duì)為Visual Assist設(shè)置快捷鍵的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++用easyx圖形庫(kù)實(shí)現(xiàn)障礙跑酷小游戲
這篇文章主要為大家詳細(xì)介紹了C++用easyx圖形庫(kù)實(shí)現(xiàn)障礙跑酷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12

