2020-11-24 고급프로그래밍

Hyeonu_Chun·2021년 6월 22일
0

Absolute C++ 6th ed./Savitch Chap.10 Programming Project.4

  1. 문제 기술
    필자는 구독자의 구독리스트를 관리하는 프로그램의 구현을 요한다.

  2. 설계 계획
    클래스를 선언하여 오버로딩 생성자에 의해 사용자의 이름을 최초로 입력 받는다. 이후 사용자는 메뉴에서 리스트를 추가할 수 있는데 이때, 동적 할당을 이용해 받은 데이터만큼 새로운 string 방을 추가한다. 동시에 개수를 계산하여 채널 개수에 입력한다. 사용자는 이름과 리스트를 추가 및 리셋 시킬 수 있다.

  3. 데이터 처리 과정
    리스트를 생성 할 시 사용자로부터 이름을 입력 받고 해당 데이터를 임시변수에 저장한다. 기존에 리스트가 생성되어 있지 않다면 동적할당을 하여 새로운 리스트를 추가하고, 리스트가 생성되어 있다면 임시주솟값에 기존의 리스트주소를 저장하여 데이터를 보존하고 새롭게 동적할당을 하여 임시주솟값에 들어간 데이터와 새로운 데이터를 입력 받고 임시주솟값의 데이터를 해제한다.

  4. 실행 결과 및 분석

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

class Subscriber {
private:
	string name;
	int numChannels;
	string* channelList;
public:
	Subscriber();
	Subscriber(string);
	~Subscriber();
	void setName();
	void setList();
	void getInfo();
	void reset();
};

Subscriber::Subscriber(){
	cout << "Please write your name: ";
	cin >> this->name;
	this->numChannels = 0;
	this->channelList = NULL;
}

Subscriber::Subscriber(string name) {
	this->name = name;
	this->numChannels = 0;
	this->channelList = NULL;
}

Subscriber::~Subscriber() {
	delete[] this->channelList;
}

void Subscriber::setName(){
	cout << "Please write your name: ";
	cin >> this->name;
}

void Subscriber::setList() {
	string channel;
	cout << "Mr(s). " << this->name << ", please write names of the channels. If you want to quit, press q." << endl;
	while (true) {
		cin >> channel;
		if (channel == "q") break;
		else {
			if (this->numChannels == 0) {
				this->channelList = new string[this->numChannels + 1];
			}
			else if (this->numChannels > 0) {
				string* tmp = this->channelList;
				this->channelList = new string[this->numChannels + 1];
				for (int i = 0; i < this->numChannels; i++) {
					this->channelList[i] = tmp[i];
				}
				delete[] tmp;
			}
			this->channelList[this->numChannels] = channel;
			this->numChannels++;
		}
	}
}

void Subscriber::getInfo() {
	cout << "The list Mr(s). " << this->name << " subcribes :" << endl;
	if (this->numChannels == 0) cout << "Nothing in your list." << endl;
	for (int i = 0; i < this->numChannels; i++) cout << channelList[i] << endl;
	cout << "please press any button. ";
	cin.get();
	cin.get();
}

void Subscriber::reset() {
	this->name = "";
	this->numChannels = 0;
	delete[] this->channelList;
	this->channelList = NULL;
}

int main(){
	int num;
	Subscriber a;
	while (1) {
		cout << "1. Show your information / 2. Change your name / 3. Add your list / 4. Reset your information / 5. Quit\nPress the number: ";
		cin >> num;
		switch (num) {
		case 1: {
			a.getInfo();
		} break;
		case 2: {
			a.setName();
		} break;
		case 3: {
			a.setList();
		} break;
		case 4: {
			a.reset();
		} break;
		case 5: return 0;
		default: cout << "Wrong number" << endl;
		}
		system("cls");
	}
}

리스트를 구현하는 여러 방법이 있었지만 사용자가 얼마나 구독을 할지 모르는 상황에서 구독채널 개수의 한계를 없애기 위해서 링크드 리스트를 간략하게 나마 표현했다. Equal operator를 현 코드에는 구현하지 않았는데, c++에서 제공하는 오퍼레이터를 그대로 사용할 경우 얕은 복사가 되어 같은 주소를 가리키는 변수가 2개가 생기게 되어 원치 않은 데이터의 손실이 생길 수 있다. 따라서 새로운 동적할당을 하여 변수가 각각 다른 주솟값을 가리키게 하는 것이 바람직하다.

profile
Stay hungry, stay foolish

0개의 댓글