概述

流程图是一种图表,表示算法、工作流或过程。流程图通过不同种类的框来表示步骤,并通过箭头连接这些框来表示步骤的顺序。这个图示化的表示法说明了一个给定问题的解决模型。流程图广泛用于分析、设计、文档记录或管理不同领域的过程或程序。

讨论

流程图通过形状来显示代码中的步骤,并通过箭头将它们连接在一起。其主要目的是创建一个粗略的解决方案草图来解决编程问题。流程图中的形状类型取决于程序员希望创建的语句类型。例如,“if”语句(只有在某个条件为真时才执行的代码部分)用菱形表示,而循环语句(允许某段代码根据需要重复执行)用六边形表示。流程图也可能会为不同类型的语句添加颜色代码,从而使代码更容易阅读。

以下是常见的流程图符号和示例。在首次阅读这一部分时,重点关注简单的符号和示例。可以在后续章节中回顾高级符号和示例。

简单流程图符号

终端符号
圆角矩形或终端点表示流程图的起始和结束点。

流程线
注意:默认的流程是从左到右、从上到下(与阅读英语的方式相同)。为了节省时间,当流程线与常规流程相反时,箭头通常只绘制在需要的地方。

输入/输出符号
平行四边形表示输入或输出操作。

处理符号
矩形表示一个处理过程,如数学运算或变量赋值。

决策符号
菱形用来表示在决策符号中测试的真假条件。

高级流程图符号

模块调用符号
程序模块在流程图中通常用矩形表示,并通过一些线条与处理符号区分开。程序员通常会区分程序控制和特定任务模块,或区分局部函数和库函数。

预定义过程符号

连接符号
有时,流程图会分成两个或多个较小的流程图。这通常发生在一个流程图无法放在一页上时,或者必须将其分成不同的部分。连接符号(一个小圆圈,内部包含字母或数字)允许你在同一页面上连接两个流程图。类似口袋形状的连接符号可以将流程图与不同页面上的流程图连接起来。

简单示例

我们将通过展示一些伪代码的流程图,演示不同的流程图元素。

函数示例
伪代码:无参数传递的函数

Function clear monitor
    Pass In: nothing
    Direct the operating system to clear the monitor
    Pass Out: nothing
End function

伪代码:主函数调用清除监视器函数

Function main
    Pass In: nothing
    Doing some lines of code
    Call: clear monitor
    Doing some lines of code
    Pass Out: value zero to the operating system
End function

顺序控制结构

下一个示例是一个简单的温度转换程序的伪代码。它演示了页面内和页面间连接符号的使用。它还展示了顺序控制结构,其中没有发生任何异常,只需按照列出的顺序逐个执行指令。

伪代码:顺序控制结构

Filename: Solution_Lab_04_Pseudocode.txt
Purpose: Convert Temperature from Fahrenheit to Celsius
Author: Ken Busbee; © 2008 Kenneth Leroy Busbee
Date: Dec 24, 2008

Pseudocode = IPO Outline

input
    display a message asking the user for the temperature in Fahrenheit
    get the temperature from the keyboard

processing
    calculate the Celsius by subtracting 32 from the Fahrenheit temperature then multiply the result by 5 then divide the result by 9. Round up or down to the whole number.
    HINT: Use 32.0 when subtracting to ensure floating-point accuracy.

output
    display the Celsius with an appropriate message
    pause so the user can see the answer

高级示例

选择控制结构

伪代码:If then Else

If age > 17
    Display a message indicating you can vote.
Else
    Display a message indicating you can't vote.
Endif

伪代码:Case

Case of age
    0 to 17   Display "You can't vote."
    18 to 64  Display "You are in your working years."
    65 +      Display "You should be retired."
End case

迭代(重复)控制结构

伪代码:While

count assigned zero
While count < 5
    Display "I love computers!"
    Increment count
End while

伪代码:For

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End for

对于 for 循环,由于没有标准的流程图方法,可以将其作为计数循环与 while 循环类似地进行流程图化。

伪代码:Do While

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero

伪代码:Repeat Until

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one

关键术语

  • 决策符号:流程图中用于询问问题并做出决策的菱形符号。
  • 流程线:连接各流程图符号的线条(有时带箭头)。
  • 流程图绘制:一种编程设计工具,使用图形元素直观地描绘函数中的逻辑流程。
  • 输入/输出符号:流程图中用于输入/输出交互的平行四边形符号。
  • 处理符号:流程图中表示常规处理过程,如赋值的矩形符号。

参考文献

  • cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
  • Wikipedia: Flowchart
Last modified: Friday, 10 January 2025, 3:39 PM