1. 程式人生 > >C++ Primer Plus第六版 第十七章 程式設計練習答案(完結篇...木有18章)

C++ Primer Plus第六版 第十七章 程式設計練習答案(完結篇...木有18章)

//第一題
//main.cpp
#include <iostream>
#include <cctype>

int main()
{
	char c;
	int n = 0;
	while (std::cin.get(c) && c != '$')
	{
		if (isalnum(c) || isalpha(c))
			++n;
	}
	std::cin.putback(c);
	std::cout << n << std::endl;

	return 0;
}

//= =我在開頭插入這段文字會被當做程式碼 好麻煩  就放在第一段和第二段之間吧

前前後後這本書也看了有很長時間了,當初剛拿到這本書的時候:臥槽 800多頁  這麼厚怎麼可能看完。 然後一點點讀一點點啃,也總算把這本書啃完了。當然 這本書讀完,C++也未必能算入門,還需要後續不斷的學習...18章不準備讀了,寫的不好。     總結下本書吧,這本書只能說差強人意,書上的例子非常詳細,基本上每個例子都是貼出完整的可以執行的程式碼,這點很方便初學者,但是帶來的弊端就是佔篇幅太長,仁者見仁吧,我不太喜歡這種貼完整的長程式碼的。當時在豆瓣看到一條評論,結合我讀完這本書的感受,感同身受:這本書的C++11部分寫的真的很爛,感覺就是每章後面強行插進去一些C++11的知識,然後最後一章來個大彙總。這樣效果感覺很不好,所以我最後一章都沒有讀。關於C++11的部分看網上推薦說第5版的C++ PRIMER寫的不錯。但是我有一本以前買的第四版的了,這書也不便宜,我暫時也不是很需要用到C++11的部分,所以先讀第4版的吧。那條評論還指出這本書的結構混亂,這我倒是沒怎麼感覺。  不過這本書的小錯誤挺多的。總之,這本書並不是什麼非讀不可的神書,如果有更好的入門教材,完全可以不用這本...


//第二題
//main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		std::cout << "引數有誤" << std::endl;
		exit(EXIT_FAILURE);
	}

	std::ofstream fout(argv[1]);
	std::string str;
	while (std::getline(std::cin, str))
		fout << str << std::endl;

	fout.close();

	return 0;
}

//第三題
//main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		std::cout << "引數有誤!" << std::endl;
		exit(EXIT_FAILURE);
	}

	std::ifstream fin(argv[1]);
	std::ofstream fout(argv[2]);
	std::string str;
	while (std::getline(fin, str))
		fout << str << std::endl;
	fin.close();
	fout.close();

	return 0;
}
//第四題
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		std::cout << "引數有誤" << std::endl;
		exit(EXIT_FAILURE);
	}

	std::ifstream FirstIn(argv[1]);
	std::ifstream SecondIn(argv[2]);
	std::ofstream Fout(argv[3]);
	std::string str1, str2;
	while (!FirstIn.eof() && !SecondIn.eof() && std::getline(FirstIn, str1) && std::getline(SecondIn, str2))
		Fout << str1 + " " + str2 << std::endl;
	while (std::getline(FirstIn, str1))
		Fout << str1 << std::endl;
	while (std::getline(SecondIn, str2))
		Fout << str2 << std::endl;

	FirstIn.close();
	SecondIn.close();
	Fout.close();

	return 0;
}


//第五題
//main.cpp
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <cstdlib>

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		std::cout << "引數有誤" << std::endl;
		exit(EXIT_FAILURE);
	}

	std::ifstream FileIn1(argv[1]);
	std::ifstream FileIn2(argv[2]);
	std::string str;
	std::list<std::string> ListMat;
	std::list<std::string> ListPat;
	std::list<std::string> ListMerge;
	while (std::getline(FileIn1, str))
		ListMat.push_back(str);
	while (std::getline(FileIn2, str))
		ListPat.push_back(str);

	ListMat.sort();
	std::cout << "Mat:\n";
	for each (std::string var in ListMat)
		std::cout << "\t" << var << std::endl;
	std::cout << std::endl;

	ListPat.sort();
	std::cout << "Pat:\n";
	for each (std::string var in ListPat)
		std::cout << "\t" << var << std::endl;
	std::cout << std::endl;
	
	ListMerge = ListMat;
	ListMerge.merge(ListPat);
	std::cout << "Merge:\n";
	for each (std::string var in ListMerge)
		std::cout << "\t" << var << std::endl;
	std::cout << std::endl;

	FileIn1.close();
	FileIn2.close();
	return 0;
}
//第六題
//main.cpp
#include "emp.h"

const char *FileName = "1.txt";

int main()
{
	std::ifstream fin;

	do
	{
		fin.clear();
		fin.open(FileName);
		if (fin.is_open())
		{
			std::cout << "當前檔案" << FileName << "內容: \n";
			int count = 0;
			std::string str;
			while (std::getline(fin, str))
			{
				abstr_emp *ptr = NULL;
				if (str == "1")
				{
					std::string fname, lname, job;
					std::getline(fin, fname);
					std::getline(fin, lname);
					std::getline(fin, job);

					ptr = new employee(fname, lname, job);
					ptr->ShowAll();
				}
				else if (str == "2")
				{
					std::string fname, lname, job, inchargeof;
					std::getline(fin, fname);
					std::getline(fin, lname);
					std::getline(fin, job);
					std::getline(fin, inchargeof);
					
					ptr = new manager(fname, lname, job, atoi(inchargeof.c_str()));
					ptr->ShowAll();
				}
				else if (str == "3")
				{
					std::string fname, lname, job, reportsto;
					std::getline(fin, fname);
					std::getline(fin, lname);
					std::getline(fin, job);
					std::getline(fin, reportsto);

					ptr = new fink(fname, lname, job, reportsto);
					ptr->ShowAll();
				}
				else
				{
					std::string fname, lname, job, inchargeof, reportsto;
					std::getline(fin, fname);
					std::getline(fin, lname);
					std::getline(fin, job);
					std::getline(fin, inchargeof);
					std::getline(fin, reportsto);

					ptr = new highfink(fname, lname, job, reportsto, atoi(inchargeof.c_str()));
					ptr->ShowAll();
				}
				std::getline(fin, str);
			}
			fin.close();
		}

		std::ofstream fout(FileName, std::ios::out | std::ios::app);
		if (!fout.is_open())
		{
			std::cerr << "Can't open " << FileName << " file for output.\n";
			exit(EXIT_FAILURE);
		}

		abstr_emp *ptr = NULL;
		std::cout << "選擇要生成的型別:\n";
		std::cout << "1、employee\t2、manager\n3、fink\t4、highfink\n";
		int ans;
		std::cin >> ans;
		std::cin.get();
		switch (ans)
		{
		case 1:
			ptr = new employee;
			break;
		case 2:
			ptr = new manager;
			break;
		case 3:
			ptr = new fink;
			break;
		case 4:
			ptr = new highfink;
			break;
		default:
			break;
		}
		ptr->SetAll();
		ptr->WriteAll(fout);

		char ch;
		std::cout << "繼續?<y/n>: ";
		std::cin >> ch;
		std::cin.get();
		if (ch == 'n')
			break;
	} while (true);

	fin.close();
	return 0;
}

//emp.h
#ifndef EMP_H_
#define EMP_H_

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

class abstr_emp
{
private:
	std::string fname;
	std::string lname;
	std::string job;
protected:
	const std::string &GetFname() const { return fname; }
	const std::string &GetLname() const { return lname; }
	const std::string &GetJob() const { return job; }
	void SetFname() { std::cout << "Input Fname: "; std::getline(std::cin, fname); }
	void SetLname() { std::cout << "Input Lname: "; std::getline(std::cin, lname); }
	void SetJob() { std::cout << "Input Job: "; std::getline(std::cin, job); }
public:
	abstr_emp();
	abstr_emp(const std::string &fn, const std::string &ln, const std::string &j);
	abstr_emp(const abstr_emp &e);
	virtual void ShowAll() const;
	virtual void SetAll();
	virtual void WriteAll(std::ofstream &of);
	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);
	~employee() {}
	virtual void ShowAll() const;
	virtual void SetAll();
	virtual void WriteAll(std::ofstream &of);
};

class manager : virtual public abstr_emp
{
private:
	int inchargeof;
protected:
	int InChargeOf() const { return inchargeof; }
	int &InChargOf() { return inchargeof; }
	void SetInChargeOf() { std::cout << "Input InChargeOf: "; std::cin >> inchargeof; std::cin.get(); }
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);
	~manager() {}
	virtual void ShowAll() const;
	virtual void SetAll();
	virtual void WriteAll(std::ofstream &of);
};

class fink : virtual public abstr_emp
{
private:
	std::string reportsto;
protected:
	const std::string ReportsTo() const { return reportsto; }
	std::string &ReportsTo() { return reportsto; }
	void SetReportsto() { std::cout << "Input reportsto: "; std::getline(std::cin, 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);
	~fink() {}
	virtual void ShowAll() const;
	virtual void SetAll();
	virtual void WriteAll(std::ofstream &of);
};

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);
	~highfink() {}
	virtual void ShowAll() const;
	virtual void SetAll();
	virtual void WriteAll(std::ofstream &of);
};

#endif


//emp.cpp
#include "emp.h"

abstr_emp::abstr_emp() : fname("NULL"), lname("NULL"), job("NULL")
{
}

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

abstr_emp::abstr_emp(const abstr_emp & e) : fname(e.fname), lname(e.lname), job(e.job)
{
}

void abstr_emp::ShowAll() const
{
	std::cout << "fname: " << GetFname() << std::endl;
	std::cout << "lname: " << GetLname() << std::endl;
	std::cout << "job: " << GetJob() << std::endl;
}

void abstr_emp::SetAll()
{
	SetFname();
	SetLname();
	SetJob();
}

void abstr_emp::WriteAll(std::ofstream & of)
{
	of << GetFname() << std::endl << GetLname() << std::endl << GetJob() << std::endl << std::endl;
}

std::ostream &operator<<(std::ostream &os, const abstr_emp &e)
{
	os << e.fname << ", " << e.lname << ", " << e.job;
	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::SetAll()
{
	abstr_emp::SetAll();
}

void employee::WriteAll(std::ofstream & of)
{
	of << "1" << std::endl;
	of << GetFname() << std::endl << GetLname() << std::endl << GetJob() << std::endl << std::endl;
}

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((const abstr_emp &)m)
{
	inchargeof = m.inchargeof;
}

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

void manager::SetAll()
{
	abstr_emp::SetAll();
	SetInChargeOf();
}

void manager::WriteAll(std::ofstream & of)
{
	of << "2" << std::endl;
	of << GetFname() << std::endl;
	of << GetLname() << std::endl;
	of << GetJob() << std::endl;
	of << inchargeof << std::endl << std::endl;
}

fink::fink() : abstr_emp()
{
	reportsto = "NULL";
}

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((const abstr_emp &)e)
{
	reportsto = e.reportsto;
}

void fink::ShowAll() const
{
	abstr_emp::ShowAll();
	std::cout << "reportsto" << ReportsTo() << std::endl;
}

void fink::SetAll()
{
	abstr_emp::SetAll();
	SetReportsto();
}

void fink::WriteAll(std::ofstream & of)
{
	of << "3" << std::endl;
	of << GetFname() << std::endl;
	of << GetLname() << std::endl;
	of << GetJob() << std::endl;
	of << reportsto << std::endl;
}

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((const abstr_emp &)f), manager((const abstr_emp &)f, ico), fink(f)
{
}

highfink::highfink(const manager & m, const std::string & rpo) : abstr_emp((const abstr_emp &)m), manager(m), fink((const abstr_emp &)m, rpo)
{
}

highfink::highfink(const highfink & h) : abstr_emp((const abstr_emp &)h), manager((const manager &)h), fink((const fink &)h)
{
}

void highfink::ShowAll() const
{
	abstr_emp::ShowAll();
	std::cout << "inchargeof: " << InChargeOf() << std::endl;
	std::cout << "reportsto: " << ReportsTo() << std::endl;
}

void highfink::SetAll()
{
	abstr_emp::SetAll();
	manager::SetInChargeOf();
	fink::SetReportsto();
}

void highfink::WriteAll(std::ofstream & of)
{
	of << "4" << std::endl;
	of << GetFname() << std::endl;
	of << GetLname() << std::endl;
	of << GetJob() << std::endl;
	of << InChargeOf() << std::endl;
	of << ReportsTo() << std::endl << std::endl;
}
//第七題
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>

void ShowStr(const std::string &str);
void GetStrs(std::ifstream &is, std::vector<std::string> &vec);

class Store
{
	std::ofstream &of;
public:
	Store(std::ofstream &out) : of(out) {}
	void operator()(const std::string &str)
	{
		size_t len = str.size();
		of.write((char *)&len, sizeof(size_t));
		of.write(str.data(), len);
	}
};

int main()
{
	using namespace std;
	vector<string> vostr;
	string temp;

	cout << "Enter strings (empty line to quit):\n";
	while (getline(cin, temp) && temp[0] != '\0')
		vostr.push_back(temp);
	cout << "Here is your input.\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 << "Could not open file for input.\n";
		exit(EXIT_FAILURE);
	}
	GetStrs(fin, vistr);
	cout << "\nHere are the strings read form the file:\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 &is, std::vector<std::string> &vec)
{
	size_t len;
	while (is.read((char *)&len, sizeof(size_t)))
	{
		char *str = new char[len];
		is.read(str, len);
		str[len + 1] = 0;
		vec.push_back(str);
	}
}