Problem:

스택 클래스 Stack을 만들고 푸시(push)용으로 << 연산자를, 팝(pop)을 위해 >> 연산자를, 비어 있는 스택인지를 알기 위해 ! 연산자를 작성하라.

다음 코드를 main()으로 작성하라.

1 2 3 4 5 6 7 8 9 Stack stack; stack << 3 << 5 << 10; // 3,5,10 순서대로 push while(true){ if(!stack) break; //stack empty int x; stack >> x; //stack의 top에 있는 정수 pop cout << x << ' '; } cout << endl;

Execution Result:

Objective & Hints:

참조 리턴 등 참조자(&) 사용이 필요한 연산자 종합 응용

Code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include using namespace std; class Stack{ int stack[10]; int top; public: Stack(){ top=0; } Stack& operator<< (int num){ stack[top] = num; top++; return *this; } bool operator! (){ if(top) return false; return true; //top이 empty면 true 반환 } Stack operator>> (int& x){ x = stack[top-1]; top--; return *this; } }; int main() { Stack stack; stack << 3 << 5 << 10; // 3,5,10 순서대로 push while(true){ if(!stack) break; //stack empty int x; stack >> x; //stack의 top에 있는 정수 pop cout << x << ' '; } cout << endl; }