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);
	}
}


展开阅读全文

没有更多推荐了,返回首页