Ten post jest stary, ale mam nadzieję, że to pomoże innym.
„Wprowadzenie do algorytmów” książka (przez CORMEN, Leiserson i Rivesta) to dobra książka do czytania o algorytmach, ale „pseudo-code” jest straszna. Rzeczy takie jak Q [1 ... n] jest nonsens, kiedy trzeba zrozumieć, co Q [1 ... n] jest rzekomo na myśli. Które trzeba będzie zwrócić uwagę na zewnątrz „pseudo-kodu.” Ponadto, książki takie jak „Wprowadzenie do algorytmów” lubią używać matematycznego składni, która jest naruszającego jeden cel pseudo-kodu.
Pseudo-kod powinien zrobić dwie rzeczy. Streszczenie od składni i być łatwe do odczytania. Jeśli rzeczywisty kod jest bardziej opisowe niż pseudo-kodu, a rzeczywisty kod jest bardziej opisowy, to nie jest pseudo-code.
Załóżmy, że zostały napisanie prostego programu.
Konstrukcja ekranu:
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
Lista zmienna:
TOTAL: double
SUB_TOTAL: double
DISCOUNT: double
Pseudo kod:
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
Zauważ, że jest to bardzo łatwe do odczytania i nie odwoływać żadnych składni. To urządzenie obsługuje wszystkie trzy Bohma and Jacopini w strukturach kontrolnych.
Sekwencja:
Print "Some stuff"
VALUE = 2 + 1
SOME_FUNCTION(SOME_VARIABLE)
Wybó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
Powtórzenie:
while condition
do stuff
for SOME_VALUE TO ANOTHER_VALUE
do stuff
porównaj to z tym N-Queen „pseudo-kod” ( 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)
Jeśli nie można wyjaśnić po prostu nie rozumiesz to wystarczająco dobrze. - Albert Einstein