国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

c++ Primer Plus(第六版)第十七章習(xí)題,寫(xiě)代碼之路

2019-11-10 19:45:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

c++ PRimer Plus(習(xí)題17.1)

//編寫(xiě)一個(gè)程序計(jì)算$之前的字符數(shù),并且把它留在輸入流中#include<iostream>int main(){	using namespace std;	cout << "Enter a text,report the number(end with $):/n";	int count=0;	char r;	while (cin.peek()!='$')			//peek方法查看下一個(gè)字符	{			cin.get(r);					//用<<讀取會(huì)跳過(guò)空格		if (r != '$')		{			cout << r;			count++;		}	}	char next;	cin >> next;	cout << "/nThe number of character is: " << count << endl;	cout << "And the next character is: " << next << endl;	return 0;}c++ Primer Plus(習(xí)題17.2)

//將鍵盤(pán)輸入弄到文件里面,直到模擬的文件結(jié)尾#include<iostream>#include<fstream>#include<string>int main(int argc,char *argv[]){	using namespace std;	if (argc != 2)	{		cerr << "Usage: " << argv[0] << " filename[s]/n";		exit(EXIT_FAILURE);	}	ofstream fin;	fin.open(argv[1]);			//這是命令行的第二個(gè)參數(shù)	if (!fin.is_open())				{		cerr << "Could not open " << argv[0] << "/n";		exit(EXIT_FAILURE);	}	cout << "Give me something you want add to the file(<CTRL>+<Z>+<ENTER> to end): ";	string  ch;	while (cin.fail()==false)	//這里是用輸入流的方法來(lái)檢查文件結(jié)尾	{		getline(cin, ch);		cout << ch;		fin << ch;	}	cout << "Input complete!/n";	fin.close();	return 0;}c++ Primer Plus(習(xí)題17.3)

//和上一題差不多,不過(guò)是將一個(gè)文件的內(nèi)容復(fù)制到另一個(gè)文件//也是用命令行來(lái)指定文件的參數(shù)#include<iostream>#include<fstream>int main(int argc, char *argv[]){	using namespace std;	if (argc != 3)	{		cerr << "Usage: " << argv[0] << " resource filename[s] target[s]/n";		exit(EXIT_FAILURE);	}	ofstream fin;	fin.open(argv[2]);			//這是命令行的第二個(gè)參數(shù)	if (!fin.is_open())	{		cerr << "Could not open " << argv[0] << "/n";		exit(EXIT_FAILURE);	}	ifstream fou;	fou.open(argv[1]);	if (!fou.is_open())	{		cerr << "Could not open " << argv[0] << "/n";		exit(EXIT_FAILURE);	}	char c;	while (fou.get(c))	{		cout << c;		fin.put(c);		//逐字符讀取和寫(xiě)入	}	cout << "/nCopy complete!/n";		//程序出現(xiàn)一個(gè)問(wèn)題,就是會(huì)把源文件最后的字符寫(xiě)入兩下到目標(biāo)文件中	fou.close();	fin.close();	return 0;}c++ Primer Plus(習(xí)題17.4)

//三個(gè)文件的輸入輸出,對(duì)文件進(jìn)行拼接,并寫(xiě)入到第三個(gè)文件中//這個(gè)程序是讓用戶給出兩個(gè)文件的內(nèi)容,然后程序顯示拼接后的結(jié)果//輸出文件有個(gè)小小的空格,這個(gè)問(wèn)題很小,可是我不知道#include<iostream>#include<fstream>#include<cstdlib>#include<string>int main(){	using namespace std;	ofstream fin;	cout << "This program is connect two file content to another file./n";	cout << "Enter the frist filename: ";	string filename;	getline(cin, filename);	fin.open(filename.c_str());	cout << "Now,give me something to the frist resource file:/n";	string file;	while (cin.fail() == false)		//檢查文件尾	{		getline(cin, file);		fin<<file << endl;	}	fin.close();	cin.clear();	cout << "Enter the second filename: ";	string filename2;	getline(cin, filename2);	fin.open(filename2.c_str());	cout << "Now,give me someing to the second resource file:/n";	while (cin.fail() == false)	{		getline(cin, file);		fin<<file << endl;	}	fin.close();	cin.clear();	cout << "Enter the target filename: ";	string target;	getline(cin, target);	fin.open(target.c_str());	ifstream fou,fou2;	fou.open(filename);	fou2.open(filename2);	if (!fou.is_open()||!fou2.is_open())		{			cerr << "Could not open the source!"<<filename<<endl<<filename2<< "/n";			exit(EXIT_FAILURE);		}	string file2;	while (!fou.eof()&&!fou2.eof())	{		getline(fou, file);		getline(fou2, file2);		fin << file + ' ' + file2 << endl;		cout<< file + ' ' + file2 << endl;	}	while (!fou.eof())	{		getline(fou, file);		fin << file << endl;		cout << file << endl;	}	while (!fou2.eof())	{		getline(fou2, file);		fin << file << endl;		cout << file << endl;	}	fou.close();	fou2.close();	fin.close();	return 0;}c++ Primer Plus(習(xí)題17.5)

//這題和16章的差不多,不過(guò)是在文件里面讀取名字到容器里面//所以自己準(zhǔn)備好名字,容器這里有#include<iostream>#include<fstream>#include<string>#include<set>#include<algorithm>#include<iterator>				//迭代器的頭文件int main(){	using namespace std;	ostream_iterator<string, char>out(cout, "/n");			//輸出迭代器	ifstream fin;	char filename[10];	set<string>fresource;	char temp[10];	string file;	cout << "Enter the Mat friends filename: ";	cin.getline(filename, 10);	fin.open(filename);	if (!fin.is_open())	{		cerr << "Cant open the file " << filename << endl;		exit(EXIT_FAILURE);	}	while (!fin.eof())	{		fin.getline(temp,10);		fresource.insert(temp);			//用插入元素的方法		cout << temp<<endl;	}	fin.close();	cout << "Enter the Pat friends filename: ";	cin.getline(filename, 10);	fin.open(filename);	if (!fin.is_open())	{		cerr << "Cant open the file " << filename << endl;		exit(EXIT_FAILURE);	}	set<string>fresource2;			//建立第二個(gè)容器	while (!fin.eof())	{		fin.getline(temp, 10);		fresource2.insert(temp);		cout << temp << endl;	}	fin.close();	cout << "Enter the Target filename: ";	cin.getline(filename, 10);	ofstream fou;	fou.open(filename);	set<string>target;						//還是書(shū)上的老方法,用一個(gè)匿名的插入迭代器	set_union(fresource.begin(), fresource.end(), fresource2.begin(), fresource2.end(),		insert_iterator<set<string>>(target, target.begin()));	cout << "Friends they have:/n";	copy(target.begin(), target.end(), out);	ostream_iterator<string, char>fileout(fou, "/n");			//輸出到文件迭代器,自己起名的	copy(target.begin(), target.end(), fileout);	fou.close();	return 0;}c++ Primer Plus(習(xí)題17.6)

//第十七章的17.6題,跟前面14.5的相似,不過(guò)是添加了一點(diǎn)東西//書(shū)上提供了解決方案,添加數(shù)據(jù)到文件中,保存讀寫(xiě)數(shù)據(jù)的方法//對(duì)了,書(shū)上的getall()方法個(gè)人覺(jué)得是要帶參數(shù)的//這題打出來(lái)之后出現(xiàn)已有主體C2084錯(cuò)誤,原因是該函數(shù)重定義了//去掉之后出現(xiàn)錯(cuò)誤LNK2001,又是這個(gè)類設(shè)計(jì)很常見(jiàn),起碼是我這里很常見(jiàn)的//主要是無(wú)法識(shí)別的外部命令,//錯(cuò)誤原因:empoyee的虛函數(shù)沒(méi)有定義//我的文件讀取方式是/*0TripHarrisThumper*/#include<iostream>using namespace std;#include"fileemp.h"const int MAX = 10;void showClassType();int main(void){	abstr_emp *pc[MAX];					//不可以創(chuàng)建抽象類型的數(shù)組,但是可以創(chuàng)建指針	cout << "This program show the file of class Empoyee,input the data name to continue./n";	cout << "Enter the target file:";	char filename[MAX];	cin.getline(filename, MAX);	ifstream fin;	fin.open(filename);	if (!fin.is_open())	{		cerr << "Sorry ,can't open " << filename << endl;		exit(EXIT_FAILURE);	}	char ch;	int i = 0;	int classtype;	while (fin >> classtype)	{		switch (classtype)		{		case Employee:pc[i] = new employee; break;		case Manager:pc[i] = new manager; break;		case Fink:pc[i] = new fink; break;		case Highfink:pc[i] = new highfink; break;		}		pc[i]->getall(fin);		pc[i]->ShowAll();		i++;	}	fin.close();	ofstream fou;	fou.open(filename, ios_base::out | ios_base::app);	//追加模式打開(kāi)文件	int idex=0;	showClassType();	while (cin >> ch&&ch != 'q'&&idex < MAX)	{		cin.get();		switch (ch)		{		case 'a':pc[idex]= new employee;			pc[idex]->SetAll();			break;		case 'b':pc[idex] = new manager;			pc[idex]->SetAll(); break;		case 'c':pc[idex] = new fink;			pc[idex]->SetAll(); break;		case 'd':pc[idex] = new highfink;			pc[idex]->SetAll(); break;		default:cout << "Error type!/n"; break;		}		idex++;		showClassType();	}	for (i = 0; i < idex; i++)		pc[i]->writeall(fou);	fou.close();	fin.clear();	fin.open(filename);	cout << "Here is the redice file:/n";	while ((fin >> classtype).get(ch))	{		switch (classtype)		{		case Employee:pc[i] = new employee; break;		case Manager:pc[i] = new manager; break;		case Fink:pc[i] = new fink; break;		case Highfink:pc[i] = new highfink; break;		}		pc[i]->getall(fin);		pc[i]->ShowAll();		i++;	}	fin.close();	cout << "Bye!/n";	return 0;}void showClassType(){	cout << "What class type do you want to creat:/n "		"a)Employee  b)Manage  c)Fink  d)Highfink  q)quit/n";}
#pragma once#pragma execution_character_set("utf-8")//本文件為utf-8編碼格式#pragma once#pragma execution_character_set("utf-8")//本文件為utf-8編碼格式//這個(gè)頭文件屬于14.5題書(shū)上提供的多態(tài)繼承頭文件的修改版,輸出數(shù)據(jù)到文件,//可以按照書(shū)上的方法進(jìn)行修改#ifndef FILEEMP_H#define FILEEMP_H#include<iostream>#include<string>#include<fstream>using std::string;using namespace std;enum classkin { Employee, Manager, Fink, Highfink };class abstr_emp{private:	string fname;	string lname;	string job;public:	abstr_emp() :fname("No name"), lname("no name "), job("no job") {};	abstr_emp(const string&fn, const string&ln, const string &j) :fname(fn)		, lname(ln), job(j) {};	virtual void ShowAll()const;	virtual void SetAll();	virtual void writeall(ofstream &fo);		//寫(xiě)入到文件的虛函數(shù),又是這里出現(xiàn)無(wú)法識(shí)別的外部命令	virtual void getall(ifstream &fi);			//從文件中讀取的虛函數(shù)	friend std::ostream&Operator<<(std::ostream &os, const abstr_emp&e);	virtual ~abstr_emp() = 0 {};//這里少了{(lán)}會(huì)報(bào)錯(cuò),鏈接錯(cuò)誤一大堆};class employee :public abstr_emp{public:	employee(const string &fn, const string&ln, const string&j) :abstr_emp(fn, ln, j) {};	employee() :abstr_emp() {};	virtual void ShowAll()const { abstr_emp::ShowAll(); };	virtual void SetAll() { abstr_emp::SetAll(); };	void writeall(std::ofstream &fo);		//寫(xiě)入到文件的虛函數(shù)	 void getall(std::ifstream &fi);			//從文件中讀取的虛函數(shù)};class manager :virtual public abstr_emp{public:	manager() :abstr_emp(), inchargeof(0) {};	manager(const string &fn, const string &ln,		const string &j, int ico = 0) :abstr_emp(fn, ln, j), inchargeof(ico) {};	manager(const abstr_emp&e, int ico) :abstr_emp(e), inchargeof(ico) {};	manager(const manager&m);	virtual void ShowAll()const;	virtual void SetAll();	 void writeall(std::ofstream &fo);		//寫(xiě)入到文件的虛函數(shù)	 void getall(std::ifstream &fi);			//從文件中讀取的虛函數(shù)protected:	int InChargeOf()const { return inchargeof; }	//outut method	int &InChargeOf() { return inchargeof; }		//input method	void FileChangeOf(std::ofstream &fou)const { fou << inchargeof << "/t"; }	void FileChangeOf(std::ifstream&fin) { fin >> inchargeof; }private:	int inchargeof;};class fink :virtual public abstr_emp{private:	string reportsto;protected:	const string ReportsTo()const { return reportsto; }	string &ReportsTo() { return reportsto; }	void FileReportsTo(std::ofstream &fou) {fou << reportsto;}	void FileReportsTo(std::ifstream&fin) { fin >> reportsto; }public:	fink() :abstr_emp(), reportsto("null") {};	fink(const string &fn, const string &ln,		const string &j, const string &rpo) :abstr_emp(fn, ln, j), reportsto(rpo) {};	fink(const abstr_emp&e, const string &rpo) :abstr_emp(e), reportsto(rpo) {};	fink(const fink&e);	virtual void ShowAll()const;	virtual void SetAll();	 void writeall(std::ofstream &fo);		//寫(xiě)入到文件的虛函數(shù)	 void getall(std::ifstream &fi);			//從文件中讀取的虛函數(shù)};class highfink :public manager, public fink{public:	highfink() {};				//顯式調(diào)用基類構(gòu)造函數(shù)	highfink(const string &fn, const string &ln,		const string &j, const string &rpo, int ico)		:abstr_emp(fn, ln, j), fink(fn, ln, j, rpo), manager(fn, ln, j, ico) {};	highfink(const abstr_emp&e, const string &rpo, int ico)		:abstr_emp(e), fink(e, rpo), manager(e, ico) {};	highfink(const fink &f, int ico)		:abstr_emp(f), fink(f), manager(f, ico) {};	highfink(const manager &m, const string &rpo)		:abstr_emp(m), manager(m), fink(m, rpo) {};	highfink(const highfink&h)		:abstr_emp(h), manager(h), fink(h) {};	virtual void ShowAll()const;	virtual void SetAll();	 void writeall(std::ofstream &fo);		//寫(xiě)入到文件的虛函數(shù)	void getall(std::ifstream &fi);			//從文件中讀取的虛函數(shù)};#endif // !EMP_H
#include"fileemp.h"using std::cout;using std::endl;using std::cin;//abstr_emp methodsvoid abstr_emp::ShowAll()const{	cout <<"/t/t"<< lname << "," << fname << "/t"		<< job;}//設(shè)置各成員的值void abstr_emp::SetAll(){	cout << "Enter frist name: ";	std::getline(cin, fname);	cout << "Enter last name: ";	std::getline(cin, lname);	cout << "Job: ";	std::getline(cin, job);}//寫(xiě)入數(shù)據(jù)到文件的方法void abstr_emp::writeall(ofstream &fo){	fo<<lname <<endl<< fname <<endl<<job<<endl;}void abstr_emp::getall(ifstream &fi)		//書(shū)上沒(méi)有用參數(shù),想想不太可能{	std::getline(fi, lname);	std::getline(fi, fname);	std::getline(fi, job);}//only display namestd::ostream&operator<<(std::ostream &os, const abstr_emp&e){	os << "Name: " << e.lname << " , " << e.fname;	return os;}//employee的類方法void employee::writeall(std::ofstream &fo){	fo << Employee;	abstr_emp::writeall(fo);}void employee::getall(std::ifstream &fi){	abstr_emp::getall(fi);}//manage methodsmanager::manager(const manager&m) :abstr_emp(m)	//copy construction function{	inchargeof = m.inchargeof;}void manager::ShowAll()const{	cout << endl;	abstr_emp::ShowAll();	cout <<"/t"<< inchargeof;}void manager::SetAll(){	abstr_emp::SetAll();	cout << "Enter the inchangeof count: ";	cin >> inchargeof;	while (cin.get() != '/n')		continue;}void manager::writeall(std::ofstream &fou){	fou << Manager;	abstr_emp::writeall(fou);	fou << inchargeof<<endl;}void manager::getall(std::ifstream &fin){	abstr_emp::getall(fin);	fin >> inchargeof;}//fink methodsfink::fink(const fink&e) :abstr_emp(e){	reportsto = e.reportsto;}void fink::ShowAll()const{	cout << endl;	abstr_emp::ShowAll();	cout << "/t" << reportsto;}void fink::SetAll(){	cout << "Enter the reportsto: ";	std::getline(cin, reportsto);	while (cin.get() != '/n')		continue;}void fink::writeall(std::ofstream &fou){	fou << Fink;	abstr_emp::writeall(fou);	fou << reportsto<<endl;}void fink::getall(std::ifstream &fin){	abstr_emp::getall(fin);	std::getline(fin,reportsto);}//highlink methodsvoid highfink::ShowAll()const{	cout << endl;	abstr_emp::ShowAll();		//使用直接基類的保護(hù)方法進(jìn)行輸出	cout<<"/t"<< manager::InChargeOf();	cout<<"/t" << fink::ReportsTo()<<endl;}void highfink::SetAll(){	abstr_emp::SetAll();	string temp;				//這里要弄好一點(diǎn),不然會(huì)設(shè)置基類兩次,也可以自己	int tem;	cout << "Enter the inchangeof count: ";	//用的是書(shū)上那個(gè)保護(hù)方法,返回引用就可以対值修改	cin >> tem;	cin.get();		//處理?yè)Q行符	cout << "Enter the reportsto: ";	std::getline(cin, temp);	ReportsTo() = temp;	InChargeOf() = tem;}void highfink::writeall(std::ofstream &fou){	fou << Highfink;	abstr_emp::writeall(fou);	manager::FileChangeOf(fou);	fink::FileReportsTo(fou);}void highfink::getall(std::ifstream &fin){	abstr_emp::getall(fin);	manager::FileChangeOf(fin);	fink::FileReportsTo(fin);}c++ Primer Plus(習(xí)題17.7)

//大大的填空題//參考了別人的,才發(fā)現(xiàn)自己理解錯(cuò)了#include<iostream>#include<fstream>#include<algorithm>#include<string>#include<vector>#include<iterator>void ShowStr(const std::string&s) { std::cout << s << std::endl; }void GetStrs(std::ifstream&fi, std::vector<std::string>&s);class Store{private:	std::ofstream&fou;public:	Store(std::ofstream&os) :fou(os) {};	void operator()(const std::string&s)	{		size_t len = s.size();				//size的返回類型,len存儲(chǔ)字符串的長(zhǎng)度		fou.write((char*)&len, sizeof(std::size_t));	//先存儲(chǔ)長(zhǎng)度,書(shū)上的用意很明顯		//這樣寫(xiě)是先把字符串的長(zhǎng)度len長(zhǎng)度信息復(fù)制到相關(guān)聯(lián)的文件中		fou.write(s.data(), len);		//這里是指定字符串的字符數(shù),把字字符串不包括'/0'寫(xiě)入文件	}};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("string.dat", ios_base::out | ios_base::binary);	for_each(vostr.begin(), vostr.end(), Store(fout));	fout.close();	vector<string>vistr;	ifstream fin("string.dat", 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 from the file:/n";	for_each(vostr.begin(), vostr.end(), ShowStr);	return 0;}void GetStrs(std::ifstream&fi, std::vector<std::string>&ve){	size_t len;		//使用read來(lái)獲取長(zhǎng)度,這個(gè)方法很贊	while (fi.read((char *)&len, sizeof(size_t)))	{	//這里要知道循環(huán)條件就是字符數(shù)		char *st = new char[len];		fi.read(st, len);		//len按我的理解就是每個(gè)字符所占據(jù)的二進(jìn)制位數(shù)		st[len + 1] = '/0';		//由于沒(méi)有存儲(chǔ)空字符,所以這里人為加		ve.push_back(st);	}}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 屯昌县| 民县| 永善县| 乌苏市| 邵武市| 秦安县| 新巴尔虎左旗| 荥经县| 台湾省| 罗城| 嘉黎县| 勃利县| 永仁县| 吉林省| 新疆| 奇台县| 涡阳县| 富民县| 宁陕县| 洛川县| 久治县| 班玛县| 阳山县| 灌阳县| 湖南省| 濮阳县| 皋兰县| 株洲县| 和田市| 吉安县| 合作市| 修武县| 沾益县| 囊谦县| 玉屏| 藁城市| 达日县| 吴川市| 永修县| 靖宇县| 北碚区|