C語言編寫列印菱形

來源:互聯網
上載者:User

描述略,直接上代碼.

CODE:

 1 #include "stdio.h" 2  3 /* 輸出實心菱形 */ 4 int output_1(int a) 5 { 6     int i, j, n, m; 7     printf("以下是實心的圖形\n"); 8     m=a/2+1; 9     for(j=1;j<=a;j++)10     {11         n=(m-j);12         if(n<0)13             n=-n;14         for(i=1;i<=a;i++)15         {16             if(i<=n||i>=1+a-n)17                 putchar(' ');18             else19                 putchar('*');20         }21         putchar('\n');22     }23     return 1;24 }25 26 /* 輸出空心菱形 */27 int output_2(int a)28 {29     int i, j, m, n;30     m=a/2+1;31     printf("以下是空心的圖形\n");32     for(j=1;j<=a;j++)33     {34         n=(m-j);35         if(n<0)36             n=-n;37         for(i=1;i<=a;i++)38         {39             if(i==n+1||i==a-n)40                 putchar('*');41             else42                 putchar(' ');43         }44         putchar('\n');45     }46     return 1;47 }48 49 int main()50 {51     int n, choose;52     while(1)53     {54         printf("請輸入一個奇數. n=");55         scanf("%d",&n);56         if(n%2)57         {58             printf("請選擇列印菱形類型:1.實心; 2.空心.\nChoose=");59             scanf("%d",&choose);60             if(choose==1) output_1(n);61             else if(choose==2) output_2(n);62             else printf("Error.\n");63         }64     }65     66     return 0;67 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.