鐵之狂傲

 取回密碼
 註冊
搜尋

切換到指定樓層
1#
對不起不起~我想麻煩各位,因為很急...明天中午就要了.....= =

不知可否請大大幫我解釋一下
這個程式的一些東西 (紅色部份是我不懂的)




/* 一棵聖誕樹的畫法可以是一個多層的實心等腰三角形疊在一起,
   最上層的三角形邊長最小,越往下的三角形邊長越大;
   每一個三角形的底部和下一個三角形的頂點重疊在一起。
   最底部的三角形下面畫一個實心長方形作為樹幹。
   寫一個 C 語言的程式畫一棵如下所定義聖誕樹:
     1. 聖誕樹的層數在 2 和 5 之間 (含),
     2. 頂端的三角形邊長在  3 和 6 之間 (含),
     3. 每一個下面三角形的邊長比其上端三角形大 1 至 5 點 (含),
     4. 樹幹的寬是 3 和 9 之間的奇數 (含),
     5. 樹幹的高在 4 到 10之間 (含),
     6. 使用 '#' 作為三角形邊的點和 [email=]'@'[/email]作為三角形內部的點,
     7. 使用 '|' 作為樹幹的點。
*/
#include <stdio.h>
#include <stdlib.h>

//畫樹函式
void printChar(int n, char c)
{
int i;

for (i = 0; i < n; i++) printf("%c", c);
}
int main(void) {
const int leading = 10;
  int layer, L, side, growth, theight, twidth, n, i;
  
  // 讀入層數
  do {
    printf("\n Enter the number of layers (2 to 5): ");
    scanf("%d", &layer);
  } while ((layer < 2) || (layer > 5));
  
  // 讀入頂端三角形的邊長
  do {
    printf("\n Enter the side of top layer (3 to 6): ");
    scanf("%d", &side);
  } while ((side < 3) || (side > 6));
  
  // 讀入下一個三角形邊長增加的點數
  do {
    printf("\n Enter the growth of each layer (1 to 5): ");
    scanf("%d", &growth);
  } while ((growth < 1) || (growth > 5));
  
  // 讀入樹幹的寬度
  do {
    printf("\n Enter the trunk width (odd number, 3 to 9): ");
    scanf("%d", &twidth);
  } while ((twidth < 3) || (twidth > 9) || (twidth % 2 == 0));
  
  // 讀入樹幹的高度
  do {
    printf("\n Enter the trunk height (4 to 10): ");
    scanf("%d", &theight);
  } while ((theight < 4) || (theight > 10));
  
  printf("\n\n");
  //樹葉
  for (L = 0; L < layer; L++) {
    n = side + growth * L;
    if (L==0) {
      printChar(leading + n-1+(layer-L-1)*growth, ' ');
      printChar(1, '#');
      printf("\n");
    }
   
    for (i = 1; i < n-1; i++) {
      printChar(leading + n-i-1+(layer-L-1)*growth, ' ');
      printChar(1, '#');
      printChar(2*i-1,
'@');
      printChar(1, '#');
      printf("\n");
    }
   
    printChar(leading + (layer-L-1)*growth, ' ');
    printChar(2*n-1, '#');
    printf("\n");
  }
  //樹幹部分  
  for (i = 0; i < theight; i++) {
    printChar(leading + (layer-1)*growth+side-1-twidth/2, ' ');
    printChar(twidth, '|');
    printf("\n");
  }
  printf("\n");
   
  system("pause");
  return 0;
}

[ 本文最後由 正義之拳 於 07-6-26 02:05 PM 編輯 ]
 
轉播0 分享0 收藏0

回覆 使用道具 檢舉

全世界最先進的跳動筆

無名的勇者

夜明月展黑翅翔空

//畫樹函式
void printChar(int n, char c)
{
   int i;
   for (i = 0; i < n; i++) printf("%c", c);
}

首先這是可呼叫的函式
呼叫之後給他一個整數 跟 一個字元
他會依照給的數值 印出多少個給的字元

const int leading = 10;
宣告一個常數整數 名稱為leading 值為10

    if (L==0) {
      printChar(leading + n-1+(layer-L-1)*growth, ' ');
      printChar(1, '#');
如果L變數等於0
呼叫printChar 給他liading的值+上後面的方程式計算(也就是說這段leading + n-1+(layer-L-1)*growth會給入一個整數)
後面給的' '字元則是空白

同理類推
下面就是給1並且給#這個字元

你其他紅色部分也都是這樣的模式....
不知道這樣說你懂不懂...不懂再問吧0.0...
 

回覆 使用道具 檢舉

總評分:  聲望 + 2   檢視全部評分
jhun2000  GJ!!感覺很厲害可惜我不懂!  發表於 07-9-13 09:14 聲望 + 2 枚  回覆一般留言
你需要登入後才可以回覆 登入 | 註冊

存檔|手機版|聯絡我們|新聞提供|鐵之狂傲

GMT+8, 24-4-27 01:29 , Processed in 0.793979 second(s), 16 queries , Gzip On.

回頂部