Tento příspěvek je starý, ale doufejme, že to bude pomáhat ostatním.
„Úvod do algoritmů“ kniha (o CORMEN, Leiserson a Rivestem) je dobrá kniha na čtení o algoritmech, ale „pseudo-code“ je hrozné. Věci, jako je Q [1 ... n] je nesmysl, když je třeba porozumět tomu, co Q [1 ... n] se má znamenat. Který bude muset být uvedeno mimo „pseudo-kódu.“ Navíc knihy jako „Úvod do algoritmů“ chtěli používat matematický syntaxi, která je porušuje jeden účel pseudo-kódu.
Pseudo-kód by měl udělat dvě věci. Abstrakt od syntaxe a musí být snadno čitelné. Pokud skutečný kód je výstižnější než pseudo-kódu a skutečné číslo je více popisný, pak to není pseudo-code.
Řekněme, že jste psali jednoduchý program.
Design Screen:
Welcome to the Consumer Discount Program!
Please enter the customers subtotal: 9999.99
The customer receives a 10 percent discount
The customer receives a 20 percent discount
The customer does not receive a discount
The customer's total is: 9999.99
Variable List:
TOTAL: double
SUB_TOTAL: double
DISCOUNT: double
Pseudo kód:
DISCOUNT_PROGRAM
Print "Welcome to the Consumer Discount Program!"
Print "Please enter the customers subtotal:"
Input SUB_TOTAL
Select the case for SUB_TOTAL
SUB_TOTAL > 10000 AND SUB_TOTAL <= 50000
DISCOUNT = 0.1
Print "The customer receives a 10 percent discount"
SUB_TOTAL > 50000
DISCOUNT = 0.2
Print "The customer receives a 20 percent discount"
Otherwise
DISCOUNT = 0
Print "The customer does not a receive a discount"
TOTAL = SUB_TOTAL - (SUB_TOTAL * DISCOUNT)
Print "The customer's total is:", TOTAL
Všimněte si, že toto je velmi dobře čitelný a neodkazuje na žádnou syntaxi. To podporuje všechny tři Böhma a Jacopini v řídicích struktur.
Sekvence:
Print "Some stuff"
VALUE = 2 + 1
SOME_FUNCTION(SOME_VARIABLE)
Výběr:
if condition
Do one extra thing
if condition
do one extra thing
else
do one extra thing
if condition
do one extra thing
else if condition
do one extra thing
else
do one extra thing
Select the case for SYSTEM_NAME
condition 1
statement 1
condition 2
statement 2
condition 3
statement 3
otherwise
statement 4
Opakování:
while condition
do stuff
for SOME_VALUE TO ANOTHER_VALUE
do stuff
porovnat, že na tuto N-královen „pseudo-kódu“ ( https://en.wikipedia.org/wiki/Eight_queens_puzzle ):
PlaceQueens(Q[1 .. n],r)
if r = n + 1
print Q
else
for j ← 1 to n
legal ← True
for i ← 1 to r − 1
if (Q[i] = j) or (Q[i] = j + r − i) or (Q[i] = j − r + i)
legal ← False
if legal
Q[r] ← j
PlaceQueens(Q[1 .. n],r + 1)
Pokud nemůžete to vysvětlit jednoduše, vy to nechápete to dost dobře. - Albert Einstein