- ----------------------------------------------------------------
- -- Criba de Eratostenes
- --
- -- Creación de una lista que contenga todos los primos menores
- -- que el numero que el usuario introduce
- ----------------------------------------------------------------
- with Ada.Text_IO; use Ada.Text_IO;
- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
- with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
- with Lista;
- procedure criba is
- ----------------------------------------------------------------
- -- Declaración de tipos y paquetes
- ----------------------------------------------------------------
- package ListaCriba is new Lista(Natural); use ListaCriba;
- subtype LCriba is ListaCriba.Tipo;
- ----------------------------------------------------------------
- -- Declaración de funciones y procedimientos
- ----------------------------------------------------------------
- function Primo(N,Div:positive) return boolean is
- -- POST: Res = True si not Div(X,N) para todo valor de X|2 <= X < sqrt(N)
- begin
- if Div > integer(sqrt(float(N))) then
- return True;
- else
- return (N mod Div/=0) and then Primo(N,Div+1);
- end if;
- end Primo;
- function Criba_Aux(N,Cand,Inc:positive) return LCriba is
- begin
- if Cand > N then
- return Vacia;
- elsif Primo(Cand,2) then
- return Cons(Cand, Criba_Aux(N, Cand+Inc, 6-Inc));
- else
- return Criba_Aux(N, Cand+Inc, 6-Inc);
- end if;
- end Criba_Aux;
- function Criba(N:positive) return LCriba is
- -- Incluimos 2 y 3 en la criba y empezamos desde el 5
- -- POST: Devuelve todos los primos anteriores al numero especificado
- begin
- if Es_Vacia(N) then
- return vacia;
- else
- return Cons(2,Cons(3,Criba_Aux(N,5,2)));
- end if;
- end Criba;
- procedure ImprPrimo(N:positive) is
- begin
- Put(N,0);
- end ImprPrimo;
- procedure ImprCriba is new Escribir(ImprPrimo);
- ----------------------------------------------------------------
- -- Delcaración de variables y/o constantes
- ----------------------------------------------------------------
- N: natural;
- ----------------------------------------------------------------
- -- Parte ejecutiva
- ----------------------------------------------------------------
- begin
- Put("Introduce un numero: "); Get(N); New_Line;
- Put("Primos anteriores a ese numero:"); New_Line;
- ImprCriba(Criba(N));
- end criba;
Se que el modulo funciona ya que con un programa de prueba que nos ha dado el profesor funciona y con otra practica también pero con esta otra practica me da un error, a ver si podéis echarme una mano.
criba.adb (el programa):
lista.ads (especificaciones del modulo):
- ----------------------------------------------------------------
- -- lista.ads
- -- Módulo para manejo de listas en Ada (Especificación)
- ----------------------------------------------------------------
- generic type Tipo_Elemento is private; -- Tipo de los elementos
- package lista is
- ----------------------------------------
- -- Declaración de tipos
- ----------------------------------------
- type Tipo is private; -- Tipo para declarar listas
- type Tipo_Elementos is array (Positive range <>) of Tipo_Elemento; -- Tipo para la función ListaCons
- ----------------------------------------
- -- Funciones de manejo de listas
- ----------------------------------------
- function Vacia return Tipo;
- -- PRE: cierto
- -- POST: resultado = <>
- function Es_Vacia(L:Tipo) return Boolean;
- -- PRE: cierto
- -- POST: resultado = (Longitud (L) - 0)
- function Primero(L:Tipo) return Tipo_Elemento;
- -- PRE: Es Vacia (L)
- -- POST: resultado = L(1)
- function Resto(L:Tipo) return Tipo;
- -- PRE: EsVacia (L)
- -- POST: resultado = L(2..)
- function Cons(E:Tipo_Elemento;L:Tipo) return Tipo;
- -- PRE: cierto
- -- POST: resultado(l) = E /\ resultado(2..) = L
- function Lista_Cons(Elementos:Tipo_Elementos) return Tipo; -- Función para manejar constantes de tipo lista
- -- PRE: Cierto
- -- POST: Generar una lista formada por 'elementos'
- ---------------------------------------
- -- Procedimiento para imprimir listas
- ----------------------------------------
- generic with procedure Put_Elemento(E:Tipo_Elemento);
- procedure Escribir(L:Tipo);
- Error_De_Lista: exception;
- private type Desc;
- type Tipo is access Desc;
- end lista;
lista.adb (el modulo):
- ----------------------------------------------------------------
- -- lista.adb
- -- Módulo para manejo de listas en Ada (Implementación)
- ----------------------------------------------------------------
- with Ada.Text_IO; use Ada.Text_IO;
- package body lista is
- type Desc is record
- Elemento: Tipo_Elemento;
- Siguiente: Tipo;
- end record;
- function Vacia return Tipo is
- begin
- return NULL;
- end Vacia;
- function Es_Vacia(L:Tipo) return Boolean is
- begin
- return L = NULL;
- end Es_Vacia;
- function Copia(L:Tipo) return Tipo is
- Aux,Copia,Nuevo: Tipo := L;
- begin
- if Aux = NULL then
- Copia := NULL;
- else
- Copia := new Desc'(Aux.ALL.Elemento,NULL);
- Nuevo := Copia;
- while Aux.ALL.Siguiente /= NULL loop
- Aux := Aux.ALL.Siguiente;
- Nuevo.ALL.Siguiente := new Desc'(Aux.ALL.Elemento,NULL);
- Nuevo := Nuevo.ALL.Siguiente;
- end loop;
- end if;
- return Copia;
- end Copia;
- function Cons(E:Tipo_Elemento;L:Tipo) return Tipo is
- begin
- return new Desc'(E,Copia(L));
- end Cons;
- function Cons_Final(E:Tipo_Elemento;L:Tipo) return Tipo is
- Aux: Tipo;
- begin
- if L = NULL then
- return new Desc'(E,NULL);
- else
- Aux := L;
- while Aux.Siguiente /= NULL loop
- Aux := Aux.Siguiente;
- end loop;
- Aux.Siguiente := NEW Desc'(E, NULL);
- return L;
- end if;
- end Cons_Final;
- function Primero(L:Tipo) return Tipo_Elemento is
- begin
- if Es_Vacia (L) then
- raise Error_De_Lista;
- else
- return L.Elemento;
- end if;
- end Primero;
- function Resto(L:Tipo) return Tipo is
- begin
- if Es_Vacia(L) then
- raise Error_De_Lista;
- else
- return Copia(L.Siguiente);
- end if;
- end Resto;
- function Longitud(L:Tipo) return Natural is
- C: Natural := 0;
- LI: Tipo := L;
- begin
- while LI /= NULL loop
- C:=C+1;
- LI := LI.Siguiente;
- end loop;
- return C;
- end Longitud;
- function Lista_Cons(Elementos:Tipo_Elementos) return Tipo is
- Lista: Tipo;
- I: Positive;
- begin
- I := 1;
- Lista := Vacia;
- for I in Elementos'First..Elementos'Last loop
- Lista := Cons_Final(Elementos(I),Lista);
- end loop;
- return Lista;
- end Lista_Cons;
- procedure Escribir(L:Tipo) is
- LI: Tipo := L;
- begin
- Put("[");
- while LI /= NULL loop
- Put_Elemento(LI.Elemento);
- LI := LI.Siguiente;
- if LI /= NULL then
- Put(",");
- end if;
- end loop;
- Put("]");
- end Escribir;
- end lista;
Este es el error (compilando con GNAT 2008 en Windows 32bits y en Linux x64):
- $ gnatmake criba.adb
- gcc-ada -c criba.adb
- criba.adb:48:19: expected private type "Tipo" defined at lista.ads:12, instance at line 17
- criba.adb:48:19: found type "Standard.Integer"
- gnatmake: "criba.adb" compilation error
Gracias por adelantado.