C++ report 스택 클래스, 행렬 클래스
- 최초 등록일
- 2024.06.13
- 최종 저작일
- 2010.06
- 9페이지/
한컴오피스
- 가격 1,500원
![할인쿠폰받기](/images/v4/document/ico_det_coupon.gif)
목차
없음
본문내용
3. 스택 클래스 선언 및 정의
// **** file: stack.h
#include <iostream>
using namespace std;
class Stack {
public:
enum { MaxStack = 5 };
void init() { top = -1; }
void push( int n ) {
if ( isFull() ) {
errMsg( "Full stack. Can't push." );
return;
}
arr[ ++top ] = n;
}
int pop() {
if ( isEmpty() ) {
errMsg( "Empty stack. Popping dummy value." );
return dummy_val;
}
return arr[ top-- ];
}
bool isEmpty() { return top < 0; }
bool isFull() { return top >= MaxStack - 1; }
void dump() {
cout << "Stack contents, top to bottom:\n";
for ( int i = top; i >= 0; i-- )
cout << '\t' << arr[ i ] << '\n';
}
private:
void errMsg( const char* msg ) const {
cerr << "\n*** Stack operation failure: " << msg << '\n';
}
int top;
int arr[ MaxStack ];
int dummy_val;
};
4. 수행 프로그램
//**** file: stack.cpp
#include "stack.h" // header for Stack class
참고 자료
없음