hugDog
Android DevLog
hugDog
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๐Ÿ™Œ Hello? (162)
    • ๐Ÿงฉ์•ˆ๋“œ๋กœ์ด๋“œ (12)
      • ๊ฐœ๋… ์ •๋ฆฌ (5)
      • ๋ฒ„๊ทธ ํ•ด๊ฒฐ (4)
      • ๊ธฐํƒ€ (3)
    • ๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜ (54)
      • ๊ฐœ๋… (0)
      • ๋ฐฑ์ค€ (48)
      • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (6)
    • ๐Ÿ“„๊ฐœ๋ฐœ ์ผ์ง€ (0)
      • FINPO (0)
    • ๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด (71)
      • C++ ์ •๋ฆฌ (49)
      • C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ (20)
      • Kotlin (2)
    • โญProject (1)
    • ๐ŸšดTIL (13)
      • Clean Code (13)
    • ๐Ÿšฉ๊ธฐํƒ€ (9)
      • ๋ชฉํ‘œ (6)
      • ์ผ์ƒ (3)
      • ๋ฌธ์„œ (0)

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
hugDog

Android DevLog

๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ

(C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 17 ์ž…๋ ฅ, ์ถœ๋ ฅ, ํŒŒ์ผ p.1458~ 1๋ฒˆ~7๋ฒˆ

2020. 10. 26. 21:51
728x90

1๋ฒˆ

#include<iostream>
#include<sstream>
#include<string>
int main()
{
	using namespace std;
	char ch;
	int count = 0;
	while (cin.get(ch))
	{
		if (ch != '$')
			count++;
		else
		{
			cin.putback(ch);
			break;
		}
	}
	cout << "๋ฌธ์ž ์ˆ˜ : " << count << endl;
	cout << "์ž…๋ ฅ์ŠคํŠธ๋ฆผ์— " << ch << "๊ฐ€ ๋‚จ์•„์žˆ์Œ\n";

	return 0;
}

 

2๋ฒˆ

#include<iostream>
#include<fstream>
#include<cstdlib>

int main(int argc, char* argv[])
{
	using namespace std;
	ofstream fout(argv[1],ios_base::app | ios_base::out);
	char ch;
	while (cin.get(ch) && ch!='\n')
		fout << ch;
	return 0;
}

 

3๋ฒˆ

#include<iostream>
#include<fstream>
#include<cstdlib>

int main(int argc, char* argv[])
{
	using namespace std;
	ifstream fin(argv[1]);
	ofstream fout(argv[2],ios_base::app | ios_base::out);
	if (!fin.is_open() || !fout.is_open())
	{
		cout << "ํŒŒ์ผ ์—ด๊ธฐ ์‹คํŒจ!\n";
		exit(EXIT_FAILURE);
	}
	char ch;
	while (fin.get(ch))
		fout << ch;
	fin.close();
	fout.close();
	return 0;
}

 

4๋ฒˆ

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>

int main(int argc, char* argv[])
{
	using namespace std;
	ifstream f1(argv[1]);
	ifstream f2(argv[2]);
	ofstream fout(argv[3],ios_base::app | ios_base::out);
	if (!f1.is_open() || !fout.is_open() ||!f2.is_open())
	{
		cout << "ํŒŒ์ผ ์—ด๊ธฐ ์‹คํŒจ!\n";
		exit(EXIT_FAILURE);
	}
	string str;
	while (!f1.eof() || !f2.eof())
	{
		if (!f1.eof())
		{
			getline(f1, str);
			fout << str << " ";
		}

		if (!f2.eof())
		{
			getline(f2, str);
			fout << str << endl;
		}
	}
	f1.close();
	f2.close();
	fout.close();
	return 0;
}

 

5๋ฒˆ

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<set>
#include<iterator>
#include<algorithm>
using namespace std;


void input(set<string>& s,ifstream & f);

int main(int argc, char* argv[])
{
	ifstream f1(argv[1]);
	ifstream f2(argv[2]);
	ofstream fout(argv[3],ios_base::app | ios_base::out);
	if (!f1.is_open() || !fout.is_open() ||!f2.is_open())
	{
		cout << "ํŒŒ์ผ ์—ด๊ธฐ ์‹คํŒจ!\n";
		exit(EXIT_FAILURE);
	}

	set<string> Mat, Pat;
	input(Mat, f1);
	input(Pat, f2);

	ostream_iterator<string, char> out(cout, " ");
	ostream_iterator<string, char> fo(fout, "\n");

	cout << "Mat๊ฐ€ ์ž…๋ ฅํ•œ ์นœ๊ตฌ๋ชฉ๋ก\n";
	copy(Mat.begin(), Mat.end(), out);
	cout << endl;

	cout << "Pat๊ฐ€ ์ž…๋ ฅํ•œ ์นœ๊ตฌ๋ชฉ๋ก\n";
	copy(Pat.begin(), Pat.end(), out);
	cout << endl;

	set<string> MatPat;
	set_union(Mat.begin(), Mat.end(), Pat.begin(), Pat.end(),
		insert_iterator<set<string>>(MatPat, MatPat.begin()));

	copy(MatPat.begin(), MatPat.end(), fo);

	f1.close();
	f2.close();
	fout.close();
	return 0;
}

void input(set<string>& s, ifstream& f)
{
	string temp;
	while (!f.eof())
	{
		getline(f, temp);
		s.insert(temp);
	}
}

 

6๋ฒˆ

//emp.h

#include<iostream>
#include<string>
#include<fstream>
enum classkind{Employee,Manager,Fink,HighFink};

class abstr_emp
{
private:
	std::string fname; //abstr_emp์˜ ํผ์ŠคํŠธ ๋„ค์ž„
	std::string lname; //abstr_emp์˜ ๋ผ์ŠคํŠธ ๋„ค์ž„
	std::string job; //abstr_emp์˜ ์ง๋ฌด
public:
	abstr_emp();
	abstr_emp(const std::string& fn, const std::string& ln, const std::string& j);
	virtual void ShowAll() const; //๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
	virtual void writeall(std::ofstream & f) const;
	virtual void getall(std::ifstream& f);
	virtual void SetAll(); //์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฐ’๋“ค์„ ์ž…๋ ฅํ•˜๋ผ๊ณ  ์š”๊ตฌํ•œ๋‹ค.
	friend std::ostream& operator<<(std::ostream& os, const abstr_emp& e);
	//ํผ์ŠคํŠธ ๋„ค์ž„๊ณผ ๋ผ์ŠคํŠธ ๋„ค์ž„์„ ๋””์Šคํ”Œ๋ ˆ์ด ํ•œ๋‹ค.
	virtual ~abstr_emp() = 0;
};

class employee :public abstr_emp
{
public:
	employee();
	employee(const std::string& fn, const std::string& ln, const std::string& j);
	virtual void ShowAll() const;
	virtual void writeall(std::ofstream& f) const;
	virtual void getall(std::ifstream& f);
	virtual void SetAll();
};

class manager :virtual public abstr_emp
{
private:
	int inchargeof;
protected:
	int InChargeOf() const { return inchargeof; }//์ถœ๋ ฅ
	int& InChargeOf() { return inchargeof; }//์ž…๋ ฅ
public:
	manager();
	manager(const std::string& fn, const std::string& ln, const std::string& j, int ico = 0);
	manager(const abstr_emp& e, int ico);
	manager(const manager& m);
	virtual void ShowAll() const;
	virtual void writeall(std::ofstream& f) const;
	virtual void getall(std::ifstream& f);
	virtual void SetAll();
};

class fink :virtual public abstr_emp
{
private:
	std::string reportsto;
protected:
	const std::string ReportsTo() const { return reportsto; }
	std::string& ReportsTo() { return reportsto; }
public:
	fink();
	fink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo);
	fink(const abstr_emp& e, const std::string& rpo);
	fink(const fink& e);
	virtual void ShowAll() const;
	virtual void writeall(std::ofstream& f) const;
	virtual void getall(std::ifstream& f);
	virtual void SetAll();
};

class highfink :public manager, public fink
{
public:
	highfink();
	highfink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo, int ico);
	highfink(const abstr_emp& e, const std::string& rpo, int ico);
	highfink(const fink& f, int ico);
	highfink(const manager& m, const std::string& rpo);
	highfink(const highfink& h);
	virtual void ShowAll() const;
	virtual void writeall(std::ofstream& f) const;
	virtual void getall(std::ifstream& f);
	virtual void SetAll();
};





// emp.cpp
#include"emp.h"
using std::cout;
using std::endl;
using std::cin;
abstr_emp::abstr_emp() :fname("none"), lname("none"), job("none")
{

}


abstr_emp::abstr_emp(const std::string& fn, const std::string& ln, const std::string& j)
	:fname(fn), lname(ln), job(j) {}

void abstr_emp::ShowAll() const //๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
{
	cout << "์„ฑ : " << fname << "\n์ด๋ฆ„ : " << lname << endl;
	cout << "์ง์—… : " << job << endl;
}

void abstr_emp::writeall(std::ofstream& f) const
{
	f << fname << "\n" << lname << endl;
	f << job << endl;
}

void abstr_emp::getall(std::ifstream& f)
{
	getline(f, fname);
	getline(f, lname);
	getline(f, job);
}
void abstr_emp::SetAll() //์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฐ’๋“ค์„ ์ž…๋ ฅํ•˜๋ผ๊ณ  ์š”๊ตฌํ•œ๋‹ค.
{
	cout << "ํผ์ŠคํŠธ ๋„ค์ž„ ์ž…๋ ฅ : "; getline(cin, fname);
	cout << "๋ผ์ŠคํŠธ ๋„ค์ž„ ์ž…๋ ฅ : "; getline(cin, lname);
	cout << "์ง์—… ์ž…๋ ฅ : "; getline(cin, job);
}
std::ostream& operator<<(std::ostream& os, const abstr_emp& e)
//ํผ์ŠคํŠธ ๋„ค์ž„๊ณผ ๋ผ์ŠคํŠธ ๋„ค์ž„์„ ๋””์Šคํ”Œ๋ ˆ์ด ํ•œ๋‹ค.
{
	os << e.fname << ", " << e.lname << endl;
	return os;
}

abstr_emp::~abstr_emp() {}

employee::employee() :abstr_emp() {}
employee::employee(const std::string& fn, const std::string& ln, const std::string& j)
	: abstr_emp(fn, ln, j) {}
void employee::ShowAll() const
{
	abstr_emp::ShowAll();
}

void employee::writeall(std::ofstream& f) const
{
	abstr_emp::writeall(f);
}

void employee::getall(std::ifstream& f)
{
	abstr_emp::getall(f);
}
void employee::SetAll()
{
	abstr_emp::SetAll();
}

manager::manager() :abstr_emp(), inchargeof(0) {}
manager::manager(const std::string& fn, const std::string& ln, const std::string& j, int ico)
	: abstr_emp(fn, ln, j), inchargeof(ico) {}
manager::manager(const abstr_emp& e, int ico)
	: abstr_emp(e), inchargeof(ico) {}
manager::manager(const manager& m)
	: abstr_emp(m) {
	inchargeof = m.inchargeof;
}

void manager::ShowAll() const
{
	abstr_emp::ShowAll();
	cout << "inchargeof : " << inchargeof << endl;
}

void manager::writeall(std::ofstream& f) const
{
	abstr_emp::writeall(f);
	f << inchargeof << endl;
}

void manager::getall(std::ifstream& f)
{
	abstr_emp::getall(f);
	f >> inchargeof;
	while (f.get() != '\n')
		continue;
}

void manager::SetAll()
{
	abstr_emp::SetAll();
	cout << "inchargeof ์ž…๋ ฅ : "; cin >> inchargeof;
	while (cin.get() != '\n')
		continue;
}

fink::fink() : abstr_emp(), reportsto("none") {}
fink::fink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo)
	: abstr_emp(fn, ln, j), reportsto(rpo) {}
fink::fink(const abstr_emp& e, const std::string& rpo)
	: abstr_emp(e), reportsto(rpo) {}
fink::fink(const fink& e)
	: abstr_emp(e) {}
void fink::ShowAll() const
{
	abstr_emp::ShowAll();
	cout << "reportsto : " << reportsto << endl;
}

void fink::writeall(std::ofstream& f) const
{
	abstr_emp::writeall(f);
	f << reportsto << endl;
}
void fink::getall(std::ifstream& f)
{
	abstr_emp::getall(f);
	f >> reportsto;
	while (f.get() != '\n')
		continue;
}

void fink::SetAll()
{
	abstr_emp::SetAll();
	cout << "reportsto ์ž…๋ ฅ : "; cin >> reportsto;
	while (cin.get() != '\n')
		continue;
}

highfink::highfink() : abstr_emp(), manager(), fink() {}
highfink::highfink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo, int ico)
	: abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, rpo) {}
highfink::highfink(const abstr_emp& e, const std::string& rpo, int ico)
	: abstr_emp(e), manager(e, ico), fink(e, rpo) {}
highfink::highfink(const fink& f, int ico)
	: abstr_emp(f), manager(f, ico), fink(f) {}
highfink::highfink(const manager& m, const std::string& rpo)
	: abstr_emp(m), manager(m), fink(m, rpo) {}
highfink::highfink(const highfink& h)
	: abstr_emp(h), manager(h), fink(h) {}
void highfink::ShowAll() const
{
	abstr_emp::ShowAll();
	cout << "inchargeof : " << manager::InChargeOf() << endl;
	cout << "reportsto : " << fink::ReportsTo() << endl;
}

void highfink::writeall(std::ofstream& f) const
{
	abstr_emp::writeall(f);
	f << manager::InChargeOf() << endl;
	f << fink::ReportsTo() << endl;
}
void highfink::getall(std::ifstream& f)
{
	abstr_emp::getall(f);
	f >> manager::InChargeOf();
	f.get();
	getline(f, fink::ReportsTo());
}

void highfink::SetAll()
{
	abstr_emp::SetAll();
	cout << "inchargeof ์ž…๋ ฅ : "; cin >> manager::InChargeOf();
	cin.get();
	cout << "reportsto ์ž…๋ ฅ : "; getline(cin, fink::ReportsTo());
}




//pe17-6
#include<iostream>
#include<fstream>
#include<cstdlib>
#include "emp.h"
const int MAX = 10;
using namespace std;

int main()
{
	char ch;
	int classtype;
	int j,i = 0;
	abstr_emp* pc[MAX];
	ifstream fin("test.txt",ios_base::in);
	if (fin.is_open())
	{
		cout << "ํ˜„์žฌ ํŒŒ์ผ์˜ ๋‚ด์šฉ\n";
		while ((fin >> classtype).get(ch))
		{
			switch (classtype)
			{
			case 1:
				pc[i] = new employee;
				break;
			case 2:
				pc[i] = new manager;
				break;
			case 3:
				pc[i] = new fink;
				break;
			case 4:
				pc[i] = new highfink;
				break;
			}
			pc[i++]->getall(fin);
		}
		for (j = 0; j < i; j++)
			pc[j]->ShowAll();
		fin.close();
	}
	else
		cout << "ํŒŒ์ผ์ด ์ฒ˜์Œ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!\n";


	ofstream fout("test.txt", ios_base::out | ios_base::app);
	if (!fout.is_open())
	{
		cerr << "์ถœ๋ ฅ์„ ์œ„ํ•ด " << "test.txt" << " ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n";
		exit(EXIT_FAILURE);
	}

	for (j=i; j < MAX; j++)
	{
		cout << "์ƒ์„ฑํ•  ๊ฐ์ฒดํ˜•์„ ์„ ํƒ\n"
			<< "1. employee\t2. manager\n"
			<< "3. fink\t4. highfink\n";
		(cin >> classtype).get(ch);
		if (classtype > 4)
			break;
		switch (classtype)
		{
		case 1:
			pc[j] = new employee;
			break;
		case 2:
			pc[j] = new manager;
			break;
		case 3:
			pc[j] = new fink;
			break;
		case 4:
			pc[j] = new highfink;
			break;
		}
		fout << classtype<<endl;
		pc[j]->SetAll();
		pc[j]->writeall(fout);
	}
	fout.close();
	cout << "\n\n์ถ”๊ฐ€๋œ ํ›„ ํŒŒ์ผ ๋‚ด์šฉ\n\n";
	for (i = 0; i < j; i++)
		pc[i]->ShowAll();



	return 0;
}

 

 

7๋ฒˆ

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<iterator>
#include<fstream>

void ShowStr(const std::string& str) { std::cout << str << '\n'; }
void GetStrs(std::ifstream& fin, std::vector<std::string> &vistr)
{
	std::size_t len;
	std::string temp;
	while (fin.read((char*)&len, sizeof(std::size_t)) && len > 0)
	{
		char ch;
		temp = "";
		for (int i = 0; i < len; i++)
			if (fin.read(&ch, 1))
				temp += ch;
			else
				break;
		if (fin)
			vistr.push_back(temp);
	}
}
class Store
{
private:
	std::ofstream& os;
public:
	Store(std::ofstream& f)
		:os(f)
	{

	}
	void operator()(const std::string& s)
	{
		std::size_t len = s.size();
		os.write((char*)&len, sizeof(std::size_t));
		os.write(s.data(), len);
	}
};
int main()
{
	using namespace std;
	vector<string> vostr;
	string temp;

	cout << "๋ฌธ์ž์—ด๋“ค์„ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค(๋๋‚ด๋ ค๋ฉด ๋นˆ ์ค„ ์ž…๋ ฅ):\n";
	while (getline(cin, temp) && temp[0] != '\0')
		vostr.push_back(temp);
	cout << "๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.\n";
	for_each(vostr.begin(), vostr.end(), ShowStr);

	ofstream fout("strings.txt", ios_base::out | ios_base::binary);
	for_each(vostr.begin(), vostr.end(), Store(fout));
	fout.close();

	vector<string> vistr;
	ifstream fin("strings.txt", ios_base::in | ios_base::binary);
	if (!fin.is_open())
	{
		cerr << "์ž…๋ ฅ์„ ์œ„ํ•œ ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n";
		exit(EXIT_FAILURE);
	}
	GetStrs(fin, vistr);
	cout << "\nํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ์ฝ์€ ๋ฌธ์ž์—ด๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:\n";
	for_each(vistr.begin(), vistr.end(), ShowStr);

	return 0;
}
728x90
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ (์ƒˆ์ฐฝ์—ด๋ฆผ)
    '๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 18 ์ƒˆ๋กœ์šด C++ ํ‘œ์ค€๊ณผ์˜ ๋งŒ๋‚จ p.1540~ 1๋ฒˆ~4๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 16 string ํด๋ž˜์Šค์™€ ํ‘œ์ค€ ํ…œํ”Œ๋ฆฟ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ p.1342~ 1๋ฒˆ~10๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 15 ํ”„๋ Œ๋“œ,์˜ˆ์™ธ,๊ธฐํƒ€ ์‚ฌํ•ญ p.1206~ 1๋ฒˆ~4๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 14 C++ ์ฝ”๋“œ์˜ ์žฌํ™œ์šฉ p.1103~ 1๋ฒˆ~5๋ฒˆ
    hugDog
    hugDog
    ์•ˆ๋“œ๋กœ์ด๋“œ ๊ณต๋ถ€ ์ค‘์ธ ํ•™์ƒ์ž…๋‹ˆ๋‹ค!

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”