Algorithm Design & Representation 算法设计与表示
Learning Objectives 学习目标
- Design algorithms to solve problems using Pseudocode, Flowcharts, and Decision Tables.
- Draw structured Nassi-Shneiderman (NS) diagrams representing sequential logic.
- Formulate Decision Tables to map complex conditional business rules.
- 设计算法,使用伪代码、流程图和决策表解决问题。
- 绘制代表顺序逻辑的结构化 Nassi-Shneiderman (NS) 图。
- 制定决策表来映射复杂的条件业务规则。
2 · Pseudocode Outline
Pseudocode is an informal, high-level description of an algorithm. It uses structured English-like keywords (such as BEGIN, INPUT, IF-THEN-ELSE, WHILE, and END) to focus on program logic without worrying about language-specific syntax.
伪代码是算法的一种非正式的高级描述。它使用类似于结构化英语的关键字(例如 BEGIN、INPUT、IF-THEN-ELSE、WHILE 和 END),让开发者能够专注于程序逻辑,而无需担心特定语言的语法。
Core Properties of Algorithms算法的核心属性
- Definite:明确性: Each step must be clear and unambiguous.每一步都必须清晰且无歧义。
- Finite:有限性: It must terminate after a finite number of steps.必须在有限的步骤后终止。
- Effective:有效性: Operations must be feasible and simple to perform.操作必须是可行的且易于执行。
3 · Flowchart Standard Shapes
A flowchart is a graphical representation of the sequential steps of an algorithm or program logic. It uses standard geometric shapes connected by arrows (flowlines) to show the path of execution: 流程图是算法或程序逻辑的顺序步骤的图形表示。它使用由箭头(流程线)连接的标准几何形状来显示执行路径:
| Shape形状 | Name名称 | Purpose & Description用途与说明 | Example示例 |
|---|---|---|---|
| Oval / 椭圆 | Terminator终结符 | Represents the Start or End point of an algorithm.代表算法的起点或终点。 | Start / End |
| Rectangle / 矩形 | Process处理 | Represents arithmetic processes, calculations, or variables assignments.代表算术处理、计算或变量赋值。 | total = price + tax |
| Parallelogram / 平行四边形 | Input / Output输入/输出 | Represents reading data from user or displaying text on console.代表读取用户数据或在控制台显示文本。 | Input age / Display fee |
| Diamond / 菱形 | Decision决策 | Represents conditional branches with true/false (yes/no) output lines.代表具有真/假(是/否)输出线的条件分支。 | Is age < 12? |
4 · Decision Tables & NS Diagrams
When conditions grow complex, nested IF-THEN blocks become difficult to read. Two advanced tools simplify representation:
当条件变得复杂时,嵌套的 IF-THEN 块会变得难以阅读。两个先进的工具可以简化这种表示:
Decision Table决策表
A tabular layout mapping conditional rules to distinct logic actions. Excellent for business rules and verifying all test cases. 将条件规则映射到不同逻辑操作的表格布局。非常适合业务规则和验证所有测试用例。
Nassi-Shneiderman (NS)NS 盒图 (Nassi-Shneiderman)
Also known as a structogram. It uses nested boxes instead of shapes and flowlines to enforce structured programming patterns. 也被称为结构图。它使用嵌套框而不是形状和流程线来强制执行结构化编程模式。
Applied Case Study: Billing & Pricing Logic应用案例研究:账单与定价逻辑
Let's analyze three real-world logic systems designed for services and operations. We use these scenario parameters to formulate decision tables and pseudocodes. 让我们分析为服务和运营设计的三个实际逻辑系统。我们使用这些场景参数来制定决策表和伪代码。
Scenario Details场景详情
A theatre bills customers based on age bounds: age under 12 costs RM 12; senior citizen (age 60 and above) costs RM 15; others cost RM 25. 剧院根据年龄段收费:12 岁以下费用为 RM 12;老年人(60 岁及以上)费用为 RM 15;其他人费用为 RM 25。
Decision Table决策表
| Conditions & Actions条件与操作 | Rule 1 | Rule 2 | Rule 3 |
|---|---|---|---|
| Age < 12 | Yes | No | No |
| Age ≥ 60 | No | Yes | No |
| Price = RM 12 | X | - | - |
| Price = RM 15 | - | X | - |
| Price = RM 25 | - | - | X |
Pseudocode伪代码
BEGIN
DISPLAY "Enter customer age:"
INPUT age
IF age < 12 THEN
price = 12
ELSE IF age >= 60 THEN
price = 15
ELSE
price = 25
END IF
DISPLAY "Ticket price is RM " + price
END
Scenario Details场景详情
Parking fees: duration under 2 hours costs RM 3; duration between 2 and 8 hours (inclusive) costs RM 10; duration over 8 hours costs RM 20. 停车费:2小时以下收费 RM 3;2至8小时(含)收费 RM 10;8小时以上收费 RM 20。
Decision Table决策表
| Conditions & Actions条件与操作 | Rule 1 | Rule 2 | Rule 3 |
|---|---|---|---|
| Duration < 2 hours | Yes | No | No |
| Duration ≥ 2 and ≤ 8 hours | No | Yes | No |
| Fee = RM 3 | X | - | - |
| Fee = RM 10 | - | X | - |
| Fee = RM 20 | - | - | X |
Pseudocode伪代码
BEGIN
DISPLAY "Enter parking hours:"
INPUT duration
IF duration < 2 THEN
fee = 3
ELSE IF duration >= 2 AND duration <= 8 THEN
fee = 10
ELSE
fee = 20
END IF
DISPLAY "Parking fee is RM " + fee
END
Scenario Details场景详情
Vehicle rental rate calculation: standard daily rate is RM 120. If renting more than 7 days, daily rate is discounted to RM 100. 车辆租金计算:标准每日费用为 RM 120。如果租赁天数超过 7 天,每日费用折扣为 RM 100。
Decision Table决策表
| Conditions & Actions条件与操作 | Rule 1 (Long Rental) | Rule 2 (Standard Rental) |
|---|---|---|
| Rental Days > 7 | Yes | No |
| Set Daily Rate = RM 100 | X | - |
| Set Daily Rate = RM 120 | - | X |
Pseudocode伪代码
BEGIN
DISPLAY "Enter rental days:"
INPUT days
IF days > 7 THEN
rate = 100
ELSE
rate = 120
END IF
totalCost = days * rate
DISPLAY "Total cost is RM " + totalCost
END
Interactive Worked Logic Exercises交互式计算逻辑习题
Problem 1: Summing Odd Integers习题 1:奇数整数求和
Task: Read exactly 10 integers from user, sum up all odd integers, and print the total.任务:读取用户输入的 10 个整数,计算所有奇数的和,并输出总数。
💡 Hint / 提示
Use a counter-controlled loop (1 to 10). Within the loop, apply modulo checking (num % 2 != 0) to identify odd values.使用计数控制的循环(1到10)。在循环体内,使用取模判断(num % 2 != 0)来识别奇数值。
✅ Show Logic / 显示逻辑
BEGIN
sum = 0
counter = 0
WHILE counter < 10 DO
INPUT num
IF num % 2 != 0 THEN
sum = sum + num
END IF
counter = counter + 1
END WHILE
DISPLAY "Sum of odd numbers: " + sum
END
Problem 2: Summing Positive Even Integers习题 2:正偶数求和
Task: Read exactly 15 integers from user, calculate the sum of all positive even integers (value > 0 and divisible by 2).任务:读取用户输入的 15 个整数,计算所有正偶数(值大于0且能被2整除)的和,并输出结果。
💡 Hint / 提示
Combine relational operator checking (num > 0) and modulo operator checking (num % 2 == 0) inside the loop.在循环体内结合关系运算符判断(num > 0)和取模运算符判断(num % 2 == 0)。
✅ Show Logic / 显示逻辑
BEGIN
sum = 0
counter = 0
WHILE counter < 15 DO
INPUT num
IF num > 0 AND num % 2 == 0 THEN
sum = sum + num
END IF
counter = counter + 1
END WHILE
DISPLAY "Sum of positive even numbers: " + sum
END
Check Your Understanding自我检测
Q1. Which flowchart shape represents input or output operations?哪个流程图形状代表输入或输出操作?
Q2. Which algorithmic representation tool is also called a structogram?哪种算法表示工具也被称为结构图 (structogram)?
Q3. What is a key property of an algorithm stating each step must be clear and unambiguous?算法的哪个关键属性指出了每一步都必须清晰且无歧义?
Q4. In a decision table, rules evaluate output results based on combinations of:在决策表中,规则根据什么的组合来评估输出结果?