RosettaCodeData/Task/Stack/Oberon-07/stack-2.oberon
2026-04-30 12:34:36 -04:00

26 lines
737 B
Text

MODULE UseStack; (* use the StackOfInteger module *)
IMPORT Out, SI := StackOfInteger; (* imports StackOfInteger under the name "SI" *)
VAR st : SI.Stack;
v, sum : INTEGER;
BEGIN
st := SI.newStack(); (* create a stack *)
SI.push( st, 1 ); (* add some items *)
SI.push( st, 23 );
SI.push( st, 456 );
WHILE ~ SI.empty( st ) DO (* unstack, sum and print the items *)
v := SI.pop( st );
INC( sum, v );
Out.Int( v, 0 );
IF ~ SI.empty( st ) THEN
Out.String( " (" );Out.Int( SI.peek( st ), 0 );Out.String( ")" )
END;
Out.Ln
END;
Out.String( "sum: " );Out.Int( sum, 0 );Out.Ln
END UseStack.