Dit bericht is oud, maar hopelijk zal dit anderen te helpen.
"Introduction to Algorithms" book (door Cormen, Leiserson en Rivest) is een goed boek om te lezen over algoritmen, maar de "pseudo-code" is verschrikkelijk. Dingen zoals Q [1 ... n] is onzin wanneer men moet begrijpen wat Q [1 ... n] is te veronderstellen betekenen. Die zullen moeten buiten de te worden opgemerkt "pseudo-code." Bovendien boeken als "Introduction to Algorithms" graag een wiskundige syntax, die het schenden van een doel van pseudo-code te gebruiken.
Pseudo-code moet twee dingen doen. Abstracte weg van syntax en zijn gemakkelijk te lezen. Indien de werkelijke code is meer beschrijvend dan de pseudo-code, en de werkelijke code is meer beschrijvend, dan is het niet pseudo-code.
Stel dat je aan het schrijven van een eenvoudig programma.
Screen design:
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-code:
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
Merk op dat dit is zeer gemakkelijk te lezen en geen syntax verwijzen. Dit ondersteunt alle drie van Bohm en Jacopini de controle structuren.
Volgorde:
Print "Some stuff"
VALUE = 2 + 1
SOME_FUNCTION(SOME_VARIABLE)
Selectie:
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
Herhaling:
while condition
do stuff
for SOME_VALUE TO ANOTHER_VALUE
do stuff
vergelijk dat deze N-Queens "pseudo-code" ( 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)
Als je het niet zomaar kan verklaren, heb je het niet goed genoeg te begrijpen. - Albert Einstein