begin int N, TOTAL, X; input N; TOTAL := 0; X := 1; if (1 <= N) then while (X <= N) do TOTAL := TOTAL + X; X := X + 1 end fi; output TOTAL end
begin bool X, Y, AND, OR; input X; input Y; AND := X * Y; OR := X + Y; output AND; output OR end
begin int X, Y; input X; input Y; if (Y <= X) then output Y else output X fi endCON EL PROGRAMA ANTERIOR PODEMOS OBTENER:
A) La división entera de dos números: dados A y B, podemos devolver el cociente y el resto;
B) A partir de A) podemos obtener el MCD de dos números utilizando el algoritmo de Euclides.
Obs: El problema es que como en ASPLE no podemos definir funciones (o subrutinas), ni tampoco tenemos instrucciones del tipo GOTO, cada vez que necesitemos saber, por ejemplo, si A es menor que B, debemos introducir el código correspondiente en el lugar apropiado del programa que estemos escribiendo. Esto hará que programas a priori sencillos como los descritos en A) y B) sean mucho mayores de lo que cabría esperar.
begin int X, Y; ref int SMALLEST; input X; input Y; if (Y <= X) then SMALLEST := Y else SMALLEST := X fi endAunque este programa no tiene ningún "output" y por lo tanto no saca nada por pantalla, es mßs apropiado que el 3) para los objetivos que se plantean en 3.A) y 3.B).
begin int DIVIDENDO, DIVISOR, COCIENTE, RESTO; int COUNT, MUL; ref int SMALLEST; input DIVIDENDO; input DIVISOR; COCIENTE := 0; /* inicializamos las variables */ RESTO := DIVIDENDO; /* para inicializar SMALLEST tenemos que ejecutar el codigo de 5) */ /* estamos calculando el menor entre RESTO y DIVISOR */ if (RESTO = DIVISOR) then SMALLEST := DIVISOR /* mirar obs. 2 al final */ else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi; while (SMALLEST = DIVISOR) do COCIENTE := COCIENTE + 1; MUL := COCIENTE * DIVISOR; COUNT := 0; /* este trozo es necesario pues no */ while (MUL + 1 <= DIVIDENDO) do /* disponemos de la resta */ COUNT := COUNT + 1; MUL := MUL + 1 end; RESTO := COUNT; /* dentro del bucle debemos calcular el menor entre RESTO y DIVISOR */ if (RESTO = DIVISOR) then SMALLEST := DIVISOR /* mirar obs. 2 al final */ else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi end; output COCIENTE; output RESTO end
1) evidentemente, hay que eliminar todos los comentários del programa;
2) siendo iguales RESTO y DIVISOR, desde el punto de vista del código que
calcula el menor de ellos sería indiferente devolver uno u otro. Sin embargo,
en este caso queremos que devuelva el DIVISOR, para que entre en el bucle una
vez más, ya que si el RESTO y el DIVISOR son iguales esto significa que el
DIVISOR todavía cabe una vez en el DIVIDENDO, y además la división
es exacta;
3) Si el ejecutable generado no devuelve los valores esperados, aqui tenéis una tabla
con todos los valores que asumen las variables del programa cuando se introducen 9 y 4
como input y que puede ser útil para debugging.
DIVIDENDO DIVISOR COCIENTE RESTO COUNT MUL SMALLEST 9 4 0 9 0 4 -> DIVISOR 1 5 1 5 -> DIVISOR 2 1 2 6 -> RESTO 3 7 4 8 5 9 0 8 1 9
begin int NUMCUATROCIFRAS, DIVIDENDO, DIVISOR, COCIENTE, RESTO; int COUNT, MUL; int UNO, DOS, TRES, CUATRO; bool CAPICUA; ref int SMALLEST; input NUMCUATROCIFRAS; CAPICUA := false; DIVIDENDO := NUMCUATROCIFRAS; DIVISOR := 1000; COCIENTE := 0; RESTO := DIVIDENDO; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi; while (SMALLEST = DIVISOR) do COCIENTE := COCIENTE + 1; MUL := COCIENTE * DIVISOR; COUNT := 0; while (MUL + 1 <= DIVIDENDO) do COUNT := COUNT + 1; MUL := MUL + 1 end; RESTO := COUNT; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi end; UNO := COCIENTE; DOS := RESTO; DIVIDENDO := DOS; DIVISOR := 100; COCIENTE := 0; RESTO := DIVIDENDO; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi; while (SMALLEST = DIVISOR) do COCIENTE := COCIENTE + 1; MUL := COCIENTE * DIVISOR; COUNT := 0; while (MUL + 1 <= DIVIDENDO) do COUNT := COUNT + 1; MUL := MUL + 1 end; RESTO := COUNT; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi end; DOS := COCIENTE; TRES := RESTO; DIVIDENDO := TRES; DIVISOR := 10; COCIENTE := 0; RESTO := DIVIDENDO; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi; while (SMALLEST = DIVISOR) do COCIENTE := COCIENTE + 1; MUL := COCIENTE * DIVISOR; COUNT := 0; while (MUL + 1 <= DIVIDENDO) do COUNT := COUNT + 1; MUL := MUL + 1 end; RESTO := COUNT; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi end; TRES := COCIENTE; CUATRO := RESTO; if ( (UNO = CUATRO) * (DOS = TRES) ) then CAPICUA := true fi; output UNO; output DOS; output TRES; output CUATRO; output CAPICUA end
begin int DECIMAL, I, J ; int NUMCIFRAS, CIFRA ; int BASE, BI ; int RESTA, EXP ; input BASE ; input NUMCIFRAS ; DECIMAL := 0 ; I := 0 ; while ( I + 1 <= NUMCIFRAS ) do input CIFRA ; RESTA := 0; EXP := I + 1; while (EXP + 1 <= NUMCIFRAS) do RESTA := RESTA + 1 ; EXP := EXP + 1 end; J := 0 ; BI := 1 ; while (J + 1 <= RESTA) do BI := BI * BASE ; J := J + 1 end ; DECIMAL := DECIMAL + (CIFRA * BI) ; I := I + 1 end ; output DECIMAL end
begin int FA, FB, FC, I; int NUMERO; int FIBONACCI; input NUMERO; FC := 0; FB := 1; if (NUMERO=0) then FIBONACCI := 0 else if (NUMERO=1) then FIBONACCI := 1 else I := 2; while (I <= NUMERO) do FA := FB + FC ; FC := FB ; FB := FA ; FIBONACCI := FA ; I := I + 1 end fi fi; output FIBONACCI end
begin int NUMERO, DIVIDENDO, DIVISOR, COCIENTE, RESTO; int COUNT, MUL; int HORAS, MINUTOS, MINUTOSTMP, SEGUNDOS; ref int SMALLEST; input NUMERO; DIVIDENDO := NUMERO; DIVISOR := 60; COCIENTE := 0; RESTO := DIVIDENDO; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi; while (SMALLEST = DIVISOR) do COCIENTE := COCIENTE + 1; MUL := COCIENTE * DIVISOR; COUNT := 0; while (MUL + 1 <= DIVIDENDO) do COUNT := COUNT + 1; MUL := MUL + 1 end; RESTO := COUNT; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi end; MINUTOSTMP := COCIENTE; SEGUNDOS := RESTO; DIVIDENDO := MINUTOSTMP; DIVISOR := 60; COCIENTE := 0; RESTO := DIVIDENDO; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi; while (SMALLEST = DIVISOR) do COCIENTE := COCIENTE + 1; MUL := COCIENTE * DIVISOR; COUNT := 0; while (MUL + 1 <= DIVIDENDO) do COUNT := COUNT + 1; MUL := MUL + 1 end; RESTO := COUNT; if (RESTO = DIVISOR) then SMALLEST := DIVISOR else if (RESTO <= DIVISOR) then SMALLEST := RESTO else SMALLEST := DIVISOR fi fi end; HORAS := COCIENTE; MINUTOS := RESTO; output HORAS; output MINUTOS; output SEGUNDOS end
begin int NUMERO; int RADICANDO; int CONTADORRADICANDO; int RAIZN; int TMP; int RESTO; int DIFERENCIA, SUSTRAENDO; input NUMERO; input RADICANDO; RAIZN := 0 ; RESTO := 0 ; TMP := 0 ; while ( TMP + 1 <= NUMERO ) do RAIZN := RAIZN + 1 ; TMP := 1 ; CONTADORRADICANDO := 0 ; while ( CONTADORRADICANDO + 1 <= RADICANDO ) do TMP := TMP * RAIZN ; CONTADORRADICANDO := CONTADORRADICANDO + 1 end end; if ( TMP = NUMERO ) then RESTO := 0 else DIFERENCIA := 0; SUSTRAENDO := 1; while (SUSTRAENDO + 1 <= RAIZN) do DIFERENCIA := DIFERENCIA + 1 ; SUSTRAENDO := SUSTRAENDO + 1 end; RAIZN := DIFERENCIA ; TMP := 1 ; CONTADORRADICANDO := 0 ; while ( CONTADORRADICANDO + 1 <= RADICANDO ) do TMP := TMP * RAIZN ; CONTADORRADICANDO := CONTADORRADICANDO + 1 end; DIFERENCIA := 0; SUSTRAENDO := TMP; while (SUSTRAENDO + 1 <= NUMERO) do DIFERENCIA := DIFERENCIA + 1 ; SUSTRAENDO := SUSTRAENDO + 1 end; RESTO := DIFERENCIA fi ; output (RAIZN) ; output (RESTO) end