2019-02-12

Hyeonu_Chun·2021년 6월 21일
0

HW13

#include <iostream>
using namespace std;

class Time {
private:
	int hour;
	int min;
	double time;
	static int mode;
	enum { integer, real };
public:
	Time();
	Time(int, int);
	Time(double);
	Time(const Time &tr);
	Time plus(const Time &tr);
	void setHour(int);
	int getHour();
	void setMin(int);
	int getMin();
	int getMode();
	friend ostream &operator<<(ostream &os, const Time &br);
	static void mode_change();
};

int Time::mode = integer;

Time::Time() {
	this->hour = 0;
	this->min = 0;
	this->time = 0.0;
}

Time::Time(int hour,int min) {
	this->hour = hour;
	this->min = min;
	this->time = hour + double(min) / 60;
}

Time::Time(double time) {
	this->hour = int(time);
	this->min = int((time - int(time)) * 60);
	this->time = time;
}

Time::Time(const Time &tr) {
	this->hour = tr.hour;
	this->min = tr.min;
	this->time = tr.time;
}

Time Time::plus(const Time &tr){
	int sum = this->min + tr.min;
	this->hour += tr.hour;
	if (sum >= 60) {
		this->hour++;
		this->min = sum - 60
;
	}
	this->time = this->hour + double(this->min) / 60;
	return *this;
}

void Time::setHour(int hour) {
	this->hour = hour;
}

int Time::getHour() {
	return this->hour;
}

void Time::setMin(int min) {
	this->min = min;
}

int Time::getMin() {
	return this->min;
}

int Time::getMode() {
	return this->mode;
}

void Time::mode_change() {
	mode = (mode + real) % 2;
}

ostream &operator<<(ostream &os, Time &br);

int main() {
	Time a(3, 21);
	Time b(2.7);
	cout << "기본 출력 형태(시간, 분)..." << endl;
	cout << a << endl;
	cout << b << endl;
	Time::mode_change();
	cout << "출력모드를 바꾼 후..." << endl;
	cout << a << endl;
	cout << b << endl;
	return 0;
}

ostream &operator<<(ostream &os, Time &br) {
	double time = br.getHour() + double(br.getMin()) / 60;
	if (br.getMode() == 0) {
		os << br.getHour() << "시간 " << br.getMin() << "분";
	}
	else {
		os << time << "시간";
	}
	return os;
}

HW14

#include <iostream>
#include <cstring>
#pragma warning (disable : 4996)
using namespace std;

class MyString {
private:
	char *str;
	int len;
public:
	MyString();
	MyString(const char*cp);
	MyString(const MyString &br);
	~MyString();
	MyString &operator=(const MyString &br);
	MyString operator+(const MyString &br);
	bool operator>(const MyString &br);
	friend ostream &operator<<(ostream &os, MyString &br);
	friend istream &operator>>(istream &is, MyString &br);
	void setStr(const char *cp);
	char *getStr() const;
	int getLen() const;
};

ostream &operator<<(ostream &os, MyString &br);
istream &operator>>(istream &is, MyString &br);

MyString::MyString() {
	this->str = new char[strlen("") + 1];
	strcpy(this->str, ""); // this->str = ""; string 영역(상수영역)의 null string을 가리킴
	this->len = 0;
}

MyString::MyString(const char *cp) {
	this->len = strlen(cp);
	this->str = new char[this->len + 1];
	strcpy(this->str, cp);
}

MyString::MyString(const MyString &br) {
	this->len = br.len;
	this->str = new char[this->len + 1];
	strcpy(this->str, br.str);
}

MyString::~MyString() {
	delete[] this->str;
}

MyString &MyString::operator=(const MyString &br) {
	if (this == &br) { // **** 나 - 나 방지코드
		return *this;
	}
	delete[] this->str;
	this->len = br.len;
	this->str = new char[this->len + 1];
	strcpy(this->str, br.str);
	return *this;
}

MyString MyString::operator+(const MyString &br) {
	char temp[100] = "";
	strcpy(temp, this->str);
	strcat(temp, br.str);
	return MyString(temp);
	//char *cp;
	//cp = new char[this->len + br.len + 1];
	//strcpy(cp, this->str);
	//strcat(cp, br.str);
	//MyString temp(cp);
	//delete[] cp;
	//return temp;
}

bool MyString::operator>(const MyString &br) {
	//return (this->len > br.len); // 길이 비교
	return strcmp(this->str, br.str); // 스펠링 비교
}

void MyString::setStr(const char *cp) {
	if (this->str == cp) { // 본인의 문자열로 초기화시키려 할때
		return;
	}
	this->len = strlen(cp);
	delete[] this->str;
	this->str = new char[this->len + 1];
	strcpy(this->str, cp);
}

char *MyString::getStr() const{
	return this->str;
}

int MyString::getLen() const{
	return this->len;
}

int main() {
	MyString ary[5];
	MyString res;
	cout << "5개의 과일이름 입력 : ";
	for (int i = 0;i < 5;i++) {
		cin >> ary[i];
	}
	cout << "첫번째와 두번째 중 긴 과일 이름 : ";
	if (ary[0] > ary[1]) {
		cout << ary[0] << endl;
	}
	else {
		cout << ary[1] << endl;
	}
	res = ary[0] + ary[1] + ary[2] + ary[3] + ary[4];
	cout << "모든 문자열 출력 : " << res << endl;
	cout << "배열내의 문자열 출력..." << endl;
	for (int i = 0;i < 5;i++) {
		cout << ary[i] << endl;
	}
	for (int i = 0;i < 5;i++) {
		ary[i].setStr("abc");
		cout << ary[i].getStr();
		cout << ary[i].getLen();
	}
	return 0;
}

ostream &operator<<(ostream &os, MyString &br) {
	os << br.str << "(" << br.len << ")" << endl;
	return os;
}

istream &operator>>(istream &is, MyString &br) { // *****
	char temp[5000];
	is >> temp;
	delete[] br.str;
	br.len = strlen(temp);
	br.str = new char[br.len + 1];
	strcpy(br.str, temp);
	return is;
}

//int main() {
//	char temp[1000];
//	char str1[20] = "apple ";
//	char str2[20] = "pie";
//
//	strcpy(temp, str1); // temp : "apple "
//	strcat(temp, str2); // temp의 \0 자리에서부터 str2를 붙임
//	cout << temp << "" << str1 << "" << str2 << endl;
//
//	char temp2[1000] = "";
//	strcat(temp2, str1); // temp : "apple "
//	strcat(temp2, str2); // temp의 \0 자리에서부터 str2를 붙임
//	cout << temp2 << "" << str1 << "" << str2 << endl;
//}
profile
Stay hungry, stay foolish

0개의 댓글