iT邦幫忙

0

【從零開始的 C 語言筆記】第二十四篇-程式設計的流程圖製作

Alicia 2021-11-03 22:43:4618889 瀏覽
  • 分享至 

  • xImage
  •  

不怎麼重要的前言

上一篇介紹了比較少使用到的switch條件式,其實也可以用if條件式代替使用,不過就介紹給大家了解看看!

現在學習程式設計中有助釐清思路、寫程式的流程圖吧!


流程圖是什麼?

如同蓋一棟房子必須先畫設計圖,寫一個程式的時候我們也可以透過撰寫流程圖,來幫助自己釐清思路、轉換為程式碼,畢竟當一個程式越來越複雜的時候,我們要光憑想像來寫就越來越難,所以這時可以透過先劃出程式的流程圖,來幫助自己一步步完成程式。

我們來看流程圖是什麼樣子的:
https://ithelp.ithome.com.tw/upload/images/20211103/20142565zCRqjuZUlE.png

這是一個判斷質數的程式,轉換為程式碼為以下:

#include <stdio.h>

int main()
{
    int n, k=0;

    scanf("%d", &n);
    for(int i = 2; i<n; i++){
        if(n%i == 0){
            k = 1;
            break;
        }
    }
    if(k == 0){
        printf("YES\n");
    }
    else{
        printf("NO\n");
    }

    return 0;
}

https://ithelp.ithome.com.tw/upload/images/20211103/20142565AjV0gCXC1C.png


正式使用

  1. 規則
    可以使用紙筆來畫,沒有的話可以用別的線上工具。(找不到的話,參考這個線上工具也可以)

(1) 流程圖使用到的圖形

使用圖形 說明
https://ithelp.ithome.com.tw/upload/images/20211103/201425650HK24iRFPr.png 程式開始、結束的圖形
https://ithelp.ithome.com.tw/upload/images/20211103/20142565c1mjWe2ynC.png 輸入、輸出的圖形
https://ithelp.ithome.com.tw/upload/images/20211103/20142565xWvcuM5VPr.png 程式處理的圖形
https://ithelp.ithome.com.tw/upload/images/20211103/20142565Fu9fnhHmqz.png 條件式的圖形
https://ithelp.ithome.com.tw/upload/images/20211103/20142565Zunxzq0yOs.png 表示程式運行(流程)方向的指向箭頭
https://ithelp.ithome.com.tw/upload/images/20211103/20142565GYoNuEzjcR.png 連接點,單純是無任何處理的中繼點

(2) 清楚的程式開始與結束
https://ithelp.ithome.com.tw/upload/images/20211103/20142565pjg1T5Qm4Y.png
https://ithelp.ithome.com.tw/upload/images/20211103/20142565NCmTNZhWlq.png

(3) 程式的流程以箭頭標示清楚
https://ithelp.ithome.com.tw/upload/images/20211103/20142565OMtMEWg1tp.png
https://ithelp.ithome.com.tw/upload/images/20211103/20142565bbtXHAIz4w.png

(4) 條件式需標示好true、false方向
https://ithelp.ithome.com.tw/upload/images/20211103/20142565YdmmU3JdNI.png
(判斷輸入的整數是否為偶數)

(5) 注意迴圈的畫法是否正確
https://ithelp.ithome.com.tw/upload/images/20211103/201425651slTaZPCa0.png
(從1開始列印到輸入的整數n)

  1. 應用
    我們來嘗試畫一個流程圖,輸入一個整數n,印出第一行一個「*」、第二行兩個「*」、第三行三個「*」,直到列印第n行n個「*」結束,如以下這樣結果的程式。
    https://ithelp.ithome.com.tw/upload/images/20211104/20142565OTsy7ANOBN.png
    https://ithelp.ithome.com.tw/upload/images/20211104/20142565frsusn4b0d.png

流程圖:

程式碼:

#include <stdio.h>

int main()
{
    int n;

    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        for(int j=1; j<=i; j++){
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

看到這裡我們就介紹完流程圖怎麼畫了,用筆、電腦軟體或線上的工具都可以,作從簡單的輸出、加入條件式、加入迴圈,再到結合多重迴圈的範例,不曉得大家有沒有清楚一些?

下一篇我們來介紹很重要、很常使用的語法--副函式吧!


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言