C++ Primer 17 프로그래밍 연습
01
// countbefore$.cpp
#include <iostream>
int main()
{
using namespace std;
char ch;
int count = 0;
while (cin.get(ch) && ch != '$')
{
count++;
}
cin.putback(ch);
cout << "count : " << count << endl;
cout << static_cast<char>(cin.get()) << endl;
return 0;
}
02
// inputtoargv.cpp
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[])
{
using namespace std;
if (argc < 2)
{
cout << "파일 이름을 입력해주십시오.\n";
exit(EXIT_FAILURE);
}
string file = argv[1];
ofstream fout(file, ios_base::out);
if (!fout.is_open())
{
cout << "파일 열기 실패\n";
exit(EXIT_FAILURE);
}
char ch;
while (cin.get(ch))
fout << ch;
fout.close();
cout << "----------------\n";
ifstream fin(file, ios_base::in);
while (fin.get(ch))
cout << ch;
fin.close();
return 0;
}
03
// inputtoargv.cpp
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[])
{
using namespace std;
if (argc < 3)
{
cout << "복사할 파일과 복사받을 파일 이름을 모두 입력해주십시오.\n";
exit(EXIT_FAILURE);
}
string file_from = argv[1];
string file_to = argv[2];
ifstream fin_from(file_from, ios_base::out);
ofstream fout_to(file_to, ios_base::out);
if (!fin_from.is_open() || !fout_to.is_open())
{
cout << "파일 열기 실패\n";
exit(EXIT_FAILURE);
}
char ch;
while (fin_from.get(ch))
fout_to << ch;
fin_from.close();
fout_to.close();
ifstream fin_to(file_to, ios_base::in);
while (fin_to.get(ch))
cout << ch;
fin_to.close();
return 0;
}
04
// inputtwo.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void write_input(ifstream& input1, ifstream& input2, ofstream& output);
void write_input_only(ifstream& input, ofstream& output);
int main(int argc, char* argv[])
{
if (argc < 4)
{
cout << "입력 파일 두개, 출력 파일 하나를 입력해주세요\n";
exit(EXIT_FAILURE);
}
string file_input1 = argv[1];
string file_input2 = argv[2];
string file_output = argv[3];
ifstream fin_input1(file_input1, ios_base::in);
ifstream fin_input2(file_input2, ios_base::in);
ofstream fout_output(file_output, ios_base::out);
if (!fin_input1.is_open() || !fin_input2.is_open() || !fout_output.is_open())
{
cout << "파일 열기 실패\n";
exit(EXIT_FAILURE);
}
write_input(fin_input1, fin_input2, fout_output);
char ch;
ifstream fin_output(file_output, ios_base::in);
while (fin_output.get(ch))
cout << ch;
fin_output.close();
return 0;
}
void write_input(ifstream& input1, ifstream& input2, ofstream& output)
{
string str1;
string str2;
getline(input1, str1);
getline(input2, str2);
output << str1 << " " << str2 << endl;
if (str1 == "")
{
input1.close();
write_input_only(input2, output);
}
else if (str2 == "")
{
input2.close();
write_input_only(input1, output);
}
else if (str1 == "" && str2 == "")
{
input1.close();
input2.close();
return ;
}
else
write_input(input1, input2, output);
}
void write_input_only(ifstream& input, ofstream& output)
{
string str;
while (getline(input, str))
output << str;
input.close();
}
05
// Pat&Mat_fromfile.cpp
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
int main()
{
list<string> Pat;
list<string> Mat;
list<string> PatandMat;
ifstream matdata("mat.dat", ios_base::in);
ifstream patdata("pat.dat", ios_base::in);
cout << "Mat의 친구들을 입력하세요 (끝내려면 quit) : \n";
string temp;
while (matdata >> temp && temp != "quit")
Mat.push_back(temp);
Mat.sort();
cout << "Mat의 친구들 목록입니다.\n";
for (list<string>::iterator i = Mat.begin(); i != Mat.end(); ++i)
cout << (*i) << endl;
cout << "Pat의 친구들을 입력하세요 (끝내려면 quit) : \n";
while (patdata >> temp && temp != "quit")
Pat.push_back(temp);
Pat.sort();
cout << "Pat의 친구들 목록입니다.\n";
for (list<string>::iterator i = Pat.begin(); i != Pat.end(); ++i)
cout << (*i) << endl;
PatandMat = Mat;
PatandMat.merge(Pat);
PatandMat.unique();
ofstream patnmat("matnpat.dat", ios_base::out);
for (list<string>::iterator i = PatandMat.begin(); i != PatandMat.end(); ++i)
{
patnmat << (*i) << endl;
}
patnmat.close();
cout << "Pat&Mat의 친구들 목록입니다.\n";
ifstream matnpat("matnpat.dat", ios_base::in);
char ch;
while (matnpat.get(ch))
cout << ch;
return 0;
}
06
// emp_file.h
#ifndef EMP_FILE_H_
#define EMP_FILE_H_
#include <iostream>
#include <fstream>
#include <string>
class abstr_emp
{
private:
std::string fname;
std::string lname;
std::string job;
public:
enum classkind { Employee, Manager, Fink, Highfink };
abstr_emp();
abstr_emp(const std::string & fn, const std::string & ln, const std::string & j);
friend std::ostream & operator<<(std::ostream & os, const abstr_emp & e);
virtual ~abstr_emp() = 0;
virtual void ShowAll() const;
virtual void SetAll();
virtual void writeall(std::ofstream & fout);
virtual void getall(std::ifstream & fin);
};
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 SetAll();
virtual void writeall(std::ofstream & fout);
virtual void getall(std::ifstream & fin);
};
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 SetAll();
virtual void writeall(std::ofstream & fout);
virtual void getall(std::ifstream & fin);
};
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 SetAll();
virtual void writeall(std::ofstream & fout);
virtual void getall(std::ifstream & fin);
};
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 SetAll();
virtual void writeall(std::ofstream & fout);
virtual void getall(std::ifstream & fin);
};
#endif
// emp_file.cpp
#include "emp_file.h"
using std::cout;
using std::cin;
using std::endl;
abstr_emp::abstr_emp() : fname(""), lname(""), job("")
{
}
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 << " " << lname << endl;
cout << "직업 : " << job << endl;
}
void abstr_emp::SetAll()
{
cout << "성을 입력하십시오 : ";
cin >> fname;
cout << "이름을 입력하십시오 : ";
cin >> lname;
cout << "직업을 입력하십시오 : ";
cin >> job;
}
abstr_emp::~abstr_emp()
{
}
std::ostream & operator<<(std::ostream & os, const abstr_emp & e)
{
return os << e.fname << " " << e.lname << endl;
}
void abstr_emp::writeall(std::ofstream & fout)
{
fout << fname << endl;
fout << lname << endl;
fout << job << endl;
}
void abstr_emp::getall(std::ifstream & fin)
{
fin >> fname >> lname >> job;
}
// -------------------------------------------------------
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::SetAll()
{
abstr_emp::SetAll();
}
void employee::writeall(std::ofstream & fout)
{
fout << abstr_emp::Employee << std::endl;
abstr_emp::writeall(fout);
}
void employee::getall(std::ifstream & fin)
{
abstr_emp::getall(fin);
}
// --------------------------------------------------
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 << endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
cout << "관리 수를 입력하십시오 : ";
cin >> inchargeof;
}
void manager::writeall(std::ofstream & fout)
{
fout << abstr_emp::Manager << std::endl;
abstr_emp::writeall(fout);
fout << inchargeof << endl;
}
void manager::getall(std::ifstream & fin)
{
abstr_emp::getall(fin);
fin >> inchargeof;
}
// -------------------------------------------------
fink::fink() : abstr_emp(), reportsto("")
{
}
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), reportsto(e.reportsto)
{
}
void fink::ShowAll() const
{
abstr_emp::ShowAll();
cout << "보고 대상 : " << reportsto << endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
cout << "보고 대상을 입력하십시오 : ";
cin >> reportsto;
}
void fink::writeall(std::ofstream & fout)
{
fout << abstr_emp::Fink << std::endl;
abstr_emp::writeall(fout);
fout << reportsto << endl;
}
void fink::getall(std::ifstream & fin)
{
abstr_emp::getall(fin);
fin >> reportsto;
}
// ---------------------------------------------------
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()
{
}
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() << endl;
cout << "보고 대상 : " << ReportsTo() << endl;
}
void highfink::SetAll()
{
abstr_emp::SetAll();
cout << "관리 수를 입력하십시오 : ";
cin >> InChargeOf();
cout << "보고 대상을 입력하십시오 : ";
cin >> ReportsTo();
}
void highfink::writeall(std::ofstream & fout)
{
fout << abstr_emp::Highfink << std::endl;
abstr_emp::writeall(fout);
fout << InChargeOf() << endl;
fout << ReportsTo() << endl;
}
void highfink::getall(std::ifstream & fin)
{
abstr_emp::getall(fin);
fin >> InChargeOf();
fin >> ReportsTo();
}
// useemp_file.cpp
#include "emp_file.h"
#include <iostream>
#include <fstream>
using namespace std;
const int MAX = 3;
int main()
{
abstr_emp * pc[MAX];
int classtype;
int count = 0;
char ch;
ifstream fin("emp.dat", ios_base::in);
if (fin.is_open())
{
while ((fin >> classtype).get(ch))
{
switch (classtype)
{
case abstr_emp::Employee:
pc[count] = new employee;
break;
case abstr_emp::Manager:
pc[count] = new manager;
break;
case abstr_emp::Fink:
pc[count] = new fink;
break;
case abstr_emp::Highfink:
pc[count] = new highfink;
break;
}
pc[count++]->getall(fin);
}
for (int i = 0; i < count; i++)
{
pc[i]->ShowAll();
cout << endl;
}
fin.close();
}
int n;
while (count < MAX)
{
cout << "어느 데이터를 추가할 것인지 선택해주세요 : \n"
<< "1. employee\t2. manager\n3. fink\t4. highfink\n"
<< "이외의 입력을 받을시 데이터 추가 작업이 종료됩니다.\n";
cin >> n;
switch (n)
{
case 1:
pc[count] = new employee;
break;
case 2:
pc[count] = new manager;
break;
case 3:
pc[count] = new fink;
break;
case 4:
pc[count] = new highfink;
break;
default:
break;
}
if (n < 1 || n > 4)
break;
pc[count++]->SetAll();
}
ofstream fout("emp.dat", ios_base::out);
for (int i = 0; i < count; i++)
{
pc[i]->ShowAll();
cout << endl;
pc[i]->writeall(fout);
}
fout.close();
return 0;
}
07
// useemp.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
class Store
{
private:
std::ofstream& os;
public:
Store(std::ofstream & fout) : os(fout) {}
~Store() {}
Store & operator()(const std::string & str)
{
size_t len = str.length();
os.write((char*)&len, sizeof(size_t));
os.write(str.data(), len);
return *this;
}
};
void ShowStr(const std::string &);
void GetStrs(std::ifstream& fin, std::vector<std::string>& v);
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.dat", ios_base::out | ios_base::binary);
for_each(vostr.begin(), vostr.end(), Store(fout));
fout.close();
vector<string> vistr;
ifstream fin("strings.dat", 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;
}
void ShowStr(const std::string & str)
{
std::cout << str << std::endl;
}
void GetStrs(std::ifstream& fin, std::vector<std::string>& v)
{
size_t len = 0;
std::string str;
char ch;
while (fin.peek() && !fin.eof())
{
fin.read((char*)&len, sizeof(size_t));
for (size_t i = 0; i < len; i++)
{
fin.read(&ch, sizeof(char));
str.push_back(ch);
}
v.push_back(str);
str.clear();
}
fin.close();
}