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