Title: 11.2 Ada? ?? ???
1? 11? ?? ???
11.1 ??
11.2 Ada? ?? ???
11.3 C? ?? ???
11.4 Java? ?? ???
11.5 ??? ??? ??
2?? ???
11.1 ??
?? ??? (data abstraction)
?? ??? (data encapsulation)
??? ??? ?? ??
?? ?? ?? gt ???? ?? ?? ?? ?? ??
???
??? ??
??? ??, ??
3?? ???
?? ???
???? ??? ?? ??? ???? ?? ?? ????? ??
(class, package, structure ??)
???
???? ?????? ??? ???? ?? ?
Window ??
??? (public part)
??? (private part)
?? ?? export
??? import
4?? ???
Queue ???
structure queue operations ADDQ, DELETEQ,
ISEMPTYQ representation ...
end rep procedure ADDQ(...) . . .
end ADDQ procedure DELETEQ(...)
. . . end DELETEQ procedure
ISEMPTYQ(...) . . . end ISEMPTYQ
procedure NEXT(...) . . . end
NEXT begin initialization code
end end queue
- ltqueue ??gt
- ??
- 1) ?? ??
- 2) ??? ??
- 3) ?? ??
- 4) ???
- operations
- - ??? ??
- representation end rep
- - ?? ?? ??
- queue ? ?? ??
- var xqueue
- ??
- NEXT ???? ??
5?? ???
??? queue? ??
structure operations ADDQ,DELETEQ,
ISEMPTYQ representation integer array
q(099) integer front, rear end rep
procedure NEXT(Iinteger) i (i 1) mod
100 end NEXT procedure ADDQ(pqueue,
iteminteger) NEXT(rear) if front
rear then QUEUFULL else q(rear) item
endif end ADDQ
procedure DELETEQ(qqueue) returns
iteminteger if ISEMFTYQ(q) then
QUEUEEMPTY else NEXT(front)
item q(front)
endif end DELETEQ procedure
ISEMPTYQ(qqueue) returns
flagboolean flag front rear end
ISEMPTYQ begin front rear 1
end end queue
6?? ???
11.2 Ada? ?? ???
?? ????
subprogramm(procedure, function)
package
task
7?? ???
package
?? ??? ??
??
??? ? ??? - with(?? ??) , use(??? ??? ??? ??) ?
??? - ?? ?? ???
??? - ??(? ????) ??
?? - ???, ???? ?? lt- own ??
8?? ???
?? ?? package ???, ???
lt???gt
package BSTREES is type BSTREEPTR is private
type BSTREE is private function HAS(IITEM,
PBSTREEPTR) return BOOLEAN procedure
INSERT(ILITEM, in out PBSTREEPTR ) function
EQUAL(P, QBSTREEPTR) return BOOLEAN
private type BSTREEPTR type BSTREE is
record DATEITEM
LEFTCHILDBSTREESPTR
RIGHTCHILDBSTREEPTR end record type
BSTREEPTR is access BSTREE end
9?? ???
package body BSTREES is function
HAS(IITEM,PBSTREEPTR) return BOOLEAN
is QBSTREEPTR begin Q P
loop if Q null then return FALSE
elsif I lt Q.DATA then Q Q.LEFTCHILD
elsif I gt Q.DATA then Q Q.RIGHTCHILD
else return TRUE end if end
loop end HAS procedure INSERT(IITEM, in out
PBSTREEPTR) --???? INSERT? ?? function
EQUAL(P, QBSTREEPTR) return BOOLEAN if P
null and Q null then return TRUE elsif P
null or Q null then return FALSE elsif
P.DATA Q.DATA then if EQUAL
(P.LEFTCHILD, Q.LEFTCHILD) then
return EQUAL (P.RIGHTCHILD, Q.RIGHTCHILD)
else return FALSE end if else
return FALSE end if end BSTREES
lt???gt
10?? ???
BSTREE ??? ??
declare use BSTREES P,Q BSTREEPTR
begin p new BSTREE('A', null, null)
INSERT('B', P) INSERT('Z', P)
INSERT('H', P) end
11?? ???
?? ??? (generic stack package)
generic SIZEINTEGER type ELEM is
private package STACKS is type STACK is
limited private procedure PUSH(Sin out
STACK Ein ELEM) procedure POP(Sin out
STACK Eout ELEM) OVERFLOW, UNDERFLOW
exception private type STACK is
record SPACEarray(1 .. SIZE) of ELEM
INDEXINTEGER range 0 .. SIZE 0
end record end package body STACKS is
procedure PUSH(Sin out STACK Ein ELEM) is
begin if S.INDEX SIZE then raise
OVERFLOW endif S.INDEX S.INDEX
1 S.SPACE(S.INDEX) E end
PUSH procedure POP(Sin out STACK Eout
ELEM) is begin if S.INDEX 0 then
raise UNDERFLOW endif E
S.SPACE(S.INDEX) S.INDEX S.INDEX -
1 end POP end STACKS
- ?? ???(STACK ??)
- - ?? ??? ??? ?????
- ??
- PUSH(), POP()
- ????
- OVERFLOW, UNDERFLOW
- ???
- package INTSTACK is new
- STACK(SIZE gt100, ELEMgtINTEGER)
- ??
- declare
- use INTSTACK
- X, YSTACK
- ...
- begin
- PUSH(X, 175)
- PUSH(Y, 82)
- POP(X, I)
- POP(Y, J)
1211.3 C ??? ?? ??? ?
- include ltiostream.hgt
- class stack
- private
- int stack_ptr
- int max_len
- int top_ptr
- public
- stack() stack_ptr new int 100
- max_len 99
- top_ptr -1
- stack() delete stack_ptr
- void push (int number)
- if (top_ptr max_len)
- cout ltlt Error in push-stack is
full\n - else stack_ptrtop_pt number
-
- void pop()
- if (top_ptr -1)
- cout ltlt Error in pop-stack is empty\n
13?? ?? ???? ???? ?
void main() int top_one stack stk
stk.push(42) stk.push(17) stk.pop()
top_one stk.top() .
14(???) stack lt int gt stk(150)
- include ltiostream.hgt
- template ltclass Typegt
- class stack
- private
- Type stack_ptr
- int max_len
- int top_ptr
- public
- stack() stack_ptr new Type100
- max_len 99
- top_ptr -1
- stack (int size) stack_ptr new Typesize
- max_len size-1
- top -1
- stack() delete stack_ptr
- void push (Type number)
- if (top_ptr max_len)
- cout ltlt Error in push-stack is full\n
- else stack_ptrtop_ptrnumber
? 10.7 C ?? ?? ?? ?? ??? ?
1511.4 Java ??? ?? ??? ?
import java.io. Class Stack_class
private int stack_ref private int max_len,
top_index public Stack_class() // A
Constructor stack_ref new int 100
max_len 99 top_index -1
public void push(int number) if
(top_index max_len)
System.out.println("Error in push stack is
full") else stack_ref top_index
number public void pop() if
(top_index -1)
System.out.println("Error in pop-stack is
empty") else --top_index public
int top() return (stack_reftop_index)
public boolean empty() return (top_index
-1)
16Java ?? stack ??? ???
public class Tst_Stack public static void
main(String args) Stack_class myStack
new Stack_class() myStack.push(42)
myStack.push(17) System.out.println(" 17
is " myStack.top()) myStack.pop()
System.out.println(" 42 is " myStack.top())
myStack.pop() myStack.pop() //
produces an error message
17?? ???
11.5 ??? ??? ??
???? ??? ?? ??
?? ???
???? ?? ??
?? ??? ?? ??
??? ??
?? ?? ??? ??, ??, ??? ?? ??? ??
?? ?? ?? ?? ??
?? ?? ?? ?? ??? ??? gt ???
18?? ???
lt?? ifthenelse() ??gt
ifthenelse(true, g, r) q ifthenelse(false, q,
r) r if p then q else r
Infix ??
19?? ???