Re: [問題] c語言聖誕樹並排
.h內容
===============================================
void printLeaf(int Lf_AH,int Lf_NH);
void printStem(int SmW);
void printground(int gl);
void tree3(int T1,int T2,int T3);
.cpp內容
===============================================
void printLeaf(int Lf_AH, int Lf_NH){
int Lwidth;
Lwidth = Lf_AH*2-1;
Lf_NH = Lf_NH-1;
for(int a=1; a<=Lwidth; a++){
if((a >= (Lf_AH-Lf_NH)) && (a <= (Lf_AH+Lf_NH)) )
printf("*");
else
printf("-");
}
}
===============================================
void printStem(int SmW){
int b;
b = ((SmW-1)/2);
for(int a=0; a<SmW; a++){
if(a == b)
printf("*");
else
printf("-");
}
}
===============================================
void printground(int gl){
for(int a=0; a<gl; a++)
printf(" ");
}
===============================================
void tree3(int T1,int T2,int T3){
int maxH=T1;
int width1,width2,width3;
int Stem1,Stem2,Stem3;
int Lcount1,Lcount2,Lcount3;
int Scount1,Scount2,Scount3;
int x;
width1 = T1*2-1;
width2 = T2*2-1;
width3 = T3*2-1;
Stem1 = (T1-1)/2;
Stem2 = (T2-1)/2;
Stem3 = (T3-1)/2;
maxH=T1+Stem1;
if(maxH<(T2+Stem2)) maxH=T2+Stem2;
if(maxH<(T3+Stem3)) maxH=T3+Stem3;
//init value
x=1;
Lcount1=Lcount2=Lcount3=1;
Scount1=Scount2=Scount3=1;
while(x<=maxH){
x++;
//======================================列印第一棵樹範圍
if(Lcount1 <= T1){
printLeaf(T1,Lcount1);
Lcount1++;
}else if(Scount1 <= Stem1){
printStem(width1);
Scount1++;
}else{
printground(width1);
}
printf(" ");
//======================================列印第二棵樹範圍
if(Lcount2 <= T2){
printLeaf(T2,Lcount2);
Lcount2++;
}else if(Scount2 <= Stem2){
printStem(width2);
Scount2++;
}else{
printground(width2);
}
printf(" ");
//======================================列印第三棵樹範圍
if(Lcount3 <= T3){
printLeaf(T3,Lcount3);
Lcount3++;
}else if(Scount3 <= Stem3){
printStem(width3);
Scount3++;
}else{
printground(width3);
}
//======================================
printf("\n");
}
}
main中
執行
tree3(x1,x2,x3); x1~3為樹的高度,一樣是奇數。
即可
要增加樹目,只要修改tree3即可,相信你看的懂低~
============================================
下面就是tree3(3,5,7)出來的結果:
--*-- ----*---- ------*------
-***- ---***--- -----***-----
***** --*****-- ----*****----
--*-- -*******- ---*******---
********* --*********--
----*---- -***********-
----*---- *************
------*------
------*------
------*------
--
沒人推文 我好想哭...QQ 兩銀的文章你要人推!?...算了吧 !
\ /
○
︵ \○╱/|
. . ﹎ ﹍﹍ ╱ ̄ ﹚╱>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.155.180
※ 編輯: lingpxs 來自: 140.118.155.180 (04/30 15:41)
推
05/01 00:08, , 1F
05/01 00:08, 1F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):
問題
5
6