본문 바로가기

언어/C++

7장 9번 - 통계 클래스 만들기

#include <iostream>
using namespace std;

class Statistics {
	int *p;
	int count;
public :
	Statistics(){ p = new int[10]; count=0; }

	bool operator ! () {
		if( count == 0 ) return true;
		else return false;
	}
	Statistics& operator << (int n) {
		p[count] = n;
		count++;
		return *this;
	};
	void operator ~ () {
		for(int i=0; i< count; i++)
			cout << p[i] << ' ';
		cout << endl;
	}
	void operator >>(int& avg) {
		int sum=0;
		for(int i=0;i< count;i++)
			sum += p[i];
		avg = sum / count;
	}
};

int main() {
	Statistics stat;
	if(!stat) cout << "현재 통계 데이타가 없습니다." << endl;

	int x[5];
	cout << "5 개의 정수를 입력하라>>";
	for(int i=0; i<5; i++) cin >> x[i];

	for(int i=0; i<5; i++) stat << x[i];
	stat << 100 << 200;
	~stat;

	int avg;
	stat >> avg;
	cout << "avg=" << avg << endl;
}