如何用C语言实现圣诞树
如何用C语言实现圣诞树
这篇文章主要介绍“如何用C语言实现圣诞树”,在日常操作中,相信很多人在如何用C语言实现圣诞树问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用C语言实现圣诞树”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
初学者版:
#include<stdio.h>#include<windows.h>#include<string.h>voidcolor(intx){if(x>=0&&x<=15)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);elseSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);}voidset_Christmastree(char*p){inti,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("=");}}intmain(){intt=0;color(11);printf("首先祝这个电脑前的小可爱圣诞快乐(~ ̄▽ ̄)~");printf("\n");printf("接下来有几个选项需要您的选择,你需要我的作者给您画的圣诞树还是自己做一个呢?");printf("\n");printf("看作者的选0,如果想自己做呢选1,祝您开心o(*^@^*)o");printf("您的选择是:");scanf("%d",&t);if(t==0){charl[6]="HAPPYU",m[100]="MerryChristmas.";set_Christmastree(l);color(3);printf("\nIwanttosay:\n");puts(m);printf("Madebyzhanduanwanfeng.");}if(t==1){chark[6],w[100]="MerryChristmas.";printf("请选择您要隐藏的字符,只有六个字符哦,可以有特殊含义(IMISSU,ILOVEU等哦)后面还可加一句你想说的话,与前面字符用空格隔开,字符间不要加空格哦>_<:");scanf("%s",k);gets(w);set_Christmastree(k);color(3);printf("\nIwanttosay:\n");puts(w);printf("Madebyzhanduanwanfeng.");}return0;}
这是效果图:
初学者版2
比前一个多了雪花,复杂了些。
#include<stdio.h>#include<windows.h>#include<string.h>voidcolor(intx){if(x>=0&&x<=15)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);elseSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);}voidset_Christmastree(char*p){//打印圣诞树和雪景的函数inti,j,n=5,s,y=0;color(2);printf("\n");for(i=1;i<=n;i++){y++;if(i==1){//第一层是一个*,之后都是梯形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);}elseprintf("");}elseif(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);}elseprintf("");}else{if(s==n+2+y){//这几个if函数都是用来打印圣诞树上的变色字符color(11);printf("%c",p[0]);color(2);continue;}elseif(s==n+4+y){color(5);printf("%c",p[1]);color(2);continue;}elseif(s==n+6+y){color(4);printf("%c",p[2]);color(2);continue;}elseif(s==n+8+y){color(1);printf("%c",p[3]);color(2);continue;}elseif(s==n+10+y){color(12);printf("%c",p[4]);color(2);continue;}elseprintf("*");}}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;}elseprintf("=");}}intmain(){intt=0;color(11);printf("首先祝这个电脑前的小可爱圣诞快乐(~ ̄▽ ̄)~");printf("\n");printf("接下来有几个选项需要您的选择,你需要我的作者给您画的圣诞树还是自己做一个呢?");printf("\n");printf("看作者的选0,如果想自己做呢选1,祝您开心o(*^@^*)o");printf("您的选择是:");scanf("%d",&t);if(t==0){charl[6]="HAPPYU",m[100]="MerryChristmas.";set_Christmastree(l);color(3);printf("\nIwanttosay:\n");puts(m);printf("Madebyzhanduanwanfeng.");}if(t==1){chark[6],w[100]="MerryChristmas.";printf("请选择您要隐藏的字符,只有六个字符哦,可以有特殊含义(IMISSU,ILOVEU等哦)后面还可加一句你想说的话,与前面字符用空格隔开,字符间不要加空格哦>_<:");scanf("%s",k);gets(w);set_Christmastree(k);color(3);printf("\nIwanttosay:\n");puts(w);printf("Madebyzhanduanwanfeng.");}return0;}
这是效果图:
以下是进阶版,更炫:
#include<windows.h>#include<math.h>#include<stdio.h>#include<stdlib.h>#definePI3.14159265359#defineTpx+scale*r*cosf(theta),py+scale*r*sin(theta)floatsx,sy;floatsdCircle(floatpx,floatpy,floatr){floatdx=px-sx,dy=py-sy;returnsqrtf(dx*dx+dy*dy)-r;}floatopUnion(floatd1,floatd2){returnd1<d2?d1:d2;}floatf(floatpx,floatpy,floattheta,floatscale,intn){floatd=0.0f;for(floatr=0.0f;r<0.8f;r+=0.02f)d=opUnion(d,sdCircle(T,0.05f*scale*(0.95f-r)));if(n>0)for(intt=-1;t<=1;t+=2){floattt=theta+t*1.8f;floatss=scale*0.9f;for(floatr=0.2f;r<0.8f;r+=0.1f){d=opUnion(d,f(T,tt,ss*0.5f,n-1));ss*=0.8f;}}returnd;}intribbon(){floatx=(fmodf(sy,0.1f)/0.1f-0.5f)*0.5f;returnsx>=x-0.05f&&sx<=x+0.05f;}intmain(intargc,char*argv[]){intn=argc>1?atoi(argv[1]):3;floatzoom=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_ti=0;i<40;i++){putchar('');}//_sleep(5000);printf("MerryChristmas,baby~!\n");Sleep(500);for(size_ti=0;i<40;i++){putchar('');}printf("Neversaydie.\n");Sleep(500);for(size_ti=0;i<40;i++){putchar('');}printf("2016/12/24,最爱你的UP主\n");getchar();}
到此,关于“如何用C语言实现圣诞树”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注恰卡编程网网站,小编会继续努力为大家带来更多实用的文章!