/* ( n5.C ) NIHクラス・ライブラリ [ Stack ] 機能:スタック(重複要素もよい) Stack s(10) Stack クラスを定義する(スタックのサイズを 10 で定義する)。 s.push(A) スタックに要素を入れる *(s.top()) スタックの先頭の要素を取り出す *(s.pop()) 先頭の要素を取り出し、除去する s.size() スタックに入っている要素の数を取り出す s.reSize(20) スタックのサイズを変更する。 s.capacity() スタックのサイズを取り出す s.removeAll() スタックの要素を全てクリアする s.isEmpty() スタックが空か調べる。空なら1、でなければ0を返す。 s.includes(C) スタックの中に要素 C と同じ内容があるか調べる。あれば1、なければ0を返す。 s.printOn(cout) cout << s; と同じ意味 s.add(A) s.push(A) と同じ? */ #include "Stack.h" #include "Point.h" #include "Rectangle.h" main() { Point A(1,2); Point B(3,4); Stack s(10); // Stack s; デフォルトのサイズは16である。 s.push(A); s.push(B); cout << "s = " << s << endl; // (3,4)(1,2) cout << "top : " << *(s.top()) << endl; // (3,4) スタックの先頭を取り出す cout << "pop : " << *(s.pop()) << endl; // (3,4) 先頭を取り出し、除去する cout << "s = " << s << endl; // (1,2) Point* C = new Point(5,6); s.push(*C); s.printOn(cout) ; cout << endl ; // (5,6)(1,2)、cout << s; と同じ意味 cout << "num = " << s.size() << endl ; // 2 : スタックに入っている要素の数 s.reSize(20) ; // スタックのサイズを変更する。 cout << "size = " << s.capacity() << endl ; // Size = 20 Point D(1,2); cout << "judge: " << s.includes(D) << endl; // 1 : 同じ内容の要素がある。 while ( !s.isEmpty() ) s.pop() ; // スタックをクリアする。s.removeAll()でもよい。 /* Rectangle a(0,0,10,10); Point AA(1,2); Object* CC = AA.copy(); Stack t(10); t.push(a); t.push(AA); cout << "top compare C = " << t.top()->compare(*CC) << endl; */ }