Eiffel Introduction Part IV (2)

zhaozj2021-02-08  361

Eiffel Introduction

Eiffel introduction

Rensselaer, 2000

James C. McKim, JR, Rensselaer At Hartford

K] [N g of @ r k translation

4.4 Test_Stack

Class test_stack

CREATION MAKE_TEST

Feature {none}

Si: my_stack [integer]; ss: my_stack [string]

Make_test Is

DO

TEST_INTEGER_STACK

Test_string_stack

end

TEST_INTEGER_STACK IS

DO

!! si.make (3)

Si.push (3)

Si.push (2)

Si.push (1)

IO.PUTINT (Si.Depth) o.new_line

IO.PUTINT (Si.Top) o.new_line

Si.POP

IO.PUTINT (Si.Top) o.new_line

Si.POP

IO.PUTINT (Si.Top) o.new_line

Si.POP

end

Test_String_Stack IS

DO

!! ss.make (10)

SS.PUSH ("in dixie.")

SS.PUSH ("I Was")

ss.push ("oh, i wish")

IO.PUTSTRING (SS.TOP)

SS.POP

IO.PUTSTRING (SS.TOP)

SS.POP

IO.PUTSTRING (SS.TOP) o.new_line

end

end

4.5 Stack Class (another implementation)

Class my_stack [g]

Creation make

Feature {any}

Capacity: integer

DEPTH: INTEGER IS

DO

Result: = Current_Depth

end

Push (x: g) IS

- make x The top item.

Require

NOT_FULL: Depth

DO

Current_Depth: = current_depth 1

S.PUT (x, current_depth)

Top: = X

Ensure

Depth = Old Depth 1; TOP = X

End - push

Top: g

- The item That Has Been on The Stack for the Least

- Amount of Time.

- Require

- not_empty: depth> 0

POP IS

- Remove the top item.

Require

NOT_EMPTY: DEPTH> 0

DO

Current_Depth: = current_depth - 1

IF current_Depth> 0 THEN

Top: = S.Item (current_depth)

end

Ensure

Depth = Old Depth - 1

- Top = The item Remaining on the stack (if any)

- That Has Been there The Least Amount of Time.

END - POP

Feature {none}

S: array [g]

Current_Depth: Integer

Make (C: Integer)

- Initialize An Empty Stack with Capacity C.Require

C> 0

DO

Capacity: = C

!! s.make (1, Capacity)

Current_Depth: = 0

Ensure

Capacity = C

DEPTH = 0

END - MAKE

end

转载请注明原文地址:https://www.9cbs.com/read-418.html

New Post(0)