Ez a bejegyzés régi, de remélhetőleg ez segít másoknak.
„Bevezetés a algoritmusok” könyv (a Cormen, Leiserson és Rivest) egy jó könyvet olvasni algoritmusok, de a „pszeudo-kód” szörnyű. A dolgok, mint a Q [1 ... n] nonszensz, ha kell megérteni, mi Q [1 ... n] tegyük fel, hogy értem. Amelynek meg kell jegyezni kívül a „pszeudo-kódot.” Sőt, a könyvek mint a „Bevezetés a algoritmusok” szeretnék használni egy matematikai szintaxist, amely megsérti az egyik célja pszeudo-kódot.
Pszeudo-kódot kell két dolgot. Abstract távol szintaxist és könnyen olvasható. Ha a tényleges kód inkább leíró, mint az ál-kódot, és a tényleges kód inkább leíró, akkor nem ál-kódot.
Tegyük fel, hogy írtak egy egyszerű program.
Képernyő 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
Változó listája:
TOTAL: double
SUB_TOTAL: double
DISCOUNT: double
Pszeudo-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
Figyeljük meg, hogy ez nagyon könnyen olvasható, és nem hivatkozik semmilyen szintaxis. Ez támogatja mindhárom Bohm és Jacopini ellenőrzési struktúrákat.
Sorrend:
Print "Some stuff"
VALUE = 2 + 1
SOME_FUNCTION(SOME_VARIABLE)
Kiválasztás:
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
Ismétlés:
while condition
do stuff
for SOME_VALUE TO ANOTHER_VALUE
do stuff
összehasonlítani, hogy ez az N-Queens "pszeudo-kód" ( 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)
Ha nem tudja megmagyarázni, egyszerűen, akkor nem értem, elég jól. - Albert Einstein