返回列表 回復 發帖

[討論] [C語言]清大資工程式設計作業六解答

[討論] [C語言]清大資工程式設計作業六解答

題目
讓使用者輸入一個字串,譬如 "abc",然後程式要輸出如下排列的圖形:
abccba
b         b
c         c
c         c
b         b
abccba


[提示] 用字元陣列把輸入的字串存起來,例如:

   char str[100];
   scanf("%s", str);

再用雙重迴圈把陣列中需要被顯示的字元印出來。
關於雙重迴圈,除了講義之外,還可以參考首頁裡最新消息放的 nested_loops.ppt 檔案。
程式必須知道使用者輸入的字串長度是多少,所以要用 strlen(),記得要在程式最前面把 string.h 檔案 include。
解答
基本上解答有很多種寫法,我寫的這是最簡單的方法
事實上還有更簡潔的寫法,歡迎大家貼一下自己的寫法
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main(void)
  4. {
  5.     int a, b, c, k;
  6.     char str[40];
  7.    
  8.     printf("Please input a string : ");
  9.     scanf("%s", str);
  10.    
  11.     k = strlen(str);
  12.    
  13.     /*我是快樂的分隔線一號*/
  14.    
  15.     printf("%s", str);
  16.     for(a = 0; a < k; a++)
  17.     {
  18.           printf("%c", str[k-a-1]);
  19.     }
  20.     printf("\n");
  21.    
  22.     /*我是快樂的分隔線二號*/
  23.    
  24.     for(b = 1; b < k; b++)
  25.     {
  26.           printf("%c", str[b]);
  27.          
  28.           for(c = 0; c < (k*2 - 2); c++)
  29.           {
  30.                 printf(" ");
  31.           }
  32.          
  33.           printf("%c\n", str[b]);
  34.     }
  35.    
  36.     /*我是快樂的分隔線三號*/
  37.    
  38.     for(b = 1; b < k; b++)
  39.     {
  40.           printf("%c", str[k-b]);
  41.          
  42.           for(c = 0; c < (k*2 - 2); c++)
  43.           {
  44.                 printf(" ");
  45.           }
  46.          
  47.           printf("%c\n", str[k-b]);
  48.     }
  49.    
  50.     /*我是快樂的分隔線四號*/
  51.         
  52.     printf("%s", str);
  53.     for(a = 0; a < k; a++)
  54.     {
  55.           printf("%c", str[k-a-1]);
  56.     }
  57.    
  58.     return 0;
  59. }
複製代碼
[ 本帖最後由 Eason 於 2007-10-29 15:34 編輯 ]
今天老師公布的另一種解法
相較於我的"暴力分解法" 更有邏輯性 更簡潔
老師還有頰上寫法說明喔^ ^


  1. #include <stdio.h>
  2. #include <string.h>
  3. int main(void)
  4. {
  5.     int i, j;
  6.     char word[50];                  /* word 字元陣列用來記錄使用者輸入的字串 */
  7.     char out[100];                  /* out 是用來暫存輸出的結果 */
  8.     int len;
  9.      
  10.     scanf("%s", word);              /* 讓使用者輸入一個字串 */
  11.     len = strlen(word);             /* 計算使用者輸入的字串長度 */
  12.    
  13.     for (i = 0; i < len; i++) {     /* 用迴圈把整個 word 從頭到尾看一遍 */
  14.         out[i] = word[i];           /* 把 word 逐字複製到 out 對應的位置 */
  15.         out[2*len-i-1] = word[i];   /* out 另外一邊對稱的位置也要填上同樣的字元 */
  16.     }
  17.     out[2*len] = '\0';              /* 在 out 的後面填上'\0',讓 out 變成字串 */
  18.     printf("%s\n", out);            /* 這樣就可以用 printf() 配合 %s 格式直接輸出整個字串*/
  19.    
  20.     for (i=0; i<2*len-1; i++) {    /* 先把 out 用空白字元填滿 (注意: '\0' 還在,不會被蓋掉)*/
  21.         out[i] = ' ';
  22.     }
  23.     for (i = 1; i < len; i++) {     /* 接下來要顯示垂直的部份 */
  24.         out[0] = word[i];           /* 把 out 的第一個字元填上 word[i] */
  25.         out[2*len-1] = word[i];     /* 把 out 最後面的字元填上 word[i] */
  26.         printf("%s\n", out);        /* 把 out 輸出 */
  27.     }
  28.     for (i = len-1; i > 0; i--) {   /* 接下來要顯示垂直的另一半對稱的部份,迴圈要倒著跑 */
  29.         out[0] = word[i];           /* 把 out 的第一個字元填上 word[i] */
  30.         out[2*len-1] = word[i];     /* 把 out 最後面的字元填上 word[i] */
  31.         printf("%s\n", out);        /* 把 out 輸出 */
  32.     }

  33.     for (i = 0; i < len; i++) {     /* 重複做一次第一部份的迴圈 */
  34.         out[i] = word[i];
  35.         out[2*len-i-1] = word[i];
  36.     }
  37.     printf("%s\n", out);            /* 因為 out 的結尾 '\0' 並沒有被更動,所以不用再填 */
  38.    
  39.     return 0;
  40. }

複製代碼
[ 本帖最後由 Eason 於 2007-10-29 20:06 編輯 ]
返回列表