반응형

문제

정수 배열을 항상 증가 순으로 유지하는 SortedArray 클래스를 작성하려고 한다. 아래의 main() 함수가 작동할 만큼만 SortedArray 클래스를 작성하고 +와 = 연산자도 작성하라

class SortedArray {
	int size; //현재 배열의 크기
	int* p;
	void sort();
public:
	SortedArray(); //p는 NULL로 size는 0으로 초기화
	SortedArray(SortedArray& src); //복사 생성자
	SortedArray(int p[], int size); //생성자, 정수 배열과 크기를 전달받음
	~SortedArray(); //소멸자
	SortedArray operator +(SortedArray& op2); //현재 배열에 op2 배열 추가
	SortedArray& operator = (const SortedArray& op2); //현재 배열에 op2 배열 복사
	void show(); //배열의 원소 출력
};

int main()
{
	int n[] = { 2,20,6 };
	int m[] = { 10,7,8,30 };
	SortedArray a(n, 3), b(m, 4), c;

	c = a + b; // +, = 연산자 작성 필요
	// + 연산자가 SortedArray 객체를 리턴하므로 복사 생성자 필요

	a.show();
	b.show();
	c.show();
}

 

결과

배열 출력 : 2 6 20

배열 출력 : 7 8 10 30

배열 출력 : 2 6 7 8 10 20 30

 

소스코드

#include<iostream>
using namespace std;
class SortedArray {
	int size; //현재 배열의 크기
	int* p;
	void sort();
public:
	SortedArray(); //p는 NULL로 size는 0으로 초기화
	SortedArray(SortedArray& src); //복사 생성자
	SortedArray(int p[], int size); //생성자, 정수 배열과 크기를 전달받음
	~SortedArray(); //소멸자
	SortedArray operator +(SortedArray& op2); //현재 배열에 op2 배열 추가
	SortedArray& operator = (const SortedArray& op2); //현재 배열에 op2 배열 복사
	void show(); //배열의 원소 출력
};
void SortedArray::sort() //정렬함수
{
	int temp;
	for (int i = 0; i < this->size - 1; i++)
	{
		for (int j = i + 1; j < this->size; j++)
		{
			if (p[i] > p[j]) {
				temp = p[i];
				p[i] = p[j];
				p[j] = temp;
			}
		}
	}
}
SortedArray::SortedArray() //생성자
{
	p = nullptr;
	size = 0;
}
SortedArray::SortedArray(SortedArray& src) //복사생성자
{
	this->size = src.size;
	this->p = new int[this->size];
	for (int i = 0; i < this->size; i++)
		this->p[i] = src.p[i];
}
SortedArray::SortedArray(int p[], int size) //매개변수 생성자
{
	this->size = size;
	this->p = new int[size];
	for (int i = 0; i < size; i++)
		this->p[i] = p[i];
}
SortedArray::~SortedArray() //소멸자
{
	delete[] p;
}
SortedArray SortedArray::operator+(SortedArray& op2)
{
	SortedArray op3;
	op3.size = this->size + op2.size;
	op3.p = new int[op3.size];
	for (int i = 0; i < op3.size; i++)
	{
		if (i < this->size)
			op3.p[i] = this->p[i];
		else
			op3.p[i] = op2.p[i - this->size];
	}
	return op3;
}
SortedArray& SortedArray::operator=(const SortedArray& op2)
{
	this->size = op2.size;
	this->p = new int[this->size];
	for (int i = 0; i < this->size; i++)
	{
		this->p[i] = op2.p[i];
	}
	return *this;
}
void SortedArray::show()
{
	this->sort(); //정렬
	cout << "배열 출력 : ";
	for (int i = 0; i < this->size; i++)
		cout << this->p[i] << ' ';
	cout << endl;
}
int main()
{
	int n[] = { 2,20,6 };
	int m[] = { 10,7,8,30 };
	SortedArray a(n, 3), b(m, 4), c;

	c = a + b; // +, = 연산자 작성 필요
	// + 연산자가 SortedArray 객체를 리턴하므로 복사 생성자 필요

	a.show();
	b.show();
	c.show();
}
반응형
반응형

문제

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

int main()
{
	Stack stack;
	stack << 3 << 5 << 10; // 3, 5, 10을 순서대로 푸시
	while (true) {
		if (!stack)break; // 스택 empty
		int x;
		stack >> x; // 스택의 탑에 있는 정수 팝
		cout << x << ' ';
	}
	cout << endl;
}

 

결과

10 5 3

 

스택이란?

스택은 자료구조 중에 하나로서, 후입선출(LIFO : Last-In First-Out)구조이다. 데이터가 들어간 순서의 역순이 나오는 순서이다.

하나의 스택 스택 입력(B) 스택 입력(C) 스택 삭제(C)
    C  
  B B B
A A A A

 

 

소스코드(friend 함수 구현)

#include<iostream>
using namespace std;
class Stack {
	int stack[10];
	int top;
public:
	Stack() { top = -1; }
	friend bool operator!(Stack& sta);
	friend Stack& operator<<(Stack& sta, int n);
	friend void operator>>(Stack& sta, int& n);
};
bool operator!(Stack& sta)
{
	if (sta.top == -1)
		return true;
	return false;
}
Stack& operator<<(Stack& sta, int n)
{
	sta.stack[++sta.top] = n;
	return sta;
}
void operator>>(Stack& sta, int& n)
{
	n = sta.stack[sta.top--];
}
int main()
{
	Stack stack;
	stack << 3 << 5 << 10; // 3, 5, 10을 순서대로 푸시
	while (true) {
		if (!stack)break; // 스택 empty
		int x;
		stack >> x; // 스택의 탑에 있는 정수 팝
		cout << x << ' ';
	}
	cout << endl;
}

 

 

반응형
반응형

문제

통계를 내는 Statistics 클래스를 만들려고 한다. 데이터는 Statistics 클래스 내부에 int 배열을 동적으로 할당받아 유지한다. 다음과 같은 연산이 잘 이루어지도록 Statistics 클래스와 !, >>, <<, ~연산자 함수를 작성하라.

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;
}

결과

현재 통계 데이타가 없습니다.

5 개의 정수를 입력하라 >> 1 2 3 4 5

1 2 3 4 5 100 200

avg=45

 

소스코드

#include<iostream>
using namespace std;
class Statistics {
	int* a;
	int size=0;
public:
	Statistics() { a = new int[10]; size = 0; }
	friend bool operator!(Statistics& stat);
	friend Statistics& operator<<(Statistics& stat,int x);
	friend void operator>>(Statistics& stat,int& avg);
	friend void operator~(Statistics& stat);
	~Statistics() { delete[]a; }
};
bool operator!(Statistics& stat)
{
	if (stat.size == 0) return true;
	return false;
}
Statistics& operator<<(Statistics& stat,int x)
{
	stat.a[stat.size++] = x;
	return stat;
}
void operator>>(Statistics& stat,int& avg)
{
	avg = 0;
	for (int i = 0; i < stat.size; i++)
		avg += stat.a[i];
	avg /= stat.size;
}
void operator~(Statistics& stat)
{
	for (int i = 0; i < stat.size; i++)
		cout << stat.a[i] << ' ';
	cout << endl;
}
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;
}
반응형

+ Recent posts