HELLO WORLD!
This program simply demonstrates the return of a string from a procedure
call, in the Ada programming language. The program will display the
message "Hello world" each time the procedure HELLO is called.
Source Code
with Text_To; use Text_To
procedure hello is
begin
put("Hello World");
end hello
STACK PROGRAMS
This program demonstrates the STACK ADT, using the Ada programming
language.
Source Code
generic
package STACK_PACKAGE is
NULL_STACK : exception;
STACK_OVERFLOW : exception;
STACK_UNDERFLOW : exception;
private
type GENERIC_STACK is
record
CONTENTS : STACK_LIST;
TOP : NATURAL range NATURAL'FIRST .. SIZE := NATURAL'FIRST;
end record;
end STACK_PACKAGE;
-------------------------------------------------------------------------
---------------
-- function TOP_ELEMENT -- This function returns the value of the top
-- element on the stack. It does not return a
-- pointer to the top element. If the stack is empty, a constraint error
-- occurs. The exception handler will then raise the NULL_STACK
-- exception and pass it to the calling procedure.
---------------
function TOP_ELEMENT( STACK : in GENERIC_STACK ) return ELEMENTS is
begin
return STACK.CONTENTS(STACK.TOP);
exception
when CONSTRAINT_ERROR =>
raise NULL_STACK;
when others =>
raise;
end TOP_ELEMENT;
----------
-- Is stack empty?
----------
function STACK_IS_EMPTY( STACK : in GENERIC_STACK )
return BOOLEAN is
begin
return (STACK.TOP = NATURAL'FIRST);
exception
when OTHERS =>
raise;
end STACK_IS_EMPTY;
---------------
-- procedure CLEAR_STACK -- This procedure resets the stack pointer, TOP,
-- to a value representing an empty stack.
---------------
procedure CLEAR_STACK( STACK : in out GENERIC_STACK ) is
begin
STACK.TOP := NATURAL'FIRST;
end CLEAR_STACK;
---------------
-- procedure PUSH -- This procedure attempts to push another element onto
-- the stack. If the stack is full, a constraint error
-- occurs. The exception handler will then raise the STACK_OVERFLOW
-- exception and pass it to the calling procedure.
---------------
procedure PUSH ( FRAME : in ELEMENTS;
STACK : in out GENERIC_STACK ) is
begin
STACK.TOP := STACK.TOP + 1;
STACK.CONTENTS(STACK.TOP) := FRAME;
exception
when CONSTRAINT_ERROR =>
raise STACK_OVERFLOW;
when others =>
raise;
end PUSH;
---------------
-- procedure POP -- This procedure attempts to pop an element from
-- the stack. If the stack is empty, a constraint error
-- occurs. The exception handler will then raise the STACK_UNDERFLOW
-- exception and pass it to the calling procedure.
---------------
procedure POP ( FRAME : out ELEMENTS;
STACK : in out GENERIC_STACK ) is
begin
FRAME := STACK.CONTENTS(STACK.TOP);
STACK.TOP := STACK.TOP - 1;
exception
when CONSTRAINT_ERROR =>
raise STACK_UNDERFLOW;
when others =>
raise;
end POP;
end STACK_PACKAGE;
ARRAY SUMMATION EXAMPLE PROGRAM
This program demonstrates some of ADA's features, such as packaging and
array processing.
Source Code
package ArrayCalc is
type Mydata is private;
function sum return integer;
procedure setval(arg:in integer);
private
size: constant:= 99;
type myarray is array(1..size) of integer;
type Mydata is record
val: myarray;
sz: integer := 0;
end record;
v: Mydata;
end;