2019-02-13

Hyeonu_Chun·2021년 6월 21일
0

HW16

//Happy.h
#pragma once
#include <string>
using namespace std;

class Happy {
private:
	string name;
	int money;
	char *list[100] = { NULL, };
	int index;
public:
	Happy(string np = "", int money = 10000);
	Happy(Happy &r);
	~Happy();
	Happy &operator=(Happy &r);
	void use(char *lp, int n);
	Happy &winner(Happy &r);
	void setName(string &name);
	string &getName();
	void setMoney(int money);
	int getMoney();
	char **getList();
	int getIndex();
};
//Happy.cpp
#include <iostream>
#include <string>
#include "Happy.h"
#pragma warning (disable : 4996)
using namespace std;

Happy::Happy(string np, int n) {
	this->name = np;
	this->money = n;
	this->index = 0;
}

Happy::Happy(Happy &r) {
	int i = 0;
	this->name = r.name;
	this->money = r.money;
	this->index = r.index;
	while (r.list[i] != NULL) {
		this->list[i] = new char[strlen(r.list[i]) + 1];
		strcpy(this->list[i], r.list[i]);
		i++;
	}
}

Happy::~Happy() {
	int i = 0;
	while (this->list[i] != NULL) {
		delete[] this->list[i];
		i++;
	}
}

Happy &Happy::operator=(Happy &r) {
	int i = 0;
	this->name = r.name;
	this->money = r.money;
	this->index = r.index;
	while (r.list[i] != NULL) {
		this->list[i] = new char[strlen(r.list[i]) + 1];
		strcpy(this->list[i], r.list[i]);
		i++;
	}
	return *this;
}

void Happy::use(char *lp, int n) {
	int i = 0;
	int len = strlen(lp) + 2;
	if (this->money >= n) {
		this->money -= n;
	}
	else cout << "잔액이 부족합니다." << endl;
	this->index++;
	while (1) {
		if (this->list[i] == NULL) {
			this->list[i] = new char[len];
			strcpy(this->list[i], lp);
			break;
		}
		else i++;
	}
}

Happy &Happy::winner(Happy &r) {
	if (this->money > r.money) {
		return *this;
	}
	else {//if (this->money < r.money) {
		return r;
	}
	//else return Happy();
}

void Happy::setName(string &name) {
	this->name = name;
}

string &Happy::getName() {
	return this->name;
}

void Happy::setMoney(int money) {
	this->money = money;
}

int Happy::getMoney() {
	return this->money;
}

char **Happy::getList() {
	return this->list;
}

int Happy::getIndex() {
	return this->index;
}
//HW16.cpp
#include <iostream>
#include "Happy.h"
#pragma warning (disable : 4996)
using namespace std;

int main() {
	Happy a("철이"), b("메텔"), w;
	char item[100];
	int price;

	cout << "철이가 돈을 씁니다..." << endl;
	while (1) {
		cout << "구입 내역 : ";
		cin >> item;
		if (strcmp(item, "끝") == 0)break;
		cout << "금액 입력 : ";
		cin >> price;
		a.use(item, price);
	}

	cout << "\n메텔이 돈을 씁니다..." << endl;
	while (1) {
		cout << "구입 내역 : ";
		cin >> item;
		if (strcmp(item, "끝") == 0)break;
		cout << "금액 입력 : ";
		cin >> price;
		b.use(item, price);
	}

	cout << "\n최종 승자는?" << endl;
	w = a.winner(b);
	cout << w.getName() << "이고 총 " << w.getIndex() << "건을 사용하고 남은 금액은 " << w.getMoney() << "원 입니다." << endl;
	
	int i = 0;
	cout << "사용 내역 : ";
	while (w.getList()[i]) {
		cout << w.getList()[i] << " ";
		i++;
	}
	return 0;
}
profile
Stay hungry, stay foolish

0개의 댓글