c++ PRimer Plus(习题17.1)
//编写一个程序计算$之前的字符数,并且把它留在输入流中#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方法查看下一个字符 { cin.get(r); //用<<读取会跳过空格 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(习题17.2)//将键盘输入弄到文件里面,直到模拟的文件结尾#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]); //这是命令行的第二个参数 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) //这里是用输入流的方法来检查文件结尾 { getline(cin, ch); cout << ch; fin << ch; } cout << "Input complete!/n"; fin.close(); return 0;}c++ Primer Plus(习题17.3)//和上一题差不多,不过是将一个文件的内容复制到另一个文件//也是用命令行来指定文件的参数#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]); //这是命令行的第二个参数 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); //逐字符读取和写入 } cout << "/nCopy complete!/n"; //程序出现一个问题,就是会把源文件最后的字符写入两下到目标文件中 fou.close(); fin.close(); return 0;}c++ Primer Plus(习题17.4)//三个文件的输入输出,对文件进行拼接,并写入到第三个文件中//这个程序是让用户给出两个文件的内容,然后程序显示拼接后的结果//输出文件有个小小的空格,这个问题很小,可是我不知道#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(习题17.5)//这题和16章的差不多,不过是在文件里面读取名字到容器里面//所以自己准备好名字,容器这里有#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; //建立第二个容器 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; //还是书上的老方法,用一个匿名的插入迭代器 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(习题17.6)//第十七章的17.6题,跟前面14.5的相似,不过是添加了一点东西//书上提供了解决方案,添加数据到文件中,保存读写数据的方法//对了,书上的getall()方法个人觉得是要带参数的//这题打出来之后出现已有主体C2084错误,原因是该函数重定义了//去掉之后出现错误LNK2001,又是这个类设计很常见,起码是我这里很常见的//主要是无法识别的外部命令,//错误原因:empoyee的虚函数没有定义//我的文件读取方式是/*0TripHarrisThumper*/#include<iostream>using namespace std;#include"fileemp.h"const int MAX = 10;void showClassType();int main(void){ abstr_emp *pc[MAX]; //不可以创建抽象类型的数组,但是可以创建指针 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); //追加模式打开文件 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编码格式//这个头文件属于14.5题书上提供的多态继承头文件的修改版,输出数据到文件,//可以按照书上的方法进行修改#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); //写入到文件的虚函数,又是这里出现无法识别的外部命令 virtual void getall(ifstream &fi); //从文件中读取的虚函数 friend std::ostream&Operator<<(std::ostream &os, const abstr_emp&e); virtual ~abstr_emp() = 0 {};//这里少了{}会报错,链接错误一大堆};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); //写入到文件的虚函数 void getall(std::ifstream &fi); //从文件中读取的虚函数};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); //写入到文件的虚函数 void getall(std::ifstream &fi); //从文件中读取的虚函数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); //写入到文件的虚函数 void getall(std::ifstream &fi); //从文件中读取的虚函数};class highfink :public manager, public fink{public: highfink() {}; //显式调用基类构造函数 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); //写入到文件的虚函数 void getall(std::ifstream &fi); //从文件中读取的虚函数};#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;}//设置各成员的值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);}//写入数据到文件的方法void abstr_emp::writeall(ofstream &fo){ fo<<lname <<endl<< fname <<endl<<job<<endl;}void abstr_emp::getall(ifstream &fi) //书上没有用参数,想想不太可能{ 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(); //使用直接基类的保护方法进行输出 cout<<"/t"<< manager::InChargeOf(); cout<<"/t" << fink::ReportsTo()<<endl;}void highfink::SetAll(){ abstr_emp::SetAll(); string temp; //这里要弄好一点,不然会设置基类两次,也可以自己 int tem; cout << "Enter the inchangeof count: "; //用的是书上那个保护方法,返回引用就可以対值修改 cin >> tem; cin.get(); //处理换行符 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(习题17.7)//大大的填空题//参考了别人的,才发现自己理解错了#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存储字符串的长度 fou.write((char*)&len, sizeof(std::size_t)); //先存储长度,书上的用意很明显 //这样写是先把字符串的长度len长度信息复制到相关联的文件中 fou.write(s.data(), len); //这里是指定字符串的字符数,把字字符串不包括'/0'写入文件 }};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来获取长度,这个方法很赞 while (fi.read((char *)&len, sizeof(size_t))) { //这里要知道循环条件就是字符数 char *st = new char[len]; fi.read(st, len); //len按我的理解就是每个字符所占据的二进制位数 st[len + 1] = '/0'; //由于没有存储空字符,所以这里人为加 ve.push_back(st); }}
新闻热点
疑难解答
图片精选